Is there a standard interface for objects that populate their values from same-type objects?

Essentially the opposite of Cloneable, an interface declaring some method like this:
interface CopyableFrom {
void copyFrom(Object other);
}

class Foo implements CopyableFrom {
private int x;
private int y;

void copyFrom(Object other) {
if (other instanceof Foo foo) {
this.x = foo.x;
this.y = foo.y;
}
}
}
interface CopyableFrom {
void copyFrom(Object other);
}

class Foo implements CopyableFrom {
private int x;
private int y;

void copyFrom(Object other) {
if (other instanceof Foo foo) {
this.x = foo.x;
this.y = foo.y;
}
}
}
I feel like either the JDK or some Commons library (Apache, Guava) probably already has something like this, so I don't want to make my own if I can avoid it.
5 Replies
JavaBot
JavaBot6mo ago
This post has been reserved for your question.
Hey @Haiku! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st6mo ago
no but why would you need that to work with Object you can make something like this with records though why do you need it though?
Madjosz
Madjosz6mo ago
Are you coming from C++ and looking for something like the std::copy template?
dan1st
dan1st6mo ago
I don't really see a reason why that'd be necessary though
JavaBot
JavaBot6mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?