OSDN Git Service

* javax/naming/AuthenticationException.java: Update copyright header.
[pf3gnuchains/gcc-fork.git] / libjava / javax / naming / directory / BasicAttributes.java
1 /* BasicAttributes.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 22, 2001
47  */
48 public class BasicAttributes implements Attributes
49 {
50   public BasicAttributes ()
51   {
52     this (false);
53   }
54
55   public BasicAttributes (boolean ignoreCase)
56   {
57     this.ignoreCase = ignoreCase;
58     this.attributes = new Vector ();
59   }
60
61   public BasicAttributes (String attrID, Object val)
62   {
63     this (attrID, val, false);
64   }
65
66   public BasicAttributes (String attrID, Object val, boolean ignoreCase)
67   {
68     this.ignoreCase = ignoreCase;
69     attributes = new Vector ();
70     attributes.add (new BasicAttribute (attrID, val));
71   }
72
73   public Object clone ()
74   {
75     // Slightly inefficient as we make a garbage Vector here.
76     BasicAttributes ba = new BasicAttributes (ignoreCase);
77     ba.attributes = (Vector) attributes.clone ();
78     return ba;
79   }
80
81   public boolean equals (Object obj)
82   {
83     if (! (obj instanceof BasicAttributes))
84       return false;
85     BasicAttributes b = (BasicAttributes) obj;
86     if (ignoreCase != b.ignoreCase
87         || attributes.size () != b.attributes.size ())
88       return false;
89
90     // Does order matter?
91     for (int i = 0; i < attributes.size (); ++i)
92       {
93         if (! attributes.get (i).equals (b.attributes.get (i)))
94           return false;
95       }
96
97     return true;
98   }
99
100   public Attribute get (String attrID)
101   {
102     for (int i = 0; i < attributes.size (); ++i)
103       {
104         Attribute at = (Attribute) attributes.get (i);
105         if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
106             || (! ignoreCase && attrID.equals (at.getID ())))
107           return at;
108       }
109
110     return null;
111   }
112
113   public NamingEnumeration getAll ()
114   {
115     return new BasicAttributesEnumeration (false);
116   }
117
118   public NamingEnumeration getIDs ()
119   {
120     return new BasicAttributesEnumeration (true);
121   }
122
123   public int hashCode ()
124   {
125     int val = 0;
126     for (int i = 0; i < attributes.size (); ++i)
127       val += attributes.get (i).hashCode ();
128     return val;
129   }
130
131   public boolean isCaseIgnored ()
132   {
133     return ignoreCase;
134   }
135
136   public Attribute put (Attribute attr)
137   {
138     Attribute r = remove (attr.getID ());
139     attributes.add (attr);
140     return r;
141   }
142
143   public Attribute put (String attrID, Object val)
144   {
145     return put (new BasicAttribute (attrID, val));
146   }
147
148   public Attribute remove (String attrID)
149   {
150     for (int i = 0; i < attributes.size (); ++i)
151       {
152         Attribute at = (Attribute) attributes.get (i);
153         if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
154             || (! ignoreCase && attrID.equals (at.getID ())))
155           {
156             attributes.remove (i);
157             return at;
158           }
159       }
160
161     return null;
162   }
163
164   public int size ()
165   {
166     return attributes.size ();
167   }
168
169   public String toString ()
170   {
171     String r = "";
172     for (int i = 0; i < attributes.size (); ++i)
173       {
174         if (i > 0)
175           r += "; ";
176         r += attributes.get (i).toString ();
177       }
178     return r;
179   }
180
181   // This is set by the serialization spec.
182   private boolean ignoreCase;
183   private transient Vector attributes;
184
185   // Used when enumerating.
186   private class BasicAttributesEnumeration implements NamingEnumeration
187   {
188     int where = -1;
189     boolean id;
190
191     public BasicAttributesEnumeration (boolean id)
192     {
193       this.id = id;
194     }
195
196     public void close () throws NamingException
197     {
198     }
199
200     public boolean hasMore () throws NamingException
201     {
202       return hasMoreElements ();
203     }
204
205     public Object next () throws NamingException
206     {
207       return nextElement ();
208     }
209
210     public boolean hasMoreElements ()
211     {
212       return where < attributes.size ();
213     }
214
215     public Object nextElement () throws NoSuchElementException
216     {
217       if (where + 1 >= attributes.size ())
218         throw new NoSuchElementException ("no more elements");
219       ++where;
220       Attribute at = (Attribute) attributes.get (where);
221       return id ? (Object) at.getID () : (Object) at;
222     }
223   }
224 }