1 package be
.nikiroo
.jvcard
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Collection
;
5 import java
.util
.Iterator
;
7 import java
.util
.ListIterator
;
10 * This class is basically a List with a parent and a "dirty" state check. It
11 * sends all commands down to the initial list, but will mark itself and its
12 * children as dirty or not when needed.
14 * All child elements can identify their parent.
16 * The dirty state is bubbling up (when dirty = true) or down (when dirty =
17 * false) -- so, making changes to a child element will also mark its parent as
18 * "dirty", and marking an element as pristine will also affect all its child
24 * the type of the child elements
26 public abstract class BaseClass
<E
extends BaseClass
<?
>> implements List
<E
> {
27 protected boolean dirty
;
28 protected BaseClass
<?
> parent
;
32 * Create a new {@link BaseClass} with the given list as its descendants.
35 * the descendants of this object, or NULL if none
37 protected BaseClass(List
<E
> list
) {
38 this.list
= new ArrayList
<E
>();
40 for (E child
: this) {
46 * Check if this element has unsaved changes.
48 * @return TRUE if it has
50 public boolean isDirty() {
55 * Delete this element from its parent if any.
57 * @return TRUE in case of success
59 public boolean delete() {
61 return parent
.remove(this);
68 * Replace the elements contained in this with those in the given
71 * Note: the elements will be copied from the {@link List}, you cannot
72 * manage the {@link List} from outside
75 * the list of new elements
77 public void replaceListContent(List
<E
> list
) {
78 List
<E
> del
= new ArrayList
<E
>();
79 List
<E
> add
= new ArrayList
<E
>();
81 for (E oldChild
: this) {
82 if (!list
.contains(oldChild
)) {
86 for (E newChild
: list
) {
87 if (!contains(newChild
)) {
97 * Notify that this element has unsaved changes.
104 * Notify this element <i>and all its descendants</i> that it is in pristine
105 * state (as opposed to dirty).
109 for (E child
: this) {
115 * Set the parent of this element <i>and all its descendants</i>.
120 void setParent(BaseClass
<?
> parent
) {
121 this.parent
= parent
;
122 for (E child
: this) {
123 child
.setParent(this);
128 * Each element that leaves the parent will pass trough here.
131 * the element to remove from this
133 private void _leave(E child
) {
138 * Each element that enters the parent will pass trough here.
141 * the element to add to this
143 private void _enter(E child
) {
144 _enter(child
, false);
148 * Each element that enters the parent will pass trough here.
151 * the element to add to this
153 private void _enter(E child
, boolean initialLoad
) {
154 child
.setParent(this);
160 public boolean add(E e
) {
166 @SuppressWarnings("unchecked")
167 public boolean remove(Object o
) {
168 if (list
.remove(o
)) {
169 if (o
instanceof BaseClass
<?
>) {
170 _leave((E
) o
); // expected warning
179 public boolean addAll(Collection
<?
extends E
> c
) {
184 return list
.addAll(c
);
188 public boolean addAll(int index
, Collection
<?
extends E
> c
) {
193 return list
.addAll(index
, c
);
197 public boolean removeAll(Collection
<?
> c
) {
198 boolean changed
= false;
209 public boolean retainAll(Collection
<?
> c
) {
210 ArrayList
<Object
> del
= new ArrayList
<Object
>();
214 return removeAll(del
);
218 public void clear() {
219 for (E child
: this) {
227 public E
set(int index
, E element
) {
228 E child
= get(index
);
233 return list
.set(index
, element
);
237 public void add(int index
, E element
) {
239 list
.add(index
, element
);
243 public E
remove(int index
) {
244 E child
= get(index
);
246 return list
.remove(index
);
250 public Iterator
<E
> iterator() {
251 return listIterator(0);
255 public ListIterator
<E
> listIterator() {
256 return listIterator(0);
260 public ListIterator
<E
> listIterator(int index
) {
262 return new ListIterator
<E
>() {
263 ListIterator
<E
> base
= list
.listIterator(i
);
267 public boolean hasNext() {
268 return base
.hasNext();
278 public boolean hasPrevious() {
279 return base
.hasPrevious();
283 public E
previous() {
284 last
= base
.previous();
289 public int nextIndex() {
290 return base
.nextIndex();
294 public int previousIndex() {
295 return base
.previousIndex();
299 public void remove() {
305 public void set(E e
) {
312 public void add(E e
) {
320 public Object
[] toArray() {
321 return list
.toArray();
325 public <T
> T
[] toArray(T
[] a
) {
326 return list
.toArray(a
);
335 public boolean isEmpty() {
336 return list
.isEmpty();
340 public boolean contains(Object o
) {
341 return list
.contains(o
);
345 public boolean containsAll(Collection
<?
> c
) {
346 return list
.containsAll(c
);
350 public E
get(int index
) {
351 return list
.get(index
);
355 public int indexOf(Object o
) {
356 return list
.indexOf(o
);
360 public int lastIndexOf(Object o
) {
361 return list
.lastIndexOf(o
);
365 public List
<E
> subList(int fromIndex
, int toIndex
) {
366 return list
.subList(fromIndex
, toIndex
);