Source for javax.naming.directory.BasicAttribute

   1: /* BasicAttribute.java --
   2:    Copyright (C) 2000, 2001, 2004  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: import javax.naming.OperationNotSupportedException;
  47: 
  48: /**
  49:  * @author Tom Tromey (tromey@redhat.com)
  50:  * @date June 20, 2001
  51:  */
  52: public class BasicAttribute implements Attribute
  53: {
  54:   private static final long serialVersionUID = 6743528196119291326L;
  55:   
  56:   /** The ID of this attribute.  */
  57:   protected String attrID;
  58:   /** True if this attribute's values are ordered.  */
  59:   protected boolean ordered;
  60:   /** Values for this attribute.  */
  61:   protected transient Vector values;
  62: 
  63:   // Used by cloning.
  64:   private BasicAttribute ()
  65:   {
  66:   }
  67: 
  68:   public BasicAttribute (String id)
  69:   {
  70:     this (id, false);
  71:   }
  72: 
  73:   public BasicAttribute (String id, boolean ordered)
  74:   {
  75:     attrID = id;
  76:     this.ordered = ordered;
  77:     values = new Vector ();
  78:   }
  79: 
  80:   public BasicAttribute (String id, Object value)
  81:   {
  82:     this (id, value, false);
  83:   }
  84: 
  85:   public BasicAttribute (String id, Object value, boolean ordered)
  86:   {
  87:     attrID = id;
  88:     this.ordered = ordered;
  89:     values = new Vector ();
  90:     values.add (value);
  91:   }
  92: 
  93:   public void add (int index, Object val)
  94:   {
  95:     if (! ordered && contains (val))
  96:       throw new IllegalStateException ("value already in attribute");
  97:     values.add (index, val);
  98:   }
  99: 
 100:   public boolean add (Object val)
 101:   {
 102:     if (! ordered && contains (val))
 103:       throw new IllegalStateException ("value already in attribute");
 104:     return values.add (val);
 105:   }
 106: 
 107:   public void clear ()
 108:   {
 109:     values.clear ();
 110:   }
 111: 
 112:   public Object clone ()
 113:   {
 114:     BasicAttribute c = new BasicAttribute ();
 115:     c.attrID = attrID;
 116:     c.ordered = ordered;
 117:     c.values = (Vector) values.clone ();
 118:     return c;
 119:   }
 120: 
 121:   public boolean contains (Object val)
 122:   {
 123:     for (int i = 0; i < values.size (); ++i)
 124:       {
 125:     if (equals (val, values.get (i)))
 126:       return true;
 127:       }
 128: 
 129:     return false;
 130:   }
 131: 
 132:   public boolean equals (Object obj)
 133:   {
 134:     if (! (obj instanceof BasicAttribute))
 135:       return false;
 136:     BasicAttribute b = (BasicAttribute) obj;
 137: 
 138:     if (ordered != b.ordered
 139:     || ! attrID.equals (b.attrID)
 140:     || values.size () != b.values.size ())
 141:       return false;
 142: 
 143:     for (int i = 0; i < values.size (); ++i)
 144:       {
 145:     boolean ok = false;
 146:     if (ordered)
 147:       ok = equals (values.get (i), b.values.get (i));
 148:     else
 149:       {
 150:         for (int j = 0; j < b.values.size (); ++j)
 151:           {
 152:         if (equals (values.get (i), b.values.get (j)))
 153:           {
 154:             ok = true;
 155:             break;
 156:           }
 157:           }
 158:       }
 159: 
 160:     if (! ok)
 161:       return false;
 162:       }
 163: 
 164:     return true;
 165:   }
 166: 
 167:   public Object get ()
 168:     throws NamingException
 169:   {
 170:     if (values.size () == 0)
 171:       throw new NoSuchElementException ("no values");
 172:     return get (0);
 173:   }
 174: 
 175:   public Object get (int index)
 176:     throws NamingException
 177:   {
 178:     return values.get (index);
 179:   }
 180: 
 181:   public NamingEnumeration getAll ()
 182:     throws NamingException
 183:   {
 184:     return new BasicAttributeEnumeration ();
 185:   }
 186: 
 187:   public DirContext getAttributeDefinition ()
 188:     throws OperationNotSupportedException, NamingException
 189:   {
 190:     throw new OperationNotSupportedException ();
 191:   }
 192: 
 193:   public DirContext getAttributeSyntaxDefinition ()
 194:     throws OperationNotSupportedException, NamingException
 195:   {
 196:     throw new OperationNotSupportedException ();
 197:   }
 198: 
 199:   public String getID ()
 200:   {
 201:     return attrID;
 202:   }
 203: 
 204:   public int hashCode ()
 205:   {
 206:     int val = attrID.hashCode ();
 207:     for (int i = 0; i < values.size (); ++i)
 208:       {
 209:     Object o = values.get (i);
 210:     if (o == null)
 211:       {
 212:         // Nothing.
 213:       }
 214:     else if (o instanceof Object[])
 215:       {
 216:         Object[] a = (Object[]) o;
 217:         for (int j = 0; j < a.length; ++j)
 218:           val += a[j].hashCode ();
 219:       }
 220:     else
 221:       val += o.hashCode ();
 222:       }
 223: 
 224:     return val;
 225:   }
 226: 
 227:   public boolean isOrdered ()
 228:   {
 229:     return ordered;
 230:   }
 231: 
 232:   public Object remove (int index)
 233:   {
 234:     return values.remove (index);
 235:   }
 236: 
 237:   public boolean remove (Object val)
 238:   {
 239:     for (int i = 0; i < values.size (); ++i)
 240:       {
 241:     if (equals (val, values.get (i)))
 242:       {
 243:         values.remove (i);
 244:         return true;
 245:       }
 246:       }
 247: 
 248:     return false;
 249:   }
 250: 
 251:   public Object set (int index, Object val)
 252:   {
 253:     if (! ordered && contains (val))
 254:       throw new IllegalStateException ("value already in attribute");
 255:     return values.set (index, val);
 256:   }
 257: 
 258:   public int size ()
 259:   {
 260:     return values.size ();
 261:   }
 262: 
 263:   public String toString ()
 264:   {
 265:     String r = attrID;
 266:     for (int i = 0; i < values.size (); ++i)
 267:       r += ";" + values.get (i).toString ();
 268:     return r;
 269:   }
 270: 
 271:   // This is used for testing equality of two Objects according to our
 272:   // local rules.
 273:   private boolean equals (Object one, Object two)
 274:   {
 275:     if (one == null)
 276:       return two == null;
 277: 
 278:     if (one instanceof Object[])
 279:       {
 280:     if (! (two instanceof Object[]))
 281:       return false;
 282: 
 283:     Object[] aone = (Object[]) one;
 284:     Object[] atwo = (Object[]) two;
 285: 
 286:     if (aone.length != atwo.length)
 287:       return false;
 288: 
 289:     for (int i = 0; i < aone.length; ++i)
 290:       {
 291:         if (! aone[i].equals (atwo[i]))
 292:           return false;
 293:       }
 294: 
 295:     return true;
 296:       }
 297: 
 298:     return one.equals (two);
 299:   }
 300: 
 301:   // Used when enumerating this attribute.
 302:   private class BasicAttributeEnumeration implements NamingEnumeration
 303:   {
 304:     int where = -1;
 305: 
 306:     public BasicAttributeEnumeration ()
 307:     {
 308:     }
 309: 
 310:     public void close () throws NamingException
 311:     {
 312:     }
 313: 
 314:     public boolean hasMore () throws NamingException
 315:     {
 316:       return hasMoreElements ();
 317:     }
 318: 
 319:     public Object next () throws NamingException
 320:     {
 321:       return nextElement ();
 322:     }
 323: 
 324:     public boolean hasMoreElements ()
 325:     {
 326:       return where < values.size ();
 327:     }
 328: 
 329:     public Object nextElement () throws NoSuchElementException
 330:     {
 331:       if (where + 1 >= values.size ())
 332:     throw new NoSuchElementException ("no more elements");
 333:       ++where;
 334:       return values.get (where);
 335:     }
 336:   }
 337: }