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.lib.contract.lang.op;
028:
029: import java.util.*;
030:
031: import org.cougaar.lib.contract.lang.*;
032: import org.cougaar.lib.contract.lang.op.constant.*;
033: import org.cougaar.lib.contract.lang.op.logical.*;
034: import org.cougaar.lib.contract.lang.op.list.*;
035: import org.cougaar.lib.contract.lang.op.reflect.*;
036: import org.cougaar.lib.contract.lang.cache.ClassCache;
037:
038: public final class OpBuilder implements OpCodes {
039:
040: public static Map opNames;
041: static {
042: opNames = new HashMap(16);
043: // constant
044: opNames.put(CONSTANT_NAME, new Integer(CONSTANT_ID));
045: opNames.put(GET_NAME, new Integer(GET_ID));
046: // list
047: opNames.put(ALL_NAME, new Integer(ALL_ID));
048: opNames.put(EMPTY_NAME, new Integer(EMPTY_ID));
049: opNames.put(EXISTS_NAME, new Integer(EXISTS_ID));
050: // logical
051: opNames.put(AND_NAME, new Integer(AND_ID));
052: opNames.put(FALSE_NAME, new Integer(FALSE_ID));
053: opNames.put(NOT_NAME, new Integer(NOT_ID));
054: opNames.put(OR_NAME, new Integer(OR_ID));
055: opNames.put(TRUE_NAME, new Integer(TRUE_ID));
056: // reflect
057: opNames.put(APPLY_NAME, new Integer(APPLY_ID));
058: opNames.put(FIELD_NAME, new Integer(FIELD_ID));
059: opNames.put(INSTANCEOF_NAME, new Integer(INSTANCEOF_ID));
060: opNames.put(METHOD_NAME, new Integer(METHOD_ID));
061: opNames.put(REFLECT_NAME, new Integer(REFLECT_ID));
062: opNames.put(THIS_NAME, new Integer(THIS_ID));
063: }
064:
065: private OpBuilder() {
066: }
067:
068: public static final Op create(final String name) {
069: Integer id = (Integer) opNames.get(name);
070: if (id != null) {
071: switch (id.intValue()) {
072: // constant
073: case CONSTANT_ID:
074: return new ConstantOp();
075: case GET_ID:
076: return new GetOp();
077: // list
078: case ALL_ID:
079: return new AllOp();
080: case EMPTY_ID:
081: return EmptyOp.singleInstance;
082: case EXISTS_ID:
083: return new ExistsOp();
084: // logical
085: case AND_ID:
086: return new AndOp();
087: case FALSE_ID:
088: return FalseOp.singleInstance;
089: case NOT_ID:
090: return new NotOp();
091: case OR_ID:
092: return new OrOp();
093: case TRUE_ID:
094: return TrueOp.singleInstance;
095: // reflect
096: case APPLY_ID:
097: return new ApplyOp();
098: case FIELD_ID:
099: throw new UnsupportedOperationException(
100: "OpParser should use OpBuilder.createReflectOp!");
101: case INSTANCEOF_ID:
102: throw new UnsupportedOperationException(
103: "OpParser should use OpBuilder.createInstanceOfOp!");
104: case METHOD_ID:
105: throw new UnsupportedOperationException(
106: "OpParser should use OpBuilder.createReflectOp!");
107: case REFLECT_ID:
108: throw new UnsupportedOperationException(
109: "OpParser should use OpBuilder.createReflectOp!");
110: case THIS_ID:
111: return new ThisOp();
112: // other
113: default:
114: throw new RuntimeException("Unknown Op code: " + id);
115: }
116: } else if (name.startsWith("is:")) {
117: return createInstanceOf(name);
118: } else {
119: // must be reflect (method/field)
120: return createReflectOp(name);
121: }
122: }
123:
124: /** create a String ConstantOp from a String **/
125: public static final Op createConstantOp(String sval) {
126: return new ConstantOp(String.class, sval);
127: }
128:
129: /** create a ConstantOp from a String **/
130: public static final Op createConstantOp(String sclass, String sval) {
131: return new ConstantOp(sclass, sval);
132: }
133:
134: /** create an InstanceOfOp from a String **/
135: public static final Op createInstanceOf(final String s) {
136: return new InstanceOfOp(s);
137: }
138:
139: /** create an InstanceOfOp from boolean/Class pair **/
140: public static final Op createInstanceOf(final boolean not,
141: final Class cl) {
142: return new InstanceOfOp(not, cl);
143: }
144:
145: /** create a ReflectOp from a String **/
146: public static final Op createReflectOp(final String s) {
147: return new ReflectOp(s);
148: }
149:
150: }
|