com.avaje.ebean.common.ModifyHolder Maven / Gradle / Ivy
package com.avaje.ebean.common;
import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* Holds sets of additions and deletions from a 'owner' List Set or Map.
*
* These sets of additions and deletions are used to support persisting
* ManyToMany relationships. The additions becoming inserts into the
* intersection table and the removals becoming deletes from the intersection
* table.
*
*/
class ModifyHolder implements Serializable {
private static final long serialVersionUID = 2572572897923801083L;
/**
* Deletions list for manyToMany persistence.
*/
private Set modifyDeletions = new LinkedHashSet();
/**
* Additions list for manyToMany persistence.
*/
private Set modifyAdditions = new LinkedHashSet();
void reset() {
modifyDeletions = new LinkedHashSet();
modifyAdditions = new LinkedHashSet();
}
/**
* Used by BeanList.addAll() methods.
*/
void modifyAdditionAll(Collection extends E> c) {
if (c != null) {
for (E e : c) {
modifyAddition(e);
}
}
}
void modifyAddition(E bean) {
if (bean != null) {
// If it is to delete then just remove the deletion
if (!modifyDeletions.remove(bean)) {
// Insert
modifyAdditions.add(bean);
}
}
}
@SuppressWarnings("unchecked")
void modifyRemoval(Object bean) {
if (bean != null) {
// If it is to be added then just remove the addition
if (!modifyAdditions.remove(bean)) {
modifyDeletions.add((E) bean);
}
}
}
Set getModifyAdditions() {
return modifyAdditions;
}
Set getModifyRemovals() {
return modifyDeletions;
}
}