Search in sources :

Example 1 with Differable

use of org.bedework.calfacade.base.Differable in project bw-calendar-engine by Bedework.

the class ChangeTableEntry method diff.

/**
 * Compare the original and new collections. Update the removed and added
 * collections to reflect the changes that need to be made.
 *
 * <p>Alarms are the only peruser class we have to deal with.
 *
 * @param originalVals
 * @return true if collection set changed - that is, members were added or
 *              removed.
 */
@SuppressWarnings("unchecked")
public boolean diff(final Collection originalVals) {
    boolean alarms = index == PropertyInfoIndex.VALARM;
    Collection newVals = getNewValues();
    boolean collectionSetChanged = false;
    if (!Util.isEmpty(originalVals)) {
        if (!alarms && (newVals == null)) {
            // Remove everything
            setRemovedValues(getCollection(originalVals));
            getRemovedValues().addAll(originalVals);
            changed = true;
            collectionSetChanged = true;
        } else {
            for (Object o : originalVals) {
                if (alarms) {
                    BwAlarm al = (BwAlarm) o;
                    if (!al.getOwnerHref().equals(chg.getUserHref())) {
                        continue;
                    }
                }
                if ((newVals == null) || !newVals.contains(o)) {
                    if (getRemovedValues() == null) {
                        setRemovedValues(getCollection(originalVals));
                    }
                    getRemovedValues().add(o);
                    changed = true;
                    collectionSetChanged = true;
                }
            }
        }
    }
    if (newVals == null) {
        return collectionSetChanged;
    }
    if (Util.isEmpty(originalVals)) {
        // Add everything
        setAddedValues(getCollection(newVals));
        getAddedValues().addAll(newVals);
        added = true;
        changed = true;
        return true;
    }
    /* Here we also look for entries that have equals return true but are
     * in fact different in some way. These can be tested for if they
     * implement the Differable interface
     */
    List<Differable> differList = null;
    for (Object o : newVals) {
        if ((o instanceof Differable) && (differList == null)) {
            differList = new ArrayList<Differable>(originalVals);
        }
        if (!originalVals.contains(o)) {
            if (getAddedValues() == null) {
                setAddedValues(getCollection(newVals));
            }
            getAddedValues().add(o);
            changed = true;
            collectionSetChanged = true;
        } else if (differList != null) {
            // See if it changed
            Differable orig = differList.get(differList.indexOf(o));
            if (orig.differsFrom(o)) {
                if (getChangedValues() == null) {
                    setChangedValues(getCollection(newVals));
                }
                getChangedValues().add(o);
                changed = true;
            }
        }
    }
    return collectionSetChanged;
}
Also used : Differable(org.bedework.calfacade.base.Differable) Collection(java.util.Collection) BwAlarm(org.bedework.calfacade.BwAlarm)

Aggregations

Collection (java.util.Collection)1 BwAlarm (org.bedework.calfacade.BwAlarm)1 Differable (org.bedework.calfacade.base.Differable)1