Source for javax.naming.Reference

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