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 ();
  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 ();
  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) 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 = (Attribute) 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 getAll ()
 130:   {
 131:     return new BasicAttributesEnumeration (false);
 132:   }
 133: 
 134:   public NamingEnumeration getIDs ()
 135:   {
 136:     return new BasicAttributesEnumeration (true);
 137:   }
 138: 
 139:   public int hashCode ()
 140:   {
 141:     int val = 0;
 142:     for (int i = 0; i < attributes.size (); ++i)
 143:       val += attributes.get (i).hashCode ();
 144:     return val;
 145:   }
 146: 
 147:   public boolean isCaseIgnored ()
 148:   {
 149:     return ignoreCase;
 150:   }
 151: 
 152:   public Attribute put (Attribute attr)
 153:   {
 154:     Attribute r = remove (attr.getID ());
 155:     attributes.add (attr);
 156:     return r;
 157:   }
 158: 
 159:   public Attribute put (String attrID, Object val)
 160:   {
 161:     return put (new BasicAttribute (attrID, val));
 162:   }
 163: 
 164:   public Attribute remove (String attrID)
 165:   {
 166:     for (int i = 0; i < attributes.size (); ++i)
 167:       {
 168:     Attribute at = (Attribute) attributes.get (i);
 169:     if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
 170:         || (! ignoreCase && attrID.equals (at.getID ())))
 171:       {
 172:         attributes.remove (i);
 173:         return at;
 174:       }
 175:       }
 176: 
 177:     return null;
 178:   }
 179: 
 180:   public int size ()
 181:   {
 182:     return attributes.size ();
 183:   }
 184: 
 185:   public String toString ()
 186:   {
 187:     String r = "";
 188:     for (int i = 0; i < attributes.size (); ++i)
 189:       {
 190:     if (i > 0)
 191:       r += "; ";
 192:     r += attributes.get (i).toString ();
 193:       }
 194:     return r;
 195:   }
 196: 
 197:   // This is set by the serialization spec.
 198:   private boolean ignoreCase;
 199:   // Package-private to avoid a trampoline.
 200:   transient Vector attributes;
 201: 
 202:   private void readObject(ObjectInputStream s) throws IOException,
 203:       ClassNotFoundException
 204:   {
 205:     s.defaultReadObject();
 206:     int size = s.readInt();
 207:     attributes = new Vector(size);    
 208:     for (int i = 0; i < size; i++)
 209:       attributes.add(s.readObject());
 210:   }
 211: 
 212:   private void writeObject(ObjectOutputStream s) throws IOException
 213:   {
 214:     s.defaultWriteObject();
 215:     s.writeInt(attributes.size());
 216:     for (int i = 0; i < attributes.size(); i++)
 217:       s.writeObject(attributes.get(i));
 218:   }
 219: 
 220:   // Used when enumerating.
 221:   private class BasicAttributesEnumeration implements NamingEnumeration
 222:   {
 223:     int where = 0;
 224:     boolean id;
 225: 
 226:     public BasicAttributesEnumeration (boolean id)
 227:     {
 228:       this.id = id;
 229:     }
 230: 
 231:     public void close () throws NamingException
 232:     {
 233:     }
 234: 
 235:     public boolean hasMore () throws NamingException
 236:     {
 237:       return hasMoreElements ();
 238:     }
 239: 
 240:     public Object next () throws NamingException
 241:     {
 242:       return nextElement ();
 243:     }
 244: 
 245:     public boolean hasMoreElements ()
 246:     {
 247:       return where < attributes.size ();
 248:     }
 249: 
 250:     public Object nextElement () throws NoSuchElementException
 251:     {
 252:       if (where >= attributes.size ())
 253:     throw new NoSuchElementException ("no more elements");
 254:       Attribute at = (Attribute) attributes.get (where);
 255:       ++where;
 256:       return id ? (Object) at.getID () : (Object) at;
 257:     }
 258:   }
 259: }