Source for javax.naming.directory.BasicAttributes

   1: /* BasicAttributes.java --
   2:    Copyright (C) 2000, 2001, 2004, 2005, 2006  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.naming.directory;
  40: 
  41: import java.io.IOException;
  42: import java.io.ObjectInputStream;
  43: import java.io.ObjectOutputStream;
  44: import java.util.NoSuchElementException;
  45: import java.util.Vector;
  46: 
  47: import javax.naming.NamingEnumeration;
  48: import javax.naming.NamingException;
  49: 
  50: /**
  51:  * @author Tom Tromey (tromey@redhat.com)
  52:  * @date June 22, 2001
  53:  */
  54: public class BasicAttributes implements Attributes
  55: {
  56:   private static final long serialVersionUID = 4980164073184639448L;
  57:   
  58:   public BasicAttributes ()
  59:   {
  60:     this (false);
  61:   }
  62: 
  63:   public BasicAttributes (boolean ignoreCase)
  64:   {
  65:     this.ignoreCase = ignoreCase;
  66:     this.attributes = new Vector<Attribute>();
  67:   }
  68: 
  69:   public BasicAttributes (String attrID, Object val)
  70:   {
  71:     this (attrID, val, false);
  72:   }
  73: 
  74:   public BasicAttributes (String attrID, Object val, boolean ignoreCase)
  75:   {
  76:     this.ignoreCase = ignoreCase;
  77:     attributes = new Vector<Attribute>();
  78:     attributes.add (new BasicAttribute (attrID, val));
  79:   }
  80: 
  81:   public Object clone ()
  82:   {
  83:     // Slightly inefficient as we make a garbage Vector here.
  84:     BasicAttributes ba = new BasicAttributes (ignoreCase);
  85:     ba.attributes = (Vector<Attribute>) attributes.clone ();
  86:     return ba;
  87:   }
  88: 
  89:   /**
  90:    * Returns true if and only if the given Object is an instance of
  91:    * Attributes, the given attributes both do or don't ignore case for
  92:    * IDs and the collection of attributes is the same.
  93:    */
  94:   public boolean equals (Object obj)
  95:   {
  96:     if (! (obj instanceof Attributes))
  97:       return false;
  98: 
  99:     Attributes bs = (Attributes) obj;
 100:     if (ignoreCase != bs.isCaseIgnored()
 101:     || attributes.size () != bs.size ())
 102:       return false;
 103: 
 104:     NamingEnumeration bas = bs.getAll();
 105:     while (bas.hasMoreElements())
 106:       {
 107:     Attribute a = (Attribute) bas.nextElement();
 108:     Attribute b = get(a.getID ());
 109:     if (! a.equals(b))
 110:       return false;
 111:       }
 112: 
 113:     return true;
 114:   }
 115: 
 116:   public Attribute get (String attrID)
 117:   {
 118:     for (int i = 0; i < attributes.size (); ++i)
 119:       {
 120:     Attribute at = attributes.get (i);
 121:     if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
 122:         || (! ignoreCase && attrID.equals (at.getID ())))
 123:       return at;
 124:       }
 125: 
 126:     return null;
 127:   }
 128: 
 129:   public NamingEnumeration<Attribute> getAll ()
 130:   {
 131:     return new BasicAttributesEnumeration();
 132:   }
 133: 
 134:   public NamingEnumeration<String> getIDs ()
 135:   {
 136:     final NamingEnumeration<Attribute> attrs = getAll();
 137:     return new NamingEnumeration<String>() {
 138:       public boolean hasMore() throws NamingException
 139:       {
 140:         return attrs.hasMore();
 141:       }
 142:       
 143:       public boolean hasMoreElements()
 144:       {
 145:         return attrs.hasMoreElements();
 146:       }
 147: 
 148:       public String next() throws NamingException
 149:       {
 150:         return attrs.next().getID();
 151:       }
 152: 
 153:       public String nextElement()
 154:       {
 155:         return attrs.nextElement().getID();
 156:       }
 157: 
 158:       public void close() throws NamingException
 159:       {
 160:         attrs.close();
 161:       }
 162:     };
 163:   }
 164: 
 165:   public int hashCode ()
 166:   {
 167:     int val = 0;
 168:     for (int i = 0; i < attributes.size (); ++i)
 169:       val += attributes.get (i).hashCode ();
 170:     return val;
 171:   }
 172: 
 173:   public boolean isCaseIgnored ()
 174:   {
 175:     return ignoreCase;
 176:   }
 177: 
 178:   public Attribute put (Attribute attr)
 179:   {
 180:     Attribute r = remove (attr.getID ());
 181:     attributes.add (attr);
 182:     return r;
 183:   }
 184: 
 185:   public Attribute put (String attrID, Object val)
 186:   {
 187:     return put (new BasicAttribute (attrID, val));
 188:   }
 189: 
 190:   public Attribute remove (String attrID)
 191:   {
 192:     for (int i = 0; i < attributes.size (); ++i)
 193:       {
 194:     Attribute at = (Attribute) attributes.get (i);
 195:     if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
 196:         || (! ignoreCase && attrID.equals (at.getID ())))
 197:       {
 198:         attributes.remove (i);
 199:         return at;
 200:       }
 201:       }
 202: 
 203:     return null;
 204:   }
 205: 
 206:   public int size ()
 207:   {
 208:     return attributes.size ();
 209:   }
 210: 
 211:   public String toString ()
 212:   {
 213:     String r = "";
 214:     for (int i = 0; i < attributes.size (); ++i)
 215:       {
 216:     if (i > 0)
 217:       r += "; ";
 218:     r += attributes.get (i).toString ();
 219:       }
 220:     return r;
 221:   }
 222: 
 223:   // This is set by the serialization spec.
 224:   private boolean ignoreCase;
 225:   // Package-private to avoid a trampoline.
 226:   transient Vector<Attribute> attributes;
 227: 
 228:   private void readObject(ObjectInputStream s) throws IOException,
 229:       ClassNotFoundException
 230:   {
 231:     s.defaultReadObject();
 232:     int size = s.readInt();
 233:     attributes = new Vector<Attribute>(size);    
 234:     for (int i = 0; i < size; i++)
 235:       attributes.add((Attribute) s.readObject());
 236:   }
 237: 
 238:   private void writeObject(ObjectOutputStream s) throws IOException
 239:   {
 240:     s.defaultWriteObject();
 241:     s.writeInt(attributes.size());
 242:     for (int i = 0; i < attributes.size(); i++)
 243:       s.writeObject(attributes.get(i));
 244:   }
 245: 
 246:   // Used when enumerating.
 247:   private class BasicAttributesEnumeration
 248:     implements NamingEnumeration<Attribute>
 249:   {
 250:     int where = 0;
 251: 
 252:     public BasicAttributesEnumeration ()
 253:     {
 254:     }
 255: 
 256:     public void close () throws NamingException
 257:     {
 258:     }
 259: 
 260:     public boolean hasMore () throws NamingException
 261:     {
 262:       return hasMoreElements ();
 263:     }
 264: 
 265:     public Attribute next () throws NamingException
 266:     {
 267:       return nextElement ();
 268:     }
 269: 
 270:     public boolean hasMoreElements ()
 271:     {
 272:       return where < attributes.size ();
 273:     }
 274: 
 275:     public Attribute nextElement () throws NoSuchElementException
 276:     {
 277:       if (where >= attributes.size ())
 278:     throw new NoSuchElementException ("no more elements");
 279:       Attribute at = attributes.get (where);
 280:       ++where;
 281:       return at;
 282:     }
 283:   }
 284: }