001: /* CheckableTreeArray.java
002:
003: {{IS_NOTE
004:
005: Purpose:
006: Description:
007: History:
008: 2001/10/05 14:43:05, Create, Tom M. Yeh.
009: }}IS_NOTE
010:
011: Copyright (C) 2001 Potix Corporation. All Rights Reserved.
012:
013: {{IS_RIGHT
014: This program is distributed under GPL Version 2.0 in the hope that
015: it will be useful, but WITHOUT ANY WARRANTY.
016: }}IS_RIGHT
017: */
018: package org.zkoss.util;
019:
020: import java.util.Collection;
021: import java.util.Iterator;
022:
023: /**
024: * The checkable TreeArray. It extends TreeArray such that deriving
025: * classes could add validation when an element is added, removed or set.
026: * It is also useful to maintain the modification flag.
027: *
028: * @author tomyeh
029: */
030: public abstract class CheckableTreeArray extends TreeArray {
031: protected CheckableTreeArray() {
032: }
033:
034: protected CheckableTreeArray(Collection c) {
035: super (c);
036: }
037:
038: //-- deriving to override --//
039: /** Called each time an new element is about being added into the array.
040: *
041: * <p>Deriving classes usually put checking codes here.
042: * And, throws exception if failure and nothing will be affected.
043: *
044: * @param newElement the element to be added
045: * @param followingElement the elment that will 'follow' the new element.
046: * In other words, newElement will be inserted <b>before</b>
047: * followingElement. If null, it means newElement is appended at the end
048: */
049: protected void onAdd(Object newElement, Object followingElement) {
050: }
051:
052: /** Called each time an element is about being assigned into the array
053: * and replace an existence one (by ListIterator.set).
054: *
055: * <p>Deriving classes usually put checking codes here.
056: * And, throws exception if failure and nothing will be affected.
057: *
058: * @param newElement the element to be added
059: * @param replaced the element to be replaced
060: */
061: protected void onSet(Object newElement, Object replaced) {
062: }
063:
064: /** Called each time an element is about being removed from the array.
065: * Deriving classes usually put checking codes here.
066: * And, throws exception if failure.
067: */
068: protected void onRemove(Object element) {
069: }
070:
071: //-- TreeArray --//
072: protected RbEntry newEntry(Object element) {
073: return new CkEntry(element);
074: }
075:
076: protected class CkEntry extends RbEntry {
077: protected CkEntry(Object element) {
078: super (element);
079: }
080:
081: public void setElement(Object element) {
082: onSet(element, this .element);
083: super .setElement(element);
084: }
085: }
086:
087: protected RbEntry insert(RbEntry insertBefore, final RbEntry p) {
088: onAdd(p.element, insertBefore != null ? insertBefore.element
089: : null);
090: return super .insert(insertBefore, p);
091: }
092:
093: protected void delete(RbEntry p) {
094: onRemove(p.element);
095: super .delete(p);
096: }
097:
098: public void clear() {
099: for (final Iterator iter = iterator(); iter.hasNext();)
100: onRemove(iter.next());
101: super.clear();
102: }
103: }
|