001: /*
002: * <copyright>
003: *
004: * Copyright 2000-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.tools.csmart.core.property.name;
028:
029: import java.io.IOException;
030: import java.io.ObjectInputStream;
031: import java.io.ObjectOutputStream;
032: import java.io.ObjectStreamException;
033: import java.util.HashMap;
034:
035: /**
036: * An implementation of CompositeName have exactly one element that is
037: * a String.
038: **/
039: public class SimpleName implements CompositeName {
040: static final long serialVersionUID = 3189070403185567285L;
041:
042: private String name;
043:
044: public SimpleName(String name) {
045: this .name = name;
046: }
047:
048: /**
049: * Get the number of elements of the name.
050: * @return always 1
051: **/
052: public int size() {
053: return 1;
054: }
055:
056: /**
057: * Get the n-th element of the name
058: * @param n the element index
059: * @return the Name of the selected element
060: **/
061: public CompositeName get(int n) {
062: if (n != 0)
063: throw new IllegalArgumentException("Bad index: " + n);
064: return this ;
065: }
066:
067: public CompositeName getPrefix() {
068: return null;
069: }
070:
071: public CompositeName getPrefix(int n) {
072: if (n != 0)
073: throw new IllegalArgumentException("Bad index: " + n);
074: return this ;
075: }
076:
077: public CompositeName last() {
078: return this ;
079: }
080:
081: /**
082: * Test if this name ends with the given name
083: * @param o the name to compare to
084: * @return true if the final elements of this name are equal to
085: * the elements of the given name
086: **/
087: public boolean endsWith(CompositeName o) {
088: if (o.size() > 1)
089: return false;
090: return name.toString().endsWith(o.toString());
091: }
092:
093: /**
094: * Test if this name starts with the given name
095: * @param o the name to compare to
096: * @return true if the initial elements of this name are equal to
097: * the elements of the given name
098: **/
099: public boolean startsWith(CompositeName o) {
100: if (o.size() > 1)
101: return false;
102: return name.toString().startsWith(o.toString());
103: }
104:
105: /**
106: * Test two names for equality. Names may be of different classes
107: * as long as they are equivalent
108: * @param o Object to be compared with. Must be a CompositeName.
109: **/
110: public boolean equals(Object o) {
111: // short-circuit some common cases
112: if (this == o)
113: return true;
114: if (o instanceof SimpleName && ((SimpleName) o).name == name)
115: return true;
116:
117: if (o instanceof CompositeName) {
118: return compareTo(o) == 0;
119: }
120: return false;
121: }
122:
123: public int hashCode() {
124: return name.hashCode();
125: }
126:
127: /**
128: * Compare to another CompositeName. The difference is the
129: * difference of our name and the first element of the test name
130: * unless they are equal. If the test name is longer it is then
131: * greater.
132: **/
133: public int compareTo(Object o) {
134: CompositeName cn = (CompositeName) o;
135: int diff = name.compareTo(cn.get(0).toString());
136: if (diff == 0 && cn.size() > 1)
137: return -1;
138: return diff;
139: }
140:
141: public String toString() {
142: return name;
143: }
144:
145: public void decache() {
146: } // noop
147:
148: // implement a factory
149: private static final HashMap internedNames = new HashMap(97);
150:
151: /**
152: * Get a new <code>SimpleName</code> from an interned list by name
153: **/
154: public static SimpleName getSimpleName(String name) {
155: name = name.intern();
156: synchronized (internedNames) {
157: SimpleName sn = (SimpleName) internedNames.get(name);
158: if (sn == null) {
159: sn = new SimpleName(name);
160: internedNames.put(name, sn);
161: }
162: return sn;
163: }
164: }
165:
166: // just do the default for now.
167: private void writeObject(ObjectOutputStream stream)
168: throws IOException {
169: stream.defaultWriteObject();
170: }
171:
172: // just do the default for now.
173: private void readObject(ObjectInputStream stream)
174: throws IOException, ClassNotFoundException {
175: stream.defaultReadObject();
176: }
177:
178: /** keep the names interned **/
179: private Object readResolve() throws ObjectStreamException {
180: return getSimpleName(name);
181: }
182:
183: }
|