1 16 17 package de.gulden.util.javasource; 18 19 import java.lang.Class ; 20 import java.io.*; 21 import java.util.*; 22 23 30 public class NamedIterator implements Serializable { 31 32 38 public Vector data; 39 40 43 protected Hashtable hash; 44 45 48 protected int pos; 49 50 53 protected boolean readonly=false; 54 55 56 62 public NamedIterator() { 63 data=new Vector(); 64 hash=new Hashtable(); 65 reset(); 66 } 67 68 73 public NamedIterator(Vector v) { 74 data=v; hash=new Hashtable(); 76 for (Enumeration e=v.elements();e.hasMoreElements();) { 77 Named n=(Named)e.nextElement(); 78 hash.put(n.getName(),n); 79 } 80 reset(); 81 } 82 83 86 NamedIterator(Vector v, Object type, int modifierMask) { 87 this(); 92 for (Enumeration e=v.elements();e.hasMoreElements();) { 93 Member m=(Member)e.nextElement(); 94 int mod=m.getModifier(); 95 if ( 96 ((modifierMask&mod)!=0) 97 &&(m.getClass().isAssignableFrom((java.lang.Class )type)) 98 ) { 99 data.addElement(m); 100 } 101 } 102 reset(); 103 lockReadonly(); } 105 106 109 private NamedIterator(boolean dummy) { 110 } 112 113 114 public Object clone() { 118 NamedIterator c=new NamedIterator(true); 119 synchronized (this) { 120 c.data=(Vector)data.clone(); 121 c.hash=(Hashtable)hash.clone(); 122 c.pos=pos; 123 c.readonly=readonly; 124 } 125 return c; 126 } 127 128 133 public Named next() { 134 if (hasMore()) { 135 return (Named)data.elementAt(pos++); 136 } 137 else { 138 return null; 139 } 140 } 141 142 145 public boolean hasMore() { 146 return (pos<data.size()); 147 } 148 149 152 public Named find(String n) { 153 return (Named)hash.get(n); 154 } 155 156 159 public void reset() { 160 pos=0; 161 } 162 163 171 public boolean isReadOnly() { 172 return readonly; 173 } 174 175 178 public void addHere(Named n) { 179 if (!readonly) { 180 synchronized (this) { 181 data.insertElementAt(n,pos); 182 hash.put(n.getName(),n); 183 } 184 } 185 } 186 187 190 public void add(Named n) { 191 if (!readonly) { 192 synchronized (this) { 193 data.addElement(n); 194 hash.put(n.getName(),n); 195 } 196 } 197 } 198 199 202 public void remove(Named n) { 203 if (!readonly) { 204 synchronized (this) { 205 data.addElement(n); 206 hash.put(n.getName(),n); 207 } 208 } 209 } 210 211 214 public void removeAll() { 215 if (!readonly) { 216 synchronized (this) { 217 data.removeAllElements(); 218 hash.clear(); 219 } 220 } 221 } 222 223 226 public void add(NamedIterator it) { 227 it.reset(); 228 synchronized (this) { 229 while (it.hasMore()) { 230 data.addElement(it.next()); 231 } 232 } 233 } 234 235 238 public int size() { 239 return data.size(); 240 } 241 242 245 void lockReadonly() { 246 readonly=true; 247 } 248 249 252 Vector getVector() { 253 return (Vector)data.clone(); 254 } 255 256 } | Popular Tags |