BasicAttributes.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* BasicAttributes.java --
  2. Copyright (C) 2000, 2001, 2004, 2005, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.naming.directory;
  32. import java.io.IOException;
  33. import java.io.ObjectInputStream;
  34. import java.io.ObjectOutputStream;
  35. import java.util.NoSuchElementException;
  36. import java.util.Vector;
  37. import javax.naming.NamingEnumeration;
  38. import javax.naming.NamingException;
  39. /**
  40. * @author Tom Tromey (tromey@redhat.com)
  41. * @date June 22, 2001
  42. */
  43. public class BasicAttributes implements Attributes
  44. {
  45. private static final long serialVersionUID = 4980164073184639448L;
  46. public BasicAttributes ()
  47. {
  48. this (false);
  49. }
  50. public BasicAttributes (boolean ignoreCase)
  51. {
  52. this.ignoreCase = ignoreCase;
  53. this.attributes = new Vector<Attribute>();
  54. }
  55. public BasicAttributes (String attrID, Object val)
  56. {
  57. this (attrID, val, false);
  58. }
  59. public BasicAttributes (String attrID, Object val, boolean ignoreCase)
  60. {
  61. this.ignoreCase = ignoreCase;
  62. attributes = new Vector<Attribute>();
  63. attributes.add (new BasicAttribute (attrID, val));
  64. }
  65. public Object clone ()
  66. {
  67. // Slightly inefficient as we make a garbage Vector here.
  68. BasicAttributes ba = new BasicAttributes (ignoreCase);
  69. ba.attributes = (Vector<Attribute>) attributes.clone ();
  70. return ba;
  71. }
  72. /**
  73. * Returns true if and only if the given Object is an instance of
  74. * Attributes, the given attributes both do or don't ignore case for
  75. * IDs and the collection of attributes is the same.
  76. */
  77. public boolean equals (Object obj)
  78. {
  79. if (! (obj instanceof Attributes))
  80. return false;
  81. Attributes bs = (Attributes) obj;
  82. if (ignoreCase != bs.isCaseIgnored()
  83. || attributes.size () != bs.size ())
  84. return false;
  85. NamingEnumeration bas = bs.getAll();
  86. while (bas.hasMoreElements())
  87. {
  88. Attribute a = (Attribute) bas.nextElement();
  89. Attribute b = get(a.getID ());
  90. if (! a.equals(b))
  91. return false;
  92. }
  93. return true;
  94. }
  95. public Attribute get (String attrID)
  96. {
  97. for (int i = 0; i < attributes.size (); ++i)
  98. {
  99. Attribute at = attributes.get (i);
  100. if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
  101. || (! ignoreCase && attrID.equals (at.getID ())))
  102. return at;
  103. }
  104. return null;
  105. }
  106. public NamingEnumeration<Attribute> getAll ()
  107. {
  108. return new BasicAttributesEnumeration();
  109. }
  110. public NamingEnumeration<String> getIDs ()
  111. {
  112. final NamingEnumeration<Attribute> attrs = getAll();
  113. return new NamingEnumeration<String>() {
  114. public boolean hasMore() throws NamingException
  115. {
  116. return attrs.hasMore();
  117. }
  118. public boolean hasMoreElements()
  119. {
  120. return attrs.hasMoreElements();
  121. }
  122. public String next() throws NamingException
  123. {
  124. return attrs.next().getID();
  125. }
  126. public String nextElement()
  127. {
  128. return attrs.nextElement().getID();
  129. }
  130. public void close() throws NamingException
  131. {
  132. attrs.close();
  133. }
  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. public boolean isCaseIgnored ()
  144. {
  145. return ignoreCase;
  146. }
  147. public Attribute put (Attribute attr)
  148. {
  149. Attribute r = remove (attr.getID ());
  150. attributes.add (attr);
  151. return r;
  152. }
  153. public Attribute put (String attrID, Object val)
  154. {
  155. return put (new BasicAttribute (attrID, val));
  156. }
  157. public Attribute remove (String attrID)
  158. {
  159. for (int i = 0; i < attributes.size (); ++i)
  160. {
  161. Attribute at = (Attribute) attributes.get (i);
  162. if ((ignoreCase && attrID.equalsIgnoreCase (at.getID ()))
  163. || (! ignoreCase && attrID.equals (at.getID ())))
  164. {
  165. attributes.remove (i);
  166. return at;
  167. }
  168. }
  169. return null;
  170. }
  171. public int size ()
  172. {
  173. return attributes.size ();
  174. }
  175. public String toString ()
  176. {
  177. String r = "";
  178. for (int i = 0; i < attributes.size (); ++i)
  179. {
  180. if (i > 0)
  181. r += "; ";
  182. r += attributes.get (i).toString ();
  183. }
  184. return r;
  185. }
  186. // This is set by the serialization spec.
  187. private boolean ignoreCase;
  188. // Package-private to avoid a trampoline.
  189. transient Vector<Attribute> attributes;
  190. private void readObject(ObjectInputStream s) throws IOException,
  191. ClassNotFoundException
  192. {
  193. s.defaultReadObject();
  194. int size = s.readInt();
  195. attributes = new Vector<Attribute>(size);
  196. for (int i = 0; i < size; i++)
  197. attributes.add((Attribute) s.readObject());
  198. }
  199. private void writeObject(ObjectOutputStream s) throws IOException
  200. {
  201. s.defaultWriteObject();
  202. s.writeInt(attributes.size());
  203. for (int i = 0; i < attributes.size(); i++)
  204. s.writeObject(attributes.get(i));
  205. }
  206. // Used when enumerating.
  207. private class BasicAttributesEnumeration
  208. implements NamingEnumeration<Attribute>
  209. {
  210. int where = 0;
  211. public BasicAttributesEnumeration ()
  212. {
  213. }
  214. public void close () throws NamingException
  215. {
  216. }
  217. public boolean hasMore () throws NamingException
  218. {
  219. return hasMoreElements ();
  220. }
  221. public Attribute next () throws NamingException
  222. {
  223. return nextElement ();
  224. }
  225. public boolean hasMoreElements ()
  226. {
  227. return where < attributes.size ();
  228. }
  229. public Attribute nextElement () throws NoSuchElementException
  230. {
  231. if (where >= attributes.size ())
  232. throw new NoSuchElementException ("no more elements");
  233. Attribute at = attributes.get (where);
  234. ++where;
  235. return at;
  236. }
  237. }
  238. }