1 9 package org.jboss.portal.common; 10 11 import java.io.IOException ; 12 import java.io.ObjectInputStream ; 13 import java.io.ObjectOutputStream ; 14 import java.io.Serializable ; 15 16 22 public class FQN implements Serializable 23 { 24 25 private static final String [] EMPTY_ARRAY = new String [0]; 26 27 28 protected String [] names; 30 31 private int hashCode; 32 33 public FQN() 34 { 35 names = EMPTY_ARRAY; 36 hashCode = 0; 37 } 38 39 public FQN(String name) throws IllegalArgumentException 40 { 41 if (name == null) 42 { 43 throw new IllegalArgumentException ("Name cannot be null"); 44 } 45 names = new String []{name}; 46 hashCode = 0; 47 } 48 49 public FQN(String [] names) throws IllegalArgumentException 50 { 51 if (names == null) 52 { 53 throw new IllegalArgumentException ("Names array must not be null"); 54 } 55 this.names = new String [names.length]; 56 this.hashCode = 0; 57 for (int i = 0;i < names.length;i++) 58 { 59 String name = names[i]; 60 if (name == null) 61 { 62 throw new IllegalArgumentException ("One of the names is null"); 63 } 64 this.names[i] = name; 65 } 66 } 67 68 public FQN(FQN base, String name) throws IllegalArgumentException 69 { 70 if (base == null) 71 { 72 throw new IllegalArgumentException ("Base cannot be null"); 73 } 74 if (name == null) 75 { 76 throw new IllegalArgumentException ("Name cannot be null"); 77 } 78 names = new String [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 [] relative) throws IllegalArgumentException 85 { 86 if (base == null) 87 { 88 throw new IllegalArgumentException ("Base cannot be null"); 89 } 90 if (relative == null) 91 { 92 throw new IllegalArgumentException ("Relative cannot be null"); 93 } 94 names = new String [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 101 { 102 if (base == null) 103 { 104 throw new IllegalArgumentException ("Base cannot be null"); 105 } 106 if (relative == null) 107 { 108 throw new IllegalArgumentException ("Relative cannot be null"); 109 } 110 names = new String [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 getName(int index) throws ArrayIndexOutOfBoundsException 122 { 123 return names[index]; 124 } 125 126 public String [] toArray() 127 { 128 String [] copy = new String [names.length]; 129 System.arraycopy(names, 0, copy, 0, copy.length); 130 return copy; 131 } 132 133 public boolean isChildOf(FQN parent) throws IllegalArgumentException 134 { 135 if (parent == null) 136 { 137 throw new IllegalArgumentException ("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 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 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 toString() 198 { 199 StringBuffer buffer = new StringBuffer (); 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 out) throws IOException 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 in) throws IOException , ClassNotFoundException 217 { 218 names = new String [in.readInt()]; 219 for (int i = 0; i < names.length; i++) 220 { 221 names[i] = (String )in.readObject(); 222 } 223 } 224 } 225 | Popular Tags |