001: package com.sun.xml.xsom.util;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.NoSuchElementException;
008:
009: /**
010: * {@link Collection} that returns the view of objects which are actually fetched
011: * lazily from an {@link Iterator}.
012: *
013: * @author Kohsuke Kawaguchi
014: */
015: public class DeferedCollection<T> implements Collection<T> {
016: /**
017: * The iterator that lazily evaluates SCD query.
018: */
019: private final Iterator<T> result;
020:
021: /**
022: * Stores values that are already fetched from {@link #result}.
023: */
024: private final List<T> archive = new ArrayList<T>();
025:
026: public DeferedCollection(Iterator<T> result) {
027: this .result = result;
028: }
029:
030: public boolean isEmpty() {
031: if (archive.isEmpty())
032: fetch();
033: return archive.isEmpty();
034: }
035:
036: public int size() {
037: fetchAll();
038: return archive.size();
039: }
040:
041: public boolean contains(Object o) {
042: if (archive.contains(o))
043: return true;
044: while (result.hasNext()) {
045: T value = result.next();
046: archive.add(value);
047: if (value.equals(o))
048: return true;
049: }
050: return false;
051: }
052:
053: public boolean containsAll(Collection<?> c) {
054: for (Object o : c) {
055: if (!contains(o))
056: return false;
057: }
058: return true;
059: }
060:
061: public Iterator<T> iterator() {
062: return new Iterator<T>() {
063: int idx = 0;
064:
065: public boolean hasNext() {
066: if (idx < archive.size())
067: return true;
068: return result.hasNext();
069: }
070:
071: public T next() {
072: if (idx == archive.size())
073: fetch();
074: if (idx == archive.size())
075: throw new NoSuchElementException();
076: return archive.get(idx++);
077: }
078:
079: public void remove() {
080: // TODO
081: }
082: };
083: }
084:
085: public Object[] toArray() {
086: fetchAll();
087: return archive.toArray();
088: }
089:
090: public <T> T[] toArray(T[] a) {
091: fetchAll();
092: return archive.toArray(a);
093: }
094:
095: private void fetchAll() {
096: while (result.hasNext())
097: archive.add(result.next());
098: }
099:
100: /**
101: * Fetches another item from {@link
102: */
103: private void fetch() {
104: if (result.hasNext())
105: archive.add(result.next());
106: }
107:
108: // mutation methods are unsupported
109: public boolean add(T o) {
110: throw new UnsupportedOperationException();
111: }
112:
113: public boolean remove(Object o) {
114: throw new UnsupportedOperationException();
115: }
116:
117: public boolean addAll(Collection<? extends T> c) {
118: throw new UnsupportedOperationException();
119: }
120:
121: public boolean removeAll(Collection<?> c) {
122: throw new UnsupportedOperationException();
123: }
124:
125: public boolean retainAll(Collection<?> c) {
126: throw new UnsupportedOperationException();
127: }
128:
129: public void clear() {
130: throw new UnsupportedOperationException();
131: }
132: }
|