001: package net.sourceforge.orbroker;
002:
003: import java.util.Collection;
004: import java.util.Iterator;
005:
006: /*
007: * Created on Apr 20, 2004
008: *
009: */
010:
011: /**
012: * @author Nils Kilden-Pedersen
013: */
014: abstract class LazyCollection implements Collection {
015:
016: private final Broker broker;
017: private final Collection collectionInstance;
018: private final ConnectionContext context;
019: private boolean awake = false;
020: private final String statementID;
021:
022: protected LazyCollection(Broker broker, String statementID,
023: ConnectionContext contextParms, Collection collection) {
024: this .broker = broker;
025: this .context = contextParms;
026: this .statementID = statementID;
027: this .collectionInstance = collection;
028: }
029:
030: /**
031: * Return the collection instance after checking that it has
032: * been awake. Any {@link Collection} method implementations,
033: * or sub-interface implementations, <i>must</i>
034: * use this method for access.
035: * @return collection
036: */
037: protected Collection getCollection() {
038: wakeUp();
039: return this .collectionInstance;
040: }
041:
042: /**
043: * Make sure that the lazy collectionInstance is initialized.
044: */
045: protected synchronized void wakeUp() {
046: if (this .awake) {
047: return;
048: }
049:
050: Query qry = this .broker.startQuery();
051: qry.addContext(this .context);
052: try {
053: qry.selectMany(this .statementID, this .collectionInstance);
054: } finally {
055: /*
056: * Use regular close() instead of internalClose(), because
057: * lazy collections may be performed in a different thread
058: * than its owner.
059: */
060: qry.close();
061: }
062: this .awake = true;
063: }
064:
065: /**
066: * @see java.util.Collection#add(java.lang.Object)
067: */
068: public boolean add(Object object) {
069: return getCollection().add(object);
070: }
071:
072: /**
073: * @see java.util.Collection#addAll(java.util.Collection)
074: */
075: public boolean addAll(Collection collection) {
076: return getCollection().addAll(collection);
077: }
078:
079: /**
080: * @see java.util.Collection#clear()
081: */
082: public void clear() {
083: getCollection().clear();
084: }
085:
086: /**
087: * @see java.util.Collection#contains(java.lang.Object)
088: */
089: public boolean contains(Object object) {
090: return getCollection().contains(object);
091: }
092:
093: /**
094: * @see java.util.Collection#containsAll(java.util.Collection)
095: */
096: public boolean containsAll(Collection collection) {
097: return getCollection().containsAll(collection);
098: }
099:
100: /**
101: * @see java.util.Collection#isEmpty()
102: */
103: public boolean isEmpty() {
104: return getCollection().isEmpty();
105: }
106:
107: /**
108: * @see java.util.Collection#iterator()
109: */
110: public Iterator iterator() {
111: return getCollection().iterator();
112: }
113:
114: /**
115: * @see java.util.Collection#remove(java.lang.Object)
116: */
117: public boolean remove(Object object) {
118: return getCollection().remove(object);
119: }
120:
121: /**
122: * @see java.util.Collection#removeAll(java.util.Collection)
123: */
124: public boolean removeAll(Collection collection) {
125: return getCollection().removeAll(collection);
126: }
127:
128: /**
129: * @see java.util.Collection#retainAll(java.util.Collection)
130: */
131: public boolean retainAll(Collection collection) {
132: return getCollection().retainAll(collection);
133: }
134:
135: /**
136: * @see java.util.Collection#size()
137: */
138: public int size() {
139: return getCollection().size();
140: }
141:
142: /**
143: * @see java.util.Collection#toArray()
144: */
145: public Object[] toArray() {
146: return getCollection().toArray();
147: }
148:
149: /**
150: * @see java.util.Collection#toArray(java.lang.Object[])
151: */
152: public Object[] toArray(Object[] array) {
153: return getCollection().toArray(array);
154: }
155:
156: }
|