1 7 8 9 package javax.naming.directory; 10 11 import java.util.Hashtable ; 12 import java.util.Enumeration ; 13 14 import javax.naming.NamingException ; 15 import javax.naming.NamingEnumeration ; 16 17 52 53 public class BasicAttributes implements Attributes { 54 58 private boolean ignoreCase = false; 59 60 transient Hashtable attrs = new Hashtable (11); 65 66 71 public BasicAttributes() { 72 } 73 74 83 public BasicAttributes(boolean ignoreCase) { 84 this.ignoreCase = ignoreCase; 85 } 86 87 97 public BasicAttributes(String attrID, Object val) { 98 this(); 99 this.put(new BasicAttribute (attrID, val)); 100 } 101 102 119 public BasicAttributes(String attrID, Object val, boolean ignoreCase) { 120 this(ignoreCase); 121 this.put(new BasicAttribute (attrID, val)); 122 } 123 124 public Object clone() { 125 BasicAttributes attrset; 126 try { 127 attrset = (BasicAttributes )super.clone(); 128 } catch (CloneNotSupportedException e) { 129 attrset = new BasicAttributes (ignoreCase); 130 } 131 attrset.attrs = (Hashtable )attrs.clone(); 132 return attrset; 133 } 134 135 public boolean isCaseIgnored() { 136 return ignoreCase; 137 } 138 139 public int size() { 140 return attrs.size(); 141 } 142 143 public Attribute get(String attrID) { 144 Attribute attr = (Attribute ) attrs.get( 145 ignoreCase ? attrID.toLowerCase() : attrID); 146 return (attr); 147 } 148 149 public NamingEnumeration <Attribute > getAll() { 150 return new AttrEnumImpl(); 151 } 152 153 public NamingEnumeration <String > getIDs() { 154 return new IDEnumImpl(); 155 } 156 157 public Attribute put(String attrID, Object val) { 158 return (Attribute )this.put(new BasicAttribute (attrID, val)); 159 } 160 161 public Attribute put(Attribute attr) { 162 String id = attr.getID(); 163 if (ignoreCase) { 164 id = id.toLowerCase(); 165 } 166 return (Attribute )attrs.put(id, attr); 167 } 168 169 public Attribute remove(String attrID) { 170 String id = (ignoreCase ? attrID.toLowerCase() : attrID); 171 return (Attribute )attrs.remove(id); 172 } 173 174 182 public String toString() { 183 if (attrs.size() == 0) { 184 return("No attributes"); 185 } else { 186 return attrs.toString(); 187 } 188 } 189 190 208 public boolean equals(Object obj) { 209 if ((obj != null) && (obj instanceof Attributes )) { 210 Attributes target = (Attributes )obj; 211 212 if (ignoreCase != target.isCaseIgnored()) { 214 return false; 215 } 216 217 if (size() == target.size()) { 218 Attribute their, mine; 219 try { 220 NamingEnumeration theirs = target.getAll(); 221 while (theirs.hasMore()) { 222 their = (Attribute )theirs.next(); 223 mine = get(their.getID()); 224 if (!their.equals(mine)) { 225 return false; 226 } 227 } 228 } catch (NamingException e) { 229 return false; 230 } 231 return true; 232 } 233 } 234 return false; 235 } 236 237 251 public int hashCode() { 252 int hash = (ignoreCase ? 1 : 0); 253 try { 254 NamingEnumeration all = getAll(); 255 while (all.hasMore()) { 256 hash += all.next().hashCode(); 257 } 258 } catch (NamingException e) {} 259 return hash; 260 } 261 262 268 private void writeObject(java.io.ObjectOutputStream s) 269 throws java.io.IOException { 270 s.defaultWriteObject(); s.writeInt(attrs.size()); 272 Enumeration attrEnum = attrs.elements(); 273 while (attrEnum.hasMoreElements()) { 274 s.writeObject(attrEnum.nextElement()); 275 } 276 } 277 278 281 private void readObject(java.io.ObjectInputStream s) 282 throws java.io.IOException , ClassNotFoundException { 283 s.defaultReadObject(); int n = s.readInt(); attrs = (n >= 1) 286 ? new Hashtable (n * 2) 287 : new Hashtable (2); while (--n >= 0) { 289 put((Attribute )s.readObject()); 290 } 291 } 292 293 294 class AttrEnumImpl implements NamingEnumeration <Attribute > { 295 296 Enumeration <Attribute > elements; 297 298 public AttrEnumImpl() { 299 this.elements = attrs.elements(); 300 } 301 302 public boolean hasMoreElements() { 303 return elements.hasMoreElements(); 304 } 305 306 public Attribute nextElement() { 307 return elements.nextElement(); 308 } 309 310 public boolean hasMore() throws NamingException { 311 return hasMoreElements(); 312 } 313 314 public Attribute next() throws NamingException { 315 return nextElement(); 316 } 317 318 public void close() throws NamingException { 319 elements = null; 320 } 321 } 322 323 class IDEnumImpl implements NamingEnumeration <String > { 324 325 Enumeration <Attribute > elements; 326 327 public IDEnumImpl() { 328 this.elements = attrs.elements(); 331 } 332 333 public boolean hasMoreElements() { 334 return elements.hasMoreElements(); 335 } 336 337 public String nextElement() { 338 Attribute attr = (Attribute )elements.nextElement(); 339 return attr.getID(); 340 } 341 342 public boolean hasMore() throws NamingException { 343 return hasMoreElements(); 344 } 345 346 public String next() throws NamingException { 347 return nextElement(); 348 } 349 350 public void close() throws NamingException { 351 elements = null; 352 } 353 } 354 355 358 private static final long serialVersionUID = 4980164073184639448L; 359 } 360 | Popular Tags |