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.theme;
023:
024: import java.io.IOException;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027: import java.io.Serializable;
028:
029: /**
030: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
031: * @version $Revision: 8784 $
032: * @deprecated Defines a reusable and generic full qualified name.
033: */
034: public class FQN implements Serializable {
035:
036: /** The serialVersionUID */
037: private static final long serialVersionUID = -4843390736196899215L;
038:
039: private static final String[] EMPTY_ARRAY = new String[0];
040:
041: /** The list of objects. */
042: protected String[] names; // julien : perhaps Serializable[] ?
043:
044: /** The cached hashcode. */
045: private int hashCode;
046:
047: public FQN() {
048: names = EMPTY_ARRAY;
049: hashCode = 0;
050: }
051:
052: public FQN(String name) throws IllegalArgumentException {
053: if (name == null) {
054: throw new IllegalArgumentException("Name cannot be null");
055: }
056: names = new String[] { name };
057: hashCode = 0;
058: }
059:
060: public FQN(String[] names) throws IllegalArgumentException {
061: if (names == null) {
062: throw new IllegalArgumentException(
063: "Names array must not be null");
064: }
065: this .names = new String[names.length];
066: this .hashCode = 0;
067: for (int i = 0; i < names.length; i++) {
068: String name = names[i];
069: if (name == null) {
070: throw new IllegalArgumentException(
071: "One of the names is null");
072: }
073: this .names[i] = name;
074: }
075: }
076:
077: public FQN(FQN base, String name) throws IllegalArgumentException {
078: if (base == null) {
079: throw new IllegalArgumentException("Base cannot be null");
080: }
081: if (name == null) {
082: throw new IllegalArgumentException("Name cannot be null");
083: }
084: names = new String[base.names.length + 1];
085: hashCode = 0;
086: System.arraycopy(base.names, 0, names, 0, base.names.length);
087: names[base.names.length] = name;
088: }
089:
090: public FQN(FQN base, String[] relative)
091: throws IllegalArgumentException {
092: if (base == null) {
093: throw new IllegalArgumentException("Base cannot be null");
094: }
095: if (relative == null) {
096: throw new IllegalArgumentException(
097: "Relative cannot be null");
098: }
099: names = new String[base.names.length + relative.length];
100: hashCode = 0;
101: System.arraycopy(base.names, 0, names, 0, base.names.length);
102: System.arraycopy(relative, 0, names, base.names.length,
103: relative.length);
104: }
105:
106: public FQN(FQN base, FQN relative) throws IllegalArgumentException {
107: if (base == null) {
108: throw new IllegalArgumentException("Base cannot be null");
109: }
110: if (relative == null) {
111: throw new IllegalArgumentException(
112: "Relative cannot be null");
113: }
114: names = new String[base.names.length + relative.names.length];
115: hashCode = 0;
116: System.arraycopy(base.names, 0, names, 0, base.names.length);
117: System.arraycopy(relative.names, 0, names, base.names.length,
118: relative.names.length);
119: }
120:
121: public int size() {
122: return names.length;
123: }
124:
125: public String getName(int index)
126: throws ArrayIndexOutOfBoundsException {
127: return names[index];
128: }
129:
130: public String[] toArray() {
131: String[] copy = new String[names.length];
132: System.arraycopy(names, 0, copy, 0, copy.length);
133: return copy;
134: }
135:
136: public boolean isChildOf(FQN parent)
137: throws IllegalArgumentException {
138: if (parent == null) {
139: throw new IllegalArgumentException("Cannot compare to null");
140: }
141: if (names.length != parent.names.length + 1) {
142: return false;
143: }
144: for (int i = 0; i < parent.names.length; i++) {
145: Object name = parent.names[i];
146: if (!name.equals(names[i])) {
147: return false;
148: }
149: }
150: return true;
151: }
152:
153: public boolean equals(Object obj) {
154: if (obj == this ) {
155: return true;
156: }
157: if (!(obj instanceof FQN)) {
158: return false;
159: }
160: FQN other = (FQN) obj;
161: if (other.names.length != names.length) {
162: return false;
163: }
164: for (int i = 0; i < names.length; i++) {
165: if (!names[i].equals(other.names[i])) {
166: return false;
167: }
168: }
169: return true;
170: }
171:
172: public int hashCode() {
173: if (names.length == 0) {
174: return 0;
175: }
176: if (hashCode == 0) {
177: int tmp = 0;
178: for (int i = 0; i < names.length; i++) {
179: tmp = tmp * 29 + names[i].hashCode();
180: }
181: hashCode = tmp;
182: }
183: return hashCode;
184: }
185:
186: public String toString() {
187: StringBuffer buffer = new StringBuffer();
188: for (int i = 0; i < names.length; i++) {
189: buffer.append('/').append(names[i]);
190: }
191: return buffer.toString();
192: }
193:
194: private void writeObject(ObjectOutputStream out) throws IOException {
195: out.writeInt(names.length);
196: for (int i = 0; i < names.length; i++) {
197: out.writeObject(names[i]);
198: }
199: }
200:
201: private void readObject(ObjectInputStream in) throws IOException,
202: ClassNotFoundException {
203: names = new String[in.readInt()];
204: for (int i = 0; i < names.length; i++) {
205: names[i] = (String) in.readObject();
206: }
207: }
208: }
|