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
.tui
.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 while (mine
.size() > 0 || other
.size() > 0) {
159 E here
= (mine
.size() > 0) ? mine
.remove(0) : null;
160 E there
= (other
.size() > 0) ? other
.remove(0) : null;
162 if (here
== null || comparator
.compare(here
, there
) > 0) {
166 } else if (there
== null || comparator
.compare(here
, there
) < 0) {
171 // they represent the same item
172 if (!((BaseClass
) here
).isEquals(there
, false)) {
186 * Check if the given instance and this one represent the same objects (they
187 * may have different states).
192 * @return TRUE if they represent the same object
194 public boolean isSame(BaseClass
<E
> other
) {
198 if (!getClass().equals(other
.getClass()))
201 return getId().equals(other
.getId());
205 * Check if the given instance and this one are equivalent (both objects in
206 * the same state, all child elements equivalent).
212 * do not check the state of the object itslef, only its content
214 * @return TRUE if they are equivalent
216 @SuppressWarnings({ "unchecked", "rawtypes" })
217 public boolean isEquals(BaseClass
<E
> other
, boolean contentOnly
) {
221 if (size() != other
.size())
228 if (!getState().equals(other
.getState()))
232 Collections
.sort(list
, comparator
);
233 Collections
.sort(other
.list
, other
.comparator
);
234 for (int index
= 0; index
< size(); index
++) {
235 if (!((BaseClass
) get(index
)).isEquals(other
.get(index
), false))
243 * Get the recursive state of the current object, i.e., its children. It
244 * represents the full state information about this object's children.
246 * @return a {@link String} representing the current content state of this
247 * object, i.e., its children included
249 public String
getContentState() {
250 StringBuilder builder
= new StringBuilder();
251 buildContentStateRaw(builder
);
252 return StringUtils
.getHash(builder
.toString());
256 * Return the (first) child element with the given ID or NULL if not found.
261 * @return the child element or NULL
263 public E
getById(String id
) {
264 for (E child
: this) {
266 if (child
.getId() == null)
269 if (id
.equals(child
.getId()))
278 * Return the current ID of this object -- it is allowed to change over time
279 * (so, do not cache it).
281 * @return the current ID
283 abstract public String
getId();
286 * Get the state of the current object, children <b>not included</b>. It
287 * represents the full state information about this object, but do not check
288 * its children (see {@link BaseClass#getContentState()} for that).
290 * @return a {@link String} representing the current state of this object,
291 * children not included
293 abstract public String
getState();
296 * Get the recursive state of the current object, i.e., its children. It
297 * represents the full state information about this object's children.
302 * the {@link StringBuilder} that will represent the current
303 * content state of this object, i.e., its children included
305 void buildContentStateRaw(StringBuilder builder
) {
306 builder
.append(getState());
307 for (E child
: this) {
308 child
.buildContentStateRaw(builder
);
313 * Notify that this element has unsaved changes.
317 if (parent
!= null) {
323 * Notify this element <i>and all its descendants</i> that it is in pristine
324 * state (as opposed to dirty).
328 for (E child
: this) {
334 * Set the parent of this element <i>and all its descendants</i>.
339 void setParent(BaseClass
<?
> parent
) {
340 this.parent
= parent
;
341 for (E child
: this) {
342 child
.setParent(this);
347 * Each element that leaves the parent will pass trough here.
350 * the element to remove from this
352 private void _leave(E child
) {
357 * Each element that enters the parent will pass trough here.
360 * the element to add to this
362 private void _enter(E child
) {
363 _enter(child
, false);
367 * Each element that enters the parent will pass trough here.
370 * the element to add to this
372 private void _enter(E child
, boolean initialLoad
) {
373 child
.setParent(this);
381 public boolean add(E e
) {
387 @SuppressWarnings("unchecked")
388 public boolean remove(Object o
) {
389 if (list
.remove(o
)) {
390 if (o
instanceof BaseClass
<?
>) {
391 _leave((E
) o
); // expected warning
400 public boolean addAll(Collection
<?
extends E
> c
) {
405 return list
.addAll(c
);
409 public boolean addAll(int index
, Collection
<?
extends E
> c
) {
414 return list
.addAll(index
, c
);
418 public boolean removeAll(Collection
<?
> c
) {
419 boolean changed
= false;
430 public boolean retainAll(Collection
<?
> c
) {
431 ArrayList
<Object
> del
= new ArrayList
<Object
>();
435 return removeAll(del
);
439 public void clear() {
440 for (E child
: this) {
448 public E
set(int index
, E element
) {
449 E child
= get(index
);
454 return list
.set(index
, element
);
458 public void add(int index
, E element
) {
460 list
.add(index
, element
);
464 public E
remove(int index
) {
465 E child
= get(index
);
467 return list
.remove(index
);
471 public Iterator
<E
> iterator() {
472 return listIterator(0);
476 public ListIterator
<E
> listIterator() {
477 return listIterator(0);
481 public ListIterator
<E
> listIterator(int index
) {
483 return new ListIterator
<E
>() {
484 ListIterator
<E
> base
= list
.listIterator(i
);
488 public boolean hasNext() {
489 return base
.hasNext();
499 public boolean hasPrevious() {
500 return base
.hasPrevious();
504 public E
previous() {
505 last
= base
.previous();
510 public int nextIndex() {
511 return base
.nextIndex();
515 public int previousIndex() {
516 return base
.previousIndex();
520 public void remove() {
526 public void set(E e
) {
533 public void add(E e
) {
541 public Object
[] toArray() {
542 return list
.toArray();
546 public <T
> T
[] toArray(T
[] a
) {
547 return list
.toArray(a
);
556 public boolean isEmpty() {
557 return list
.isEmpty();
561 public boolean contains(Object o
) {
562 return list
.contains(o
);
566 public boolean containsAll(Collection
<?
> c
) {
567 return list
.containsAll(c
);
571 public E
get(int index
) {
572 return list
.get(index
);
576 public int indexOf(Object o
) {
577 return list
.indexOf(o
);
581 public int lastIndexOf(Object o
) {
582 return list
.lastIndexOf(o
);
586 public List
<E
> subList(int fromIndex
, int toIndex
) {
587 return list
.subList(fromIndex
, toIndex
);