KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > FQN


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.common;
10
11 import java.io.IOException JavaDoc;
12 import java.io.ObjectInputStream JavaDoc;
13 import java.io.ObjectOutputStream JavaDoc;
14 import java.io.Serializable JavaDoc;
15
16 /**
17  * Defines a reusable and generic full qualified name.
18  *
19  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
20  * @version $Revision: 1.5 $
21  */

22 public class FQN implements Serializable JavaDoc
23 {
24
25    private static final String JavaDoc[] EMPTY_ARRAY = new String JavaDoc[0];
26
27    /** The list of objects. */
28    protected String JavaDoc[] names; // julien : perhaps Serializable[] ?
29

30    /** The cached hashcode. */
31    private int hashCode;
32
33    public FQN()
34    {
35       names = EMPTY_ARRAY;
36       hashCode = 0;
37    }
38
39    public FQN(String JavaDoc name) throws IllegalArgumentException JavaDoc
40    {
41       if (name == null)
42       {
43          throw new IllegalArgumentException JavaDoc("Name cannot be null");
44       }
45       names = new String JavaDoc[]{name};
46       hashCode = 0;
47    }
48
49    public FQN(String JavaDoc[] names) throws IllegalArgumentException JavaDoc
50    {
51       if (names == null)
52       {
53          throw new IllegalArgumentException JavaDoc("Names array must not be null");
54       }
55       this.names = new String JavaDoc[names.length];
56       this.hashCode = 0;
57       for (int i = 0;i < names.length;i++)
58       {
59          String JavaDoc name = names[i];
60          if (name == null)
61          {
62             throw new IllegalArgumentException JavaDoc("One of the names is null");
63          }
64          this.names[i] = name;
65       }
66    }
67
68    public FQN(FQN base, String JavaDoc name) throws IllegalArgumentException JavaDoc
69    {
70       if (base == null)
71       {
72          throw new IllegalArgumentException JavaDoc("Base cannot be null");
73       }
74       if (name == null)
75       {
76          throw new IllegalArgumentException JavaDoc("Name cannot be null");
77       }
78       names = new String JavaDoc[base.names.length + 1];
79       hashCode = 0;
80       System.arraycopy(base.names, 0, names, 0, base.names.length);
81       names[base.names.length] = name;
82    }
83
84    public FQN(FQN base, String JavaDoc[] relative) throws IllegalArgumentException JavaDoc
85    {
86       if (base == null)
87       {
88          throw new IllegalArgumentException JavaDoc("Base cannot be null");
89       }
90       if (relative == null)
91       {
92          throw new IllegalArgumentException JavaDoc("Relative cannot be null");
93       }
94       names = new String JavaDoc[base.names.length + relative.length];
95       hashCode = 0;
96       System.arraycopy(base.names, 0, names, 0, base.names.length);
97       System.arraycopy(relative, 0, names, base.names.length, relative.length);
98    }
99
100    public FQN(FQN base, FQN relative) throws IllegalArgumentException JavaDoc
101    {
102       if (base == null)
103       {
104          throw new IllegalArgumentException JavaDoc("Base cannot be null");
105       }
106       if (relative == null)
107       {
108          throw new IllegalArgumentException JavaDoc("Relative cannot be null");
109       }
110       names = new String JavaDoc[base.names.length + relative.names.length];
111       hashCode = 0;
112       System.arraycopy(base.names, 0, names, 0, base.names.length);
113       System.arraycopy(relative.names, 0, names, base.names.length, relative.names.length);
114    }
115
116    public int size()
117    {
118       return names.length;
119    }
120
121    public String JavaDoc getName(int index) throws ArrayIndexOutOfBoundsException JavaDoc
122    {
123       return names[index];
124    }
125
126    public String JavaDoc[] toArray()
127    {
128       String JavaDoc[] copy = new String JavaDoc[names.length];
129       System.arraycopy(names, 0, copy, 0, copy.length);
130       return copy;
131    }
132
133    public boolean isChildOf(FQN parent) throws IllegalArgumentException JavaDoc
134    {
135       if (parent == null)
136       {
137          throw new IllegalArgumentException JavaDoc("Cannot compare to null");
138       }
139       if (names.length != parent.names.length + 1)
140       {
141          return false;
142       }
143       for (int i = 0;i < parent.names.length;i++)
144       {
145          Object JavaDoc name = parent.names[i];
146          if (!name.equals(names[i]))
147          {
148             return false;
149          }
150       }
151       return true;
152    }
153
154    public boolean equals(Object JavaDoc obj)
155    {
156       if (obj == this)
157       {
158          return true;
159       }
160       if (!(obj instanceof FQN))
161       {
162          return false;
163       }
164       FQN other = (FQN)obj;
165       if (other.names.length != names.length)
166       {
167          return false;
168       }
169       for (int i = 0;i < names.length;i++)
170       {
171          if (!names[i].equals(other.names[i]))
172          {
173             return false;
174          }
175       }
176       return true;
177    }
178
179    public int hashCode()
180    {
181       if (names.length == 0)
182       {
183          return 0;
184       }
185       if (hashCode == 0)
186       {
187          int tmp = 0;
188          for (int i = 0;i < names.length;i++)
189          {
190             tmp = tmp * 29 + names[i].hashCode();
191          }
192          hashCode = tmp;
193       }
194       return hashCode;
195    }
196
197    public String JavaDoc toString()
198    {
199       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
200       for (int i = 0;i < names.length;i++)
201       {
202          buffer.append('/').append(names[i]);
203       }
204       return buffer.toString();
205    }
206
207    private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc
208    {
209       out.writeInt(names.length);
210       for (int i = 0; i < names.length; i++)
211       {
212          out.writeObject(names[i]);
213       }
214    }
215
216    private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
217    {
218       names = new String JavaDoc[in.readInt()];
219       for (int i = 0; i < names.length; i++)
220       {
221          names[i] = (String JavaDoc)in.readObject();
222       }
223    }
224 }
225
Popular Tags