1 package be
.nikiroo
.jvcard
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Collection
;
5 import java
.util
.Collections
;
6 import java
.util
.Comparator
;
7 import java
.util
.Iterator
;
8 import java
.util
.LinkedList
;
10 import java
.util
.ListIterator
;
12 import be
.nikiroo
.jvcard
.resources
.StringUtils
;
15 * This class is basically a List with a parent and a "dirty" state check. It
16 * sends all commands down to the initial list, but will mark itself and its
17 * children as dirty or not when needed.
19 * All child elements can identify their parent.
21 * The dirty state is bubbling up (when dirty = true) or down (when dirty =
22 * false) -- so, making changes to a child element will also mark its parent as
23 * "dirty", and marking an element as pristine will also affect all its child
29 * the type of the child elements
31 public abstract class BaseClass
<E
extends BaseClass
<?
>> implements List
<E
> {
32 protected boolean dirty
;
33 protected BaseClass
<?
> parent
;
36 private Comparator
<E
> comparator
= new Comparator
<E
>() {
38 public int compare(E o1
, E o2
) {
39 if (o1
== null && o2
== null)
41 if (o1
== null && o2
!= null)
43 if (o1
!= null && o2
== null)
46 return o1
.getId().compareTo(o2
.getId());
51 * Create a new {@link BaseClass} with the items in the given list as its
54 * Note: the elements will be copied from the {@link List}, you cannot
55 * manage the {@link List} from outside
58 * the descendants of this object, or NULL if none
60 protected BaseClass(List
<E
> list
) {
61 this.list
= new ArrayList
<E
>();
64 this.list
.addAll(list
);
67 for (E child
: this) {
73 * Check if this element has unsaved changes.
75 * @return TRUE if it has
77 public boolean isDirty() {
82 * Delete this element from its parent if any.
84 * @return TRUE in case of success
86 public boolean delete() {
88 return parent
.remove(this);
95 * Replace the elements contained in this with those in the given
98 * Note: the elements will be copied from the {@link List}, you cannot
99 * manage the {@link List} from outside
102 * the list of new elements
104 public void replaceListContent(List
<E
> list
) {
105 List
<E
> del
= new LinkedList
<E
>();
106 List
<E
> add
= new LinkedList
<E
>();
108 if (!compare(list
, add
, del
, del
, add
)) {
115 * Compare the elements contained in <tt>this</tt> with those in the given
116 * {@link List}. It will return TRUE in case of equality, will return FALSE
119 * If not equals, the differences will be represented by the given
120 * {@link List}s if they are not NULL.
122 * <li><tt>added</tt>will represent the elements in <tt>list</tt> but not in
124 * <li><tt>removed</tt> will represent the elements in <tt>this</tt> but not
125 * in <tt>list</tt></li>
126 * <li><tt>from<tt> will represent the elements in <tt>list</tt> that are
127 * already contained in <tt>this</tt> but are not equals to them (the
128 * original element from <tt>this</tt> is stored here)</li>
129 * <li><tt>to<tt> will represent the elements in <tt>list</tt> that are
130 * already contained in <tt>this</tt> but are not equals to them (the
131 * changed element from <tt>list</tt> is stored here)</li>
135 * the list of new elements
137 * the list to add the <tt>added</tt> elements to, or NULL
139 * the list to add the <tt>removed</tt> elements to, or NULL
141 * the map to add the <tt>from</tt> elements, or NULL
143 * the map to add the <tt>to</tt> elements, or NULL
145 * @return TRUE if the elements are identical
147 @SuppressWarnings({ "unchecked", "rawtypes" })
148 public boolean compare(List
<E
> list
, List
<E
> added
, List
<E
> removed
,
149 List
<E
> from
, List
<E
> to
) {
150 Collections
.sort(this.list
, comparator
);
152 List
<E
> mine
= new LinkedList
<E
>(this.list
);
153 List
<E
> other
= new LinkedList
<E
>(list
);
155 Collections
.sort(other
, comparator
);
158 E here
= mine
.size() > 0 ? mine
.remove(0) : null;
159 E there
= other
.size() > 0 ? other
.remove(0) : null;
161 while (here
!= null || there
!= null) {
163 || (there
!= null && comparator
.compare(here
, there
) > 0)) {
168 } else if (there
== null || comparator
.compare(here
, there
) < 0) {
174 // they represent the same item
175 if (!((BaseClass
) here
).isEquals(there
, false)) {
186 if (here
== null && mine
.size() > 0)
187 here
= mine
.remove(0);
188 if (there
== null && other
.size() > 0)
189 there
= other
.remove(0);
196 * Check if the given instance and this one represent the same objects (they
197 * may have different states).
202 * @return TRUE if they represent the same object
204 public boolean isSame(BaseClass
<E
> other
) {
208 if (!getClass().equals(other
.getClass()))
211 return getId().equals(other
.getId());
215 * Check if the given instance and this one are equivalent (both objects in
216 * the same state, all child elements equivalent).
222 * do not check the state of the object itslef, only its content
224 * @return TRUE if they are equivalent
226 @SuppressWarnings({ "unchecked", "rawtypes" })
227 public boolean isEquals(BaseClass
<E
> other
, boolean contentOnly
) {
231 if (size() != other
.size())
238 if (!getState().equals(other
.getState()))
242 Collections
.sort(list
, comparator
);
243 Collections
.sort(other
.list
, other
.comparator
);
244 for (int index
= 0; index
< size(); index
++) {
245 if (!((BaseClass
) get(index
)).isEquals(other
.get(index
), false))
253 * Get the recursive state of the current object, i.e., its children. It
254 * represents the full state information about this object's children.
256 * @return a {@link String} representing the current content state of this
257 * object, i.e., its children included
259 public String
getContentState() {
260 StringBuilder builder
= new StringBuilder();
261 buildContentStateRaw(builder
);
262 return StringUtils
.getHash(builder
.toString());
266 * Return the (first) child element with the given ID or NULL if not found.
271 * @return the child element or NULL
273 public E
getById(String id
) {
274 for (E child
: this) {
276 if (child
.getId() == null)
279 if (id
.equals(child
.getId()))
288 * Return the current ID of this object -- it is allowed to change over time
289 * (so, do not cache it).
291 * @return the current ID
293 abstract public String
getId();
296 * Get the state of the current object, children <b>not included</b>. It
297 * represents the full state information about this object, but do not check
298 * its children (see {@link BaseClass#getContentState()} for that).
300 * @return a {@link String} representing the current state of this object,
301 * children not included
303 abstract public String
getState();
306 * Get the recursive state of the current object, i.e., its children. It
307 * represents the full state information about this object's children.
312 * the {@link StringBuilder} that will represent the current
313 * content state of this object, i.e., its children included
315 void buildContentStateRaw(StringBuilder builder
) {
316 builder
.append(getState());
317 for (E child
: this) {
318 child
.buildContentStateRaw(builder
);
323 * Notify that this element has unsaved changes.
327 if (parent
!= null) {
333 * Notify this element <i>and all its descendants</i> that it is in pristine
334 * state (as opposed to dirty).
338 for (E child
: this) {
344 * Set the parent of this element <i>and all its descendants</i>.
349 void setParent(BaseClass
<?
> parent
) {
350 this.parent
= parent
;
351 for (E child
: this) {
352 child
.setParent(this);
357 * Each element that leaves the parent will pass trough here.
360 * the element to remove from this
362 private void _leave(E child
) {
367 * Each element that enters the parent will pass trough here.
370 * the element to add to this
372 private void _enter(E child
) {
373 _enter(child
, false);
377 * Each element that enters the parent will pass trough here.
380 * the element to add to this
382 private void _enter(E child
, boolean initialLoad
) {
383 child
.setParent(this);
391 public boolean add(E e
) {
397 @SuppressWarnings("unchecked")
398 public boolean remove(Object o
) {
399 if (list
.remove(o
)) {
400 if (o
instanceof BaseClass
<?
>) {
401 _leave((E
) o
); // expected warning
410 public boolean addAll(Collection
<?
extends E
> c
) {
415 return list
.addAll(c
);
419 public boolean addAll(int index
, Collection
<?
extends E
> c
) {
424 return list
.addAll(index
, c
);
428 public boolean removeAll(Collection
<?
> c
) {
429 boolean changed
= false;
440 public boolean retainAll(Collection
<?
> c
) {
441 ArrayList
<Object
> del
= new ArrayList
<Object
>();
445 return removeAll(del
);
449 public void clear() {
450 for (E child
: this) {
458 public E
set(int index
, E element
) {
459 E child
= get(index
);
464 return list
.set(index
, element
);
468 public void add(int index
, E element
) {
470 list
.add(index
, element
);
474 public E
remove(int index
) {
475 E child
= get(index
);
477 return list
.remove(index
);
481 public Iterator
<E
> iterator() {
482 return listIterator(0);
486 public ListIterator
<E
> listIterator() {
487 return listIterator(0);
491 public ListIterator
<E
> listIterator(int index
) {
493 return new ListIterator
<E
>() {
494 ListIterator
<E
> base
= list
.listIterator(i
);
498 public boolean hasNext() {
499 return base
.hasNext();
509 public boolean hasPrevious() {
510 return base
.hasPrevious();
514 public E
previous() {
515 last
= base
.previous();
520 public int nextIndex() {
521 return base
.nextIndex();
525 public int previousIndex() {
526 return base
.previousIndex();
530 public void remove() {
536 public void set(E e
) {
543 public void add(E e
) {
551 public Object
[] toArray() {
552 return list
.toArray();
556 public <T
> T
[] toArray(T
[] a
) {
557 return list
.toArray(a
);
566 public boolean isEmpty() {
567 return list
.isEmpty();
571 public boolean contains(Object o
) {
572 return list
.contains(o
);
576 public boolean containsAll(Collection
<?
> c
) {
577 return list
.containsAll(c
);
581 public E
get(int index
) {
582 return list
.get(index
);
586 public int indexOf(Object o
) {
587 return list
.indexOf(o
);
591 public int lastIndexOf(Object o
) {
592 return list
.lastIndexOf(o
);
596 public List
<E
> subList(int fromIndex
, int toIndex
) {
597 return list
.subList(fromIndex
, toIndex
);