001: /*
002: * <copyright>
003: *
004: * Copyright 2003-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.lib.aggagent.util;
028:
029: import java.io.ObjectInputStream;
030: import java.io.Serializable;
031: import java.util.Collection;
032: import java.util.Iterator;
033: import java.util.LinkedList;
034:
035: /**
036: * Typesafe enumerated types used by AggregationQuery
037: */
038: public abstract class Enum implements Serializable {
039: private String enumName;
040:
041: protected Enum(String name) {
042: enumName = name;
043: }
044:
045: public String toString() {
046: return enumName;
047: }
048:
049: protected abstract String getStringObject(String name);
050:
051: public boolean equals(Object obj) {
052: if (obj == this )
053: return true;
054:
055: // Can't just return true if objects are == because one may have been serialized.
056: if (!(obj instanceof Enum))
057: return false;
058:
059: return (((Enum) obj).enumName == enumName);
060: }
061:
062: private void readObject(ObjectInputStream ois)
063: throws java.io.IOException, ClassNotFoundException {
064: enumName = (String) ois.readObject();
065: enumName = getStringObject(enumName);
066: }
067:
068: protected static Object findEnum(Collection col, String name) {
069: for (Iterator i = col.iterator(); i.hasNext();) {
070: Object nextEnum = i.next();
071: if (name.equals(nextEnum.toString()))
072: return nextEnum;
073: }
074: return null;
075: }
076:
077: public static class ScriptType extends Enum {
078: private static final Collection validValues = new LinkedList();
079:
080: public static final ScriptType UNARY_PREDICATE = new ScriptType(
081: "unary_predicate");
082: public static final ScriptType INCREMENT_FORMAT = new ScriptType(
083: "xml_encoder");
084: public static final ScriptType AGGREGATOR = new ScriptType(
085: "aggregator");
086: public static final ScriptType ALERT = new ScriptType(
087: "alert_script");
088:
089: public static Collection getValidValues() {
090: return new LinkedList(validValues);
091: }
092:
093: protected String getStringObject(String enumName) {
094: Enum en = (Enum) findEnum(validValues, enumName);
095: return en == null ? null : en.toString();
096: }
097:
098: public static ScriptType fromString(String enumName) {
099: return (ScriptType) findEnum(validValues, enumName);
100: }
101:
102: private ScriptType(String name) {
103: super (name);
104: validValues.add(this );
105: }
106: }
107:
108: public static class QueryType extends Enum {
109: private static final LinkedList validValues = new LinkedList();
110: public static final QueryType TRANSIENT = new QueryType(
111: "Transient");
112: public static final QueryType PERSISTENT = new QueryType(
113: "Persistent");
114:
115: private QueryType(String name) {
116: super (name);
117: validValues.add(this );
118: }
119:
120: protected String getStringObject(String enumName) {
121: Enum en = (Enum) findEnum(validValues, enumName);
122: return en == null ? null : en.toString();
123: }
124:
125: public static QueryType fromString(String enumName) {
126: return (QueryType) findEnum(validValues, enumName);
127: }
128:
129: public static Collection getValidValues() {
130: return (Collection) validValues.clone();
131: }
132: }
133:
134: public static class UpdateMethod extends Enum {
135: private static final LinkedList validValues = new LinkedList();
136: public static final UpdateMethod PUSH = new UpdateMethod("Push");
137: public static final UpdateMethod PULL = new UpdateMethod("Pull");
138:
139: private UpdateMethod(String name) {
140: super (name);
141: validValues.add(this );
142: }
143:
144: protected String getStringObject(String enumName) {
145: Enum en = (Enum) findEnum(validValues, enumName);
146: return en == null ? null : en.toString();
147: }
148:
149: public static UpdateMethod fromString(String enumName) {
150: return (UpdateMethod) findEnum(validValues, enumName);
151: }
152:
153: public static Collection getValidValues() {
154: return (Collection) validValues.clone();
155: }
156: }
157:
158: public static class Language extends Enum {
159: private static final LinkedList validValues = new LinkedList();
160: public static final Language SILK = new Language("SILK");
161: public static final Language JPYTHON = new Language("JPython");
162: public static final Language JAVA = new Language("Java");
163:
164: private Language(String name) {
165: super (name);
166: validValues.add(this );
167: }
168:
169: protected String getStringObject(String enumName) {
170: Enum en = (Enum) findEnum(validValues, enumName);
171: return en == null ? null : en.toString();
172: }
173:
174: public static Language fromString(String enumName) {
175: return (Language) findEnum(validValues, enumName);
176: }
177:
178: public static Collection getValidValues() {
179: return (Collection) validValues.clone();
180: }
181: }
182:
183: public static class XmlFormat extends Enum {
184: private static final LinkedList validValues = new LinkedList();
185: public static final XmlFormat INCREMENT = new XmlFormat(
186: "Increment");
187: public static final XmlFormat XMLENCODER = new XmlFormat(
188: "XMLEncoder");
189:
190: private XmlFormat(String name) {
191: super (name);
192: validValues.add(this );
193: }
194:
195: protected String getStringObject(String enumName) {
196: Enum en = (Enum) findEnum(validValues, enumName);
197: return en == null ? null : en.toString();
198: }
199:
200: public static XmlFormat fromString(String enumName) {
201: return (XmlFormat) findEnum(validValues, enumName);
202: }
203:
204: public static Collection getValidValues() {
205: return (Collection) validValues.clone();
206: }
207: }
208:
209: public static class AggType extends Enum {
210: private static final LinkedList validValues = new LinkedList();
211: public static final AggType AGGREGATOR = new AggType(
212: "Aggregator");
213: public static final AggType MELDER = new AggType("Melder");
214:
215: private AggType(String name) {
216: super (name);
217: validValues.add(this );
218: }
219:
220: protected String getStringObject(String enumName) {
221: Enum en = (Enum) findEnum(validValues, enumName);
222: return en == null ? null : en.toString();
223: }
224:
225: public static AggType fromString(String enumName) {
226: return (AggType) findEnum(validValues, enumName);
227: }
228:
229: public static Collection getValidValues() {
230: return (Collection) validValues.clone();
231: }
232: }
233: }
|