001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.migration.model22.other;
023:
024: import java.io.IOException;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027: import java.io.Serializable;
028:
029: /**
030: * Defines a reusable and generic full qualified name.
031: *
032: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
033: * @version $Revision: 8784 $
034: */
035: public class FQN implements Serializable {
036:
037: private static final String[] EMPTY_ARRAY = new String[0];
038:
039: /** The list of objects. */
040: protected String[] names; // julien : perhaps Serializable[] ?
041:
042: /** The cached hashcode. */
043: private int hashCode;
044:
045: public FQN() {
046: names = EMPTY_ARRAY;
047: hashCode = 0;
048: }
049:
050: public FQN(String name) throws IllegalArgumentException {
051: if (name == null) {
052: throw new IllegalArgumentException("Name cannot be null");
053: }
054: names = new String[] { name };
055: hashCode = 0;
056: }
057:
058: public FQN(String[] names) throws IllegalArgumentException {
059: if (names == null) {
060: throw new IllegalArgumentException(
061: "Names array must not be null");
062: }
063: this .names = new String[names.length];
064: this .hashCode = 0;
065: for (int i = 0; i < names.length; i++) {
066: String name = names[i];
067: if (name == null) {
068: throw new IllegalArgumentException(
069: "One of the names is null");
070: }
071: this .names[i] = name;
072: }
073: }
074:
075: public FQN(FQN base, String name) throws IllegalArgumentException {
076: if (base == null) {
077: throw new IllegalArgumentException("Base cannot be null");
078: }
079: if (name == null) {
080: throw new IllegalArgumentException("Name cannot be null");
081: }
082: names = new String[base.names.length + 1];
083: hashCode = 0;
084: System.arraycopy(base.names, 0, names, 0, base.names.length);
085: names[base.names.length] = name;
086: }
087:
088: public FQN(FQN base, String[] relative)
089: throws IllegalArgumentException {
090: if (base == null) {
091: throw new IllegalArgumentException("Base cannot be null");
092: }
093: if (relative == null) {
094: throw new IllegalArgumentException(
095: "Relative cannot be null");
096: }
097: names = new String[base.names.length + relative.length];
098: hashCode = 0;
099: System.arraycopy(base.names, 0, names, 0, base.names.length);
100: System.arraycopy(relative, 0, names, base.names.length,
101: relative.length);
102: }
103:
104: public FQN(FQN base, FQN relative) throws IllegalArgumentException {
105: if (base == null) {
106: throw new IllegalArgumentException("Base cannot be null");
107: }
108: if (relative == null) {
109: throw new IllegalArgumentException(
110: "Relative cannot be null");
111: }
112: names = new String[base.names.length + relative.names.length];
113: hashCode = 0;
114: System.arraycopy(base.names, 0, names, 0, base.names.length);
115: System.arraycopy(relative.names, 0, names, base.names.length,
116: relative.names.length);
117: }
118:
119: public int size() {
120: return names.length;
121: }
122:
123: public String getName(int index)
124: throws ArrayIndexOutOfBoundsException {
125: return names[index];
126: }
127:
128: public String[] toArray() {
129: String[] copy = new String[names.length];
130: System.arraycopy(names, 0, copy, 0, copy.length);
131: return copy;
132: }
133:
134: public boolean isChildOf(FQN parent)
135: throws IllegalArgumentException {
136: if (parent == null) {
137: throw new IllegalArgumentException("Cannot compare to null");
138: }
139: if (names.length != parent.names.length + 1) {
140: return false;
141: }
142: for (int i = 0; i < parent.names.length; i++) {
143: Object name = parent.names[i];
144: if (!name.equals(names[i])) {
145: return false;
146: }
147: }
148: return true;
149: }
150:
151: public boolean equals(Object obj) {
152: if (obj == this ) {
153: return true;
154: }
155: if (!(obj instanceof FQN)) {
156: return false;
157: }
158: FQN other = (FQN) obj;
159: if (other.names.length != names.length) {
160: return false;
161: }
162: for (int i = 0; i < names.length; i++) {
163: if (!names[i].equals(other.names[i])) {
164: return false;
165: }
166: }
167: return true;
168: }
169:
170: public int hashCode() {
171: if (names.length == 0) {
172: return 0;
173: }
174: if (hashCode == 0) {
175: int tmp = 0;
176: for (int i = 0; i < names.length; i++) {
177: tmp = tmp * 29 + names[i].hashCode();
178: }
179: hashCode = tmp;
180: }
181: return hashCode;
182: }
183:
184: public String toString() {
185: StringBuffer buffer = new StringBuffer();
186: for (int i = 0; i < names.length; i++) {
187: buffer.append('/').append(names[i]);
188: }
189: return buffer.toString();
190: }
191:
192: private void writeObject(ObjectOutputStream out) throws IOException {
193: out.writeInt(names.length);
194: for (int i = 0; i < names.length; i++) {
195: out.writeObject(names[i]);
196: }
197: }
198:
199: private void readObject(ObjectInputStream in) throws IOException,
200: ClassNotFoundException {
201: names = new String[in.readInt()];
202: for (int i = 0; i < names.length; i++) {
203: names[i] = (String) in.readObject();
204: }
205: }
206: }
|