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