1 19 package org.netbeans.mdr.storagemodel; 20 21 import java.util.*; 22 import java.io.*; 23 import java.lang.reflect.Array ; 24 25 import javax.jmi.reflect.*; 26 27 import org.netbeans.mdr.handlers.BaseObjectHandler; 28 import org.netbeans.mdr.persistence.*; 29 import org.netbeans.mdr.util.*; 30 31 56 public class CompositeCollection implements Collection { 57 58 private final ArrayList innerCollections = new ArrayList(); 59 private volatile boolean canChange = true; 60 61 public CompositeCollection() { 62 } 63 64 67 public void addCollection(Collection collection) { 68 if (canChange) { 69 innerCollections.add(collection); 70 } else { 71 throw new DebugException("Set of collections cannot be changed after it was accessed."); 72 } 73 } 74 75 public int size() { 76 int size = 0; 77 canChange = false; 78 79 for (Iterator it = innerCollections.iterator(); it.hasNext();) { 80 size += ((Collection) it.next()).size(); 81 } 82 83 return size; 84 } 85 86 public boolean contains(Object obj) { 87 canChange = false; 88 89 for (Iterator it = innerCollections.iterator(); it.hasNext();) { 90 if (((Collection) it.next()).contains(obj)) return true; 91 } 92 93 return false; 94 } 95 96 public Iterator iterator() { 97 canChange = false; 98 ArrayList innerIterators = new ArrayList(); 99 100 for (Iterator it = innerCollections.iterator(); it.hasNext();) { 101 innerIterators.add(((Collection) it.next()).iterator()); 102 } 103 104 return new CompositeIterator(innerIterators.iterator()); 105 } 106 107 public boolean isEmpty() { 108 canChange = false; 109 110 for (Iterator it = innerCollections.iterator(); it.hasNext();) { 111 if (!((Collection) it.next()).isEmpty()) return false; 112 } 113 114 return true; 115 } 116 117 public boolean containsAll(Collection collection) { 118 for (Iterator it = collection.iterator(); it.hasNext();) { 119 if (!contains(it.next())) return false; 120 } 121 122 return true; 123 } 124 125 public Object [] toArray(Object [] obj) { 126 canChange = false; 127 ArrayList value = new ArrayList(); 128 129 for (Iterator it = innerCollections.iterator(); it.hasNext();) { 130 value.addAll((Collection) it.next()); 131 } 132 133 return value.toArray(obj); 134 } 135 136 public Object [] toArray() { 137 return toArray(new Object [0]); 138 } 139 140 143 public void clear() { 144 throw new UnsupportedOperationException (); 145 } 146 147 150 public boolean addAll(Collection collection) { 151 throw new UnsupportedOperationException (); 152 } 153 154 157 public boolean remove(Object obj) { 158 throw new UnsupportedOperationException (); 159 } 160 161 164 public boolean add(Object obj) { 165 throw new UnsupportedOperationException (); 166 } 167 168 171 public boolean retainAll(Collection collection) { 172 throw new UnsupportedOperationException (); 173 } 174 175 178 public boolean removeAll(Collection collection) { 179 throw new UnsupportedOperationException (); 180 } 181 182 183 184 185 186 protected class CompositeIterator implements Iterator { 187 private final Iterator innerIterators; 188 private Iterator currentIterator; 189 190 protected CompositeIterator(Iterator innerIterators) { 191 this.innerIterators = innerIterators; 192 this.currentIterator = (Iterator) innerIterators.next(); 193 hasNext(); 194 } 195 196 public boolean hasNext() { 197 while (innerIterators.hasNext()) { 198 if (currentIterator.hasNext()) { 199 return true; 200 } else { 201 currentIterator = (Iterator) innerIterators.next(); 202 } 203 } 204 return currentIterator.hasNext(); 205 } 206 207 public Object next() { 208 Object result = currentIterator.next(); 209 hasNext(); 210 return result; 211 } 212 213 216 public void remove() { 217 throw new UnsupportedOperationException (); 218 } 219 } 220 } 221 | Popular Tags |