Fireblaze wrote:
Keeping the POJO as simple as possible, there for only using the Java standard library. The solution is:
Code:
private List<Entity> list = new LinkedList<Entity>();
public void add(Entity entity) {
if(list.contains(entity)) {
return;
}
list.add(entity);
}
Or you could extract the code into a helper class, leaving only one line of code and no extra complexity in your POJO.
Code:
class CollectionHelper {
public static <E> void addNoDuplicates(Collection<E> first, E ... second) {
addNoDuplicates(first, Arrays.asList(second));
}
public static <E> void addNoDuplicates(Collection<E> first, Collection<E> second) {
for(E object: second) {
if(!first.contains(object)) {
first.add(object);
}
}
}
}
private List<Entity> list = new LinkedList<Entity>();
public void add(Entity entity) {
CollectionHelper.addNoDuplicates(list, entity);
}
Hi,
Thanks for that, I thought about that and agree it will work.
It will just result in an explosion of APIs on my pojo as I must have an accessor and mutator for every association and if I now also have to extra addXXX() and removeXXX() methods it will result in an explosion of APIs as I have quite a lot of relationship associations.
It's a pity there isn't a
@NoDuplicates annotation.
Thanks for your comments.