Quick method to convert classes implementing same interface
How, in Java, to quickly (or generically) convert one class that implements an interface into another class that implements the same interface?
I mean, if they're a POJO one class setters should take the other class getters as arguments.
Is there a Pattern for this situation?
Asked by: Sawyer586 | Posted: 21-01-2022
Answer 1
The Apache Bean Utilities package has a tool for this.
org.apache.commons.beanutils.BeanUtils
.BeanUtils.copyProperties
public static void copyProperties(Object dest, Object orig)
throws IllegalAccessException,
InvocationTargetException
Copy property values from the origin bean to the destination bean for all cases where the property names are the same.
For more details see BeanUtilsBean.
Parameters:
dest - Destination bean whose properties are modified
orig - Origin bean whose properties are retrieved
Answered by: Adelaide648 | Posted: 22-02-2022
Answer 2
I believe that the pattern for this situation is called a Proxy:
Answered by: Max615 | Posted: 22-02-2022A proxy, in its most general form, is a class functioning as an interface to another thing. The other thing could be anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate.
Similar questions
java - What (not) to declare when implementing an interface with an abstract class?
I have an interface A, for which I have to supply a few different
implementations. However, those implementations share some helper methods, so
I moved those methods to an abstract base class.
Interface A {
void doX();
}
abstract Class B implements A {
protected void commonY() {
// ...
}
@Override
public abstract void doX();
}
Class C extends B {
@Override
public void ...
Find Java classes implementing an interface
This question already has answers here:
c# - Overhead of implementing an interface
One of my colleagues told me that implementing interfaces introduces overhead. Is this true?
I am not concerned about micro optimizations; I just want to know the deeper details this entails.
java - A class implementing an interface that takes an enum
So, say I have a simple enum and a class that uses it:
enum ThingType { POTATO, BICYCLE };
class Thing {
public void setValueType(ThingType value) { ... }
public ThingType getValueType() { ... }
}
But, in reality, I have lots of different classes that implement setValueType, each with a different kind of enum. I want to make an interface that these classes can implement that suppo...
Java Generics for class objects implementing a specific interface
I am trying to define
HashMap< ? , String>
where ? is a class object from some implementation of a given interface, P.
For example, for interface ISearchEngine, i want ? to possibly be Google.class, Yahoo.class, Bing.class, etc.
java - How to collect all classes implementing an interface at runtime?
For running all my test classes automatically, I look for all class files inside a dedicated directory, convert the path to a package name and check if this class implements the given interface:
try {
Class<? > myTestClass = Class.forName( constructedClassName );
if( myTestClass.isInstance( MyTestInterface.class ) ) {
testCollection.add( myTestClass );
}
}
catch( Error e ) {
//...
java - Implementing a generic interface with a raw type
I have a generic tree, the generic parameter is the data type stored by the nodes:
class TreeNode<D>{
public D data;
.....
}
Then a visitor interface to use along with a tree transversal:
interface Visitor<D> {
void visit(TreeNode<D> node);
}
Some visitors can take advantage of generics:
class DataListCr...
How can it possible that after implementing Runnable interface in java Thread methods are call?
How can it possible that after implementing Runnable interface in java Thread methods are call?
is it possible to create object for interface without implementing this by any of the classes in java?
Itest is an Interface. here i mentioned like new Itest(). Then is it means that i can create object for interface?
public interface Itest {
}
static final Itest s = new Itest(){
};
It is just like, we could create object for interface without any class implement the interface.
java - Is there a way to make sure classes implementing an Interface implement static methods?
First of all, I read erickson's useful reply to "Why can’t I define a static method in a Java interface?". This question is not about the "why" but about the "how then?".
Edit: my original example was ill-posed, but I'll leave it below.
While I am now convinced that in most cases what I wa...
c# - Implementing a tree from scratch
I'm trying to learn about trees by implementing one from scratch.
In this case I'd like to do it in C# Java or C++. (without using built in methods)
So each node will store a character and there will be a maximum of 26 nodes per node.
What data structure would I use to contain the pointers to each of the nodes?
Basically I'm trying to implement a radix tree from scratch.
Thanks,
java - What (not) to declare when implementing an interface with an abstract class?
I have an interface A, for which I have to supply a few different
implementations. However, those implementations share some helper methods, so
I moved those methods to an abstract base class.
Interface A {
void doX();
}
abstract Class B implements A {
protected void commonY() {
// ...
}
@Override
public abstract void doX();
}
Class C extends B {
@Override
public void ...
Find Java classes implementing an interface
This question already has answers here:
What data structure is most suitable for implementing a 2-D array in Java?
I want to implement a 2-D array kind of a thing.
What data structure will be most suitable for this? An array or some other data-structure will do. If there is any other data structure which will satisfy my requirement, then please tell me.
I don't want to use an array because the 2-D array needs to be declared early in the program but it is not fixed; the size will be determined at run time.
Also, ...
java - implementing a "window manager" for HTML in a JSP custom tag
I have a problem:
I need to build a custom tag, which can take its child tags (each of which will render as an HTML widget of some sort) and render them into the page in some intelligent manner.
It's a fairly open ended question, so I have thought of many different ways to solve it. What I need is some input on how:
others have done it
ideas
My ideas:
using somet...
java - Implementing a matrix, which is more efficient - using an Array of Arrays (2D) or a 1D array?
When implementing a Matrix construct using arrays, which would be more efficient?
Using a 1D array, or an array of arrays (2D)?
I would think a 2D is more efficient as you already have the X and Y coordinates of an element, where in a 1D implementation you have to calculate the index.
Edit: it is being implemented using Java
java - implementing interfaces after the fact
I think, the following can't be done in Java. But I would be happy to learn how to implement something that resembles it.
Suppose we have a class C, that is already used in compiled code. (We can neither change that code nor the original definition of C).
Suppose further there is interesting code that could be re-used, if only C would implement interface I. It is, in fact, more or less trivial to derive D t...
struts - Implementing dynamic pagesize in pagination struts2 + java
I am trying to implement pagination through display tag in Struts2.
Now my requirement is I have a combo box which has some page size value like 5, 10, 15 ..
So, How can I update that value in page size of display tag in Struts2 ?
java - Implementing a chat server as a WebService
I have a school project in which I have to implement a chat application, whose server will be a java web service.
The problem is that I've always thought of a web service as a way of calling remote functions, and I have no idea how to keep a "session" active on the web service, nor how to keep track of all the people currently in chat, rooms etc.
JAX-RS is perfect for implementing REST. What do you use to call REST services in Java?
Closed. This question does not meet Stack Overflow guid...
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)