X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2FBaseClass.java;h=df0fcf00de78f998ad87d03842dc4947f86a966a;hb=e4444b0bc462544629d9e7e7ab62b96a4d9cab10;hp=abaa9ccbd37d04fe7a48fc8a2a57b8c59ebb267a;hpb=cf77cb3542f2aefbebdb9aa00b358dbeb4489a73;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/BaseClass.java b/src/be/nikiroo/jvcard/BaseClass.java index abaa9cc..df0fcf0 100644 --- a/src/be/nikiroo/jvcard/BaseClass.java +++ b/src/be/nikiroo/jvcard/BaseClass.java @@ -9,7 +9,7 @@ import java.util.LinkedList; import java.util.List; import java.util.ListIterator; -import be.nikiroo.jvcard.tui.StringUtils; +import be.nikiroo.jvcard.resources.StringUtils; /** * This class is basically a List with a parent and a "dirty" state check. It @@ -155,17 +155,20 @@ 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 @@ -176,7 +179,14 @@ public abstract class BaseClass> implements List { 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; @@ -242,19 +252,43 @@ public abstract class BaseClass> implements List { /** * Get the recursive state of the current object, i.e., its children. It * represents the full state information about this object's children. It - * does not check the state of the object itself. + * may not contains spaces nor new lines. + * + *

+ * Not that this state is lossy. You cannot retrieve the data from + * the state, it can only be used as an ID to check if thw data are + * identical. + *

* * @return a {@link String} representing the current content state of this - * object, i.e., its children + * 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) { - builder.append(child.getContentState()); + if (id == null) { + if (child.getId() == null) + return child; + } else { + if (id.equals(child.getId())) + return child; + } } - return StringUtils.getHash(builder.toString()); + return null; } /** @@ -268,13 +302,37 @@ 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, but do not check - * its children (see {@link BaseClass#getContentState()} for that). + * its children (see {@link BaseClass#getContentState()} for that). It may + * not contains spaces nor new lines. + * + *

+ * Not that this state is lossy. You cannot retrieve the data from + * the state, it can only be used as an ID to check if thw data are + * identical. + *

* * @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. */