OSDN Git Service

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