001: /*
002: * Helma License Notice
003: *
004: * The contents of this file are subject to the Helma License
005: * Version 2.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://adele.helma.org/download/helma/license.txt
008: *
009: * Copyright 1998-2003 Helma Software. All Rights Reserved.
010: *
011: * $RCSfile$
012: * $Author: hannes $
013: * $Revision: 8729 $
014: * $Date: 2007-12-21 10:13:52 +0100 (Fre, 21 Dez 2007) $
015: */
016:
017: package helma.objectmodel.db;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021: import java.util.HashMap;
022: import java.util.Iterator;
023:
024: /**
025: * A subclass of ArrayList that adds an addSorted(Object) method to
026: */
027: public class SubnodeList extends ArrayList {
028:
029: transient WrappedNodeManager nmgr;
030: transient HashMap views = null;
031: transient Relation rel;
032:
033: /**
034: * Hide/disable zero argument constructor for subclasses
035: */
036: private SubnodeList() {
037: }
038:
039: /**
040: * Creates a new subnode list
041: * @param nmgr
042: */
043: public SubnodeList(WrappedNodeManager nmgr, Relation rel) {
044: this .nmgr = nmgr;
045: this .rel = rel;
046: }
047:
048: /**
049: * Inserts the specified element at the specified position in this
050: * list without performing custom ordering
051: *
052: * @param obj element to be inserted.
053: */
054: public boolean addSorted(Object obj) {
055: return add(obj);
056: }
057:
058: /**
059: * Adds the specified object to this list performing
060: * custom ordering
061: *
062: * @param obj element to be inserted.
063: */
064: public boolean add(Object obj) {
065: addToViews(obj);
066: return super .add(obj);
067: }
068:
069: /**
070: * Adds the specified object to the list at the given position
071: * @param idx the index to insert the element at
072: * @param obj the object t add
073: */
074: public void add(int idx, Object obj) {
075: addToViews(obj);
076: super .add(idx, obj);
077: }
078:
079: /**
080: * remove the object specified by the given index-position
081: * @param idx the index-position of the NodeHandle to remove
082: */
083: public Object remove(int idx) {
084: Object obj = get(idx);
085: if (obj != null) {
086: removeFromViews(obj);
087: }
088: return super .remove(idx);
089: }
090:
091: /**
092: * remove the given Object from this List
093: * @param obj the NodeHandle to remove
094: */
095: public boolean remove(Object obj) {
096: removeFromViews(obj);
097: return super .remove(obj);
098: }
099:
100: protected void removeFromViews(Object obj) {
101: if (views == null || views.isEmpty())
102: return;
103: for (Iterator i = views.values().iterator(); i.hasNext();) {
104: OrderedSubnodeList osl = (OrderedSubnodeList) i.next();
105: osl.remove(obj);
106: }
107: }
108:
109: public List getOrderedView(String order) {
110: String key = order.trim().toLowerCase();
111: // long start = System.currentTimeMillis();
112: if (views == null) {
113: views = new HashMap();
114: }
115: OrderedSubnodeList osl = (OrderedSubnodeList) views.get(key);
116: if (osl == null) {
117: osl = new OrderedSubnodeList(nmgr, rel, this , order);
118: views.put(key, osl);
119: }
120: return osl;
121: }
122:
123: protected void addToViews(Object obj) {
124: if (views == null || views.isEmpty())
125: return;
126: for (Iterator i = views.values().iterator(); i.hasNext();) {
127: OrderedSubnodeList osl = (OrderedSubnodeList) i.next();
128: osl.sortIn(obj);
129: }
130: }
131:
132: }
|