X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2FBaseClass.java;h=0aa18e6ff66336a3f2afecacc9ba6fbe1b4a380b;hb=4298276a4b717753397508ce5432071827d5b294;hp=b6be10e47795fd31d1169c07068eb4ca3963c6b2;hpb=e253bd50bb05519f4a16fed4fb95d5b3340128ea;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/BaseClass.java b/src/be/nikiroo/jvcard/BaseClass.java index b6be10e..0aa18e6 100644 --- a/src/be/nikiroo/jvcard/BaseClass.java +++ b/src/be/nikiroo/jvcard/BaseClass.java @@ -9,6 +9,8 @@ import java.util.LinkedList; import java.util.List; import java.util.ListIterator; +import be.nikiroo.jvcard.resources.StringUtils; + /** * This class is basically a List with a parent and a "dirty" state check. It * sends all commands down to the initial list, but will mark itself and its @@ -153,28 +155,38 @@ public abstract class BaseClass> implements List { Collections.sort(other, comparator); boolean equ = true; - while (mine.size() > 0 || other.size() > 0) { - E here = (mine.size() > 0) ? mine.remove(0) : null; - E there = (other.size() > 0) ? other.remove(0) : null; + E here = mine.size() > 0 ? mine.remove(0) : null; + E there = other.size() > 0 ? other.remove(0) : null; - if (here == null || comparator.compare(here, there) > 0) { + while (here != null || there != null) { + if (here == null + || (there != null && comparator.compare(here, there) > 0)) { if (added != null) added.add(there); + there = null; equ = false; } else if (there == null || comparator.compare(here, there) < 0) { if (removed != null) removed.add(here); + here = null; equ = false; } else { // they represent the same item - if (!((BaseClass) here).isEquals(there)) { + if (!((BaseClass) here).isEquals(there, false)) { if (from != null) from.add(here); if (to != null) to.add(there); equ = false; } + here = null; + there = null; } + + if (here == null && mine.size() > 0) + here = mine.remove(0); + if (there == null && other.size() > 0) + there = other.remove(0); } return equ; @@ -206,32 +218,72 @@ public abstract class BaseClass> implements List { * @param other * the other instance * + * @param contentOnly + * do not check the state of the object itslef, only its content + * * @return TRUE if they are equivalent */ @SuppressWarnings({ "unchecked", "rawtypes" }) - public boolean isEquals(BaseClass other) { + public boolean isEquals(BaseClass other, boolean contentOnly) { if (other == null) return false; if (size() != other.size()) return false; - if (!isSame(other)) - return false; + if (!contentOnly) { + if (!isSame(other)) + return false; - if (!getState().equals(other.getState())) - return false; + if (!getState().equals(other.getState())) + return false; + } Collections.sort(list, comparator); Collections.sort(other.list, other.comparator); for (int index = 0; index < size(); index++) { - if (!((BaseClass) get(index)).isEquals(other.get(index))) + if (!((BaseClass) get(index)).isEquals(other.get(index), false)) return false; } return true; } + /** + * Get the recursive state of the current object, i.e., its children. It + * represents the full state information about this object's children. + * + * @return a {@link String} representing the current content state of this + * object, i.e., its children included + */ + public String getContentState() { + StringBuilder builder = new StringBuilder(); + buildContentStateRaw(builder); + return StringUtils.getHash(builder.toString()); + } + + /** + * Return the (first) child element with the given ID or NULL if not found. + * + * @param id + * the id to look for + * + * @return the child element or NULL + */ + public E getById(String id) { + for (E child : this) { + if (id == null) { + if (child.getId() == null) + return child; + } else { + if (id.equals(child.getId())) + return child; + } + } + + return null; + } + /** * Return the current ID of this object -- it is allowed to change over time * (so, do not cache it). @@ -242,16 +294,31 @@ public abstract class BaseClass> implements List { /** * Get the state of the current object, children not included. It - * represents the full state information about this object, that is, two - * objects with the same state (and class) must return TRUE if - * {@link BaseClass#isEquals(BaseClass)} is called and their children - * are equivalent. + * represents the full state information about this object, but do not check + * its children (see {@link BaseClass#getContentState()} for that). * * @return a {@link String} representing the current state of this object, * children not included */ abstract public String getState(); + /** + * Get the recursive state of the current object, i.e., its children. It + * represents the full state information about this object's children. + * + * It is not hashed. + * + * @param builder + * the {@link StringBuilder} that will represent the current + * content state of this object, i.e., its children included + */ + void buildContentStateRaw(StringBuilder builder) { + builder.append(getState()); + for (E child : this) { + child.buildContentStateRaw(builder); + } + } + /** * Notify that this element has unsaved changes. */