Source for javax.naming.directory.BasicAttributes

   1: /* BasicAttributes.java --
   2:    Copyright (C) 2000, 2001, 2004, 2005  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.util.NoSuchElementException;
  42: import java.util.Vector;
  43: 
  44: import javax.naming.NamingEnumeration;
  45: import javax.naming.NamingException;
  46: 
  47: /**
  48:  * @author Tom Tromey (tromey@redhat.com)
  49:  * @date June 22, 2001
  50:  */
  51: public class BasicAttributes implements Attributes
  52: {
  53:   private static final long serialVersionUID = 4980164073184639448L;
  54:   
  55:   public BasicAttributes ()
  56:   {
  57:     this (false);
  58:   }
  59: 
  60:   public BasicAttributes (boolean ignoreCase)
  61:   {
  62:     this.ignoreCase = ignoreCase;
  63:     this.attributes = new Vector ();
  64:   }
  65: 
  66:   public BasicAttributes (String attrID, Object val)
  67:   {
  68:     this (attrID, val, false);
  69:   }
  70: 
  71:   public BasicAttributes (String attrID, Object val, boolean ignoreCase)
  72:   {
  73:     this.ignoreCase = ignoreCase;
  74:     attributes = new Vector ();
  75:     attributes.add (new BasicAttribute (attrID, val));
  76:   }
  77: 
  78:   public Object clone ()
  79:   {
  80:     // Slightly inefficient as we make a garbage Vector here.
  81:     BasicAttributes ba = new BasicAttributes (ignoreCase);
  82:     ba.attributes = (Vector) attributes.clone ();
  83:     return ba;
  84:   }
  85: 
  86:   /**
  87:    * Returns true if and only if the given Object is an instance of
  88:    * Attributes, the given attributes both do or don't ignore case for
  89:    * IDs and the collection of attributes is the same.
  90:    */
  91:   public boolean equals (Object obj)
  92:   {
  93:     if (! (obj instanceof Attributes))
  94:       return false;
  95: 
  96:     Attributes bs = (Attributes) obj;
  97:     if (ignoreCase != bs.isCaseIgnored()
  98:     || attributes.size () != bs.size ())
  99:       return false;
 100: 
 101:     NamingEnumeration bas = bs.getAll();
 102:     while (bas.hasMoreElements())
 103:       {
 104:     Attribute a = (Attribute) bas.nextElement();
 105:     Attribute b = get(a.getID ());
 106:     if (! a.equals(b))
 107:       return false;
 108:       }
 109: 
 110:     return true;
 111:   }
 112: 
 113:   public Attribute get (String attrID)
 114:   {
 115:     for (int i = 0; i < attributes.size (); ++i)
 116:       {
 117:     Attribute at = (Attribute) attributes.get (i);
 118:     if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
 119:         || (! ignoreCase && attrID.equals (at.getID ())))
 120:       return at;
 121:       }
 122: 
 123:     return null;
 124:   }
 125: 
 126:   public NamingEnumeration getAll ()
 127:   {
 128:     return new BasicAttributesEnumeration (false);
 129:   }
 130: 
 131:   public NamingEnumeration getIDs ()
 132:   {
 133:     return new BasicAttributesEnumeration (true);
 134:   }
 135: 
 136:   public int hashCode ()
 137:   {
 138:     int val = 0;
 139:     for (int i = 0; i < attributes.size (); ++i)
 140:       val += attributes.get (i).hashCode ();
 141:     return val;
 142:   }
 143: 
 144:   public boolean isCaseIgnored ()
 145:   {
 146:     return ignoreCase;
 147:   }
 148: 
 149:   public Attribute put (Attribute attr)
 150:   {
 151:     Attribute r = remove (attr.getID ());
 152:     attributes.add (attr);
 153:     return r;
 154:   }
 155: 
 156:   public Attribute put (String attrID, Object val)
 157:   {
 158:     return put (new BasicAttribute (attrID, val));
 159:   }
 160: 
 161:   public Attribute remove (String attrID)
 162:   {
 163:     for (int i = 0; i < attributes.size (); ++i)
 164:       {
 165:     Attribute at = (Attribute) attributes.get (i);
 166:     if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
 167:         || (! ignoreCase && attrID.equals (at.getID ())))
 168:       {
 169:         attributes.remove (i);
 170:         return at;
 171:       }
 172:       }
 173: 
 174:     return null;
 175:   }
 176: 
 177:   public int size ()
 178:   {
 179:     return attributes.size ();
 180:   }
 181: 
 182:   public String toString ()
 183:   {
 184:     String r = "";
 185:     for (int i = 0; i < attributes.size (); ++i)
 186:       {
 187:     if (i > 0)
 188:       r += "; ";
 189:     r += attributes.get (i).toString ();
 190:       }
 191:     return r;
 192:   }
 193: 
 194:   // This is set by the serialization spec.
 195:   private boolean ignoreCase;
 196:   // Package-private to avoid a trampoline.
 197:   transient Vector attributes;
 198: 
 199:   // Used when enumerating.
 200:   private class BasicAttributesEnumeration implements NamingEnumeration
 201:   {
 202:     int where = 0;
 203:     boolean id;
 204: 
 205:     public BasicAttributesEnumeration (boolean id)
 206:     {
 207:       this.id = id;
 208:     }
 209: 
 210:     public void close () throws NamingException
 211:     {
 212:     }
 213: 
 214:     public boolean hasMore () throws NamingException
 215:     {
 216:       return hasMoreElements ();
 217:     }
 218: 
 219:     public Object next () throws NamingException
 220:     {
 221:       return nextElement ();
 222:     }
 223: 
 224:     public boolean hasMoreElements ()
 225:     {
 226:       return where < attributes.size ();
 227:     }
 228: 
 229:     public Object nextElement () throws NoSuchElementException
 230:     {
 231:       if (where >= attributes.size ())
 232:     throw new NoSuchElementException ("no more elements");
 233:       Attribute at = (Attribute) attributes.get (where);
 234:       ++where;
 235:       return id ? (Object) at.getID () : (Object) at;
 236:     }
 237:   }
 238: }