001: /*
002: * <copyright>
003: *
004: * Copyright 1997-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.planning.ldm.plan;
028:
029: import java.io.Serializable;
030: import java.util.HashMap;
031:
032: /**
033: * Verb is the action part of a Task.
034: *
035: */
036:
037: public class Verb implements Serializable {
038: private String name;
039:
040: /** Constructor takes a String that represents the verb.
041: * @note pre-11.0 this was deprecated and public - now Verb.get(String) should be used.
042: */
043: protected Verb(String v) {
044: if (v == null)
045: throw new IllegalArgumentException();
046: name = v.intern();
047: }
048:
049: /** @return String toString returns the String that represents the verb */
050: public final String toString() {
051: return name;
052: }
053:
054: /** Capabilities are equal IFF they encapsulate the same string
055: */
056: public final boolean equals(Object v) {
057: // use == since verb strings are interned
058: return (this == v
059: || (v instanceof Verb && name == ((Verb) v).name) || (v instanceof String && name
060: .equals((String) v)));
061: }
062:
063: /** convenience method for verb testing */
064: public final boolean equals(String v) {
065: return (name == v || name.equals(v));
066: }
067:
068: public final int hashCode() {
069: return name.hashCode();
070: }
071:
072: // replace with an interned variation
073: protected Object readResolve() {
074: return getVerb(name);
075: }
076:
077: //
078: // verb cache
079: //
080:
081: private static final HashMap verbs = new HashMap(29);
082:
083: /** older alias for Verb.get()
084: * @deprecated Use Verb.get(String)
085: **/
086: public static Verb getVerb(String vs) {
087: return get(vs);
088: }
089:
090: /** Verb factory method. Constructs or returns cached verb instances
091: * matching the requested paramater.
092: * Note that this will only construct and/or return direct instances of
093: * Verb and never any subclass.
094: **/
095: public static Verb get(String vs) {
096: synchronized (verbs) {
097: Verb v = (Verb) verbs.get(vs);
098: if (v != null)
099: return v;
100: else {
101: vs = vs.intern();
102: v = new Verb(vs);
103: verbs.put(vs, v);
104: return v;
105: }
106: }
107: }
108: }
|