Reference.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Reference.java --
  2. Copyright (C) 2000, 2001 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., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 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;
  32. import java.io.Serializable;
  33. import java.util.Enumeration;
  34. import java.util.Vector;
  35. /**
  36. * @author Tom Tromey <tromey@redhat.com>
  37. * @date May 16, 2001
  38. */
  39. public class Reference implements Cloneable, Serializable
  40. {
  41. public Reference (String className)
  42. {
  43. this.className = className;
  44. addrs = new Vector ();
  45. }
  46. public Reference (String className, RefAddr addr)
  47. {
  48. this.className = className;
  49. addrs = new Vector ();
  50. addrs.add (addr);
  51. }
  52. public Reference (String className, String factory, String factoryLocation)
  53. {
  54. this.className = className;
  55. this.classFactory = factory;
  56. this.classFactoryLocation = factoryLocation;
  57. addrs = new Vector ();
  58. }
  59. public Reference (String className, RefAddr addr,
  60. String factory, String factoryLocation)
  61. {
  62. this.className = className;
  63. this.classFactory = factory;
  64. this.classFactoryLocation = factoryLocation;
  65. addrs = new Vector ();
  66. addrs.add (addr);
  67. }
  68. public void add (int posn, RefAddr addr)
  69. {
  70. addrs.add (posn, addr);
  71. }
  72. public void add (RefAddr addr)
  73. {
  74. addrs.add (addr);
  75. }
  76. public void clear ()
  77. {
  78. addrs.clear ();
  79. }
  80. public Object clone ()
  81. {
  82. Reference r = new Reference (className, classFactory,
  83. classFactoryLocation);
  84. r.addrs = (Vector) addrs.clone ();
  85. return r;
  86. }
  87. // Convenience function.
  88. private boolean equals (String a, String b)
  89. {
  90. return (a == null) ? (b == null) : a.equals (b);
  91. }
  92. public boolean equals (Object obj)
  93. {
  94. if (! (obj instanceof Reference))
  95. return false;
  96. Reference r = (Reference) obj;
  97. return (equals (classFactory, r.classFactory)
  98. && equals (classFactoryLocation, r.classFactoryLocation)
  99. && equals (className, r.className)
  100. && addrs.equals (r.addrs));
  101. }
  102. public RefAddr get (int posn)
  103. {
  104. return (RefAddr) addrs.get (posn);
  105. }
  106. public RefAddr get (String addrType)
  107. {
  108. for (int i = 0; i < addrs.size (); ++i)
  109. {
  110. RefAddr r = (RefAddr) addrs.get (i);
  111. if (addrType.equals (r.getType ()))
  112. return r;
  113. }
  114. return null;
  115. }
  116. public Enumeration getAll ()
  117. {
  118. return addrs.elements ();
  119. }
  120. public String getClassName ()
  121. {
  122. return className;
  123. }
  124. public String getFactoryClassLocation ()
  125. {
  126. return classFactoryLocation;
  127. }
  128. public String getFactoryClassName ()
  129. {
  130. return classFactory;
  131. }
  132. public int hashCode ()
  133. {
  134. // The spec says the hash code is the sum of the hash codes of the
  135. // addresses. It does not mention the other fields.
  136. int h = 0;
  137. for (int i = 0; i < addrs.size (); ++i)
  138. h += addrs.get (i).hashCode ();
  139. return h;
  140. }
  141. public Object remove (int posn)
  142. {
  143. return addrs.remove (posn);
  144. }
  145. public int size ()
  146. {
  147. return addrs.size ();
  148. }
  149. public String toString ()
  150. {
  151. String x = getClass ().toString () + "[";
  152. for (int i = 0; i < addrs.size (); ++i)
  153. {
  154. if (i > 0)
  155. x += ",";
  156. x += addrs.get (i).toString ();
  157. }
  158. return x + "]";
  159. }
  160. protected Vector addrs;
  161. protected String classFactory;
  162. protected String classFactoryLocation;
  163. protected String className;
  164. }