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.reflect;
028:
029: import java.util.*;
030:
031: import org.cougaar.lib.contract.lang.*;
032: import org.cougaar.lib.contract.lang.op.OpCodes;
033: import org.cougaar.lib.contract.lang.op.logical.FalseOp;
034: import org.cougaar.lib.contract.lang.op.logical.TrueOp;
035: import org.cougaar.lib.contract.lang.type.TypeImpl;
036:
037: /**
038: * "instanceof" <code>Op</code> -- checks class/null compatability.
039: * <p>
040: * May want to replace constructor to cache "getter".
041: */
042: public final class InstanceOfOp extends OpImpl implements Type {
043:
044: /**
045: * Multiple inheritance would be nice, in which case this class
046: * could extend both <code>InstanceOfOp</code> and <code>TypeImpl</code>.
047: */
048: private final Type type;
049: /** shortcut type fields for quick <tt>execute</tt> **/
050: private final boolean not;
051: private final Class cl;
052:
053: private InstanceOfOp() {
054: throw new InternalError("Empty constructor not used!");
055: }
056:
057: public InstanceOfOp(Type type) {
058: this .type = type;
059: this .not = type.isNot();
060: this .cl = type.getClazz();
061: }
062:
063: public InstanceOfOp(String s) {
064: this (TypeImpl.getInstance(s));
065: }
066:
067: public InstanceOfOp(boolean not, String classname) {
068: this (TypeImpl.getInstance(not, classname));
069: }
070:
071: public InstanceOfOp(boolean not, Class cl) {
072: this (TypeImpl.getInstance(not, cl));
073: }
074:
075: public final int getID() {
076: return OpCodes.INSTANCEOF_ID;
077: }
078:
079: public final Op parse(final OpParser p) throws ParseException {
080: Op u1 = p.nextOp();
081: if (u1 != null) {
082: throw new ParseException("\"" + OpCodes.INSTANCEOF_NAME
083: + "\" \"" + this + "\" expecting zero arguments,"
084: + " but given " + u1.getClass().toString());
085: }
086:
087: switch (p.addType(type)) {
088: case TypeList.ADD_IGNORED:
089: return TrueOp.singleInstance;
090: case TypeList.ADD_USED:
091: return this ;
092: case TypeList.ADD_CONFLICT:
093: return FalseOp.singleInstance;
094: default:
095: throw new ParseException("\"" + OpCodes.INSTANCEOF_NAME
096: + "\" unable to add " + type + " to "
097: + p.getTypeList());
098: }
099: }
100:
101: public final boolean isNot() {
102: return not;
103: }
104:
105: public final Class getClazz() {
106: return cl;
107: }
108:
109: public final boolean implies(final Type xtype) {
110: return type.implies(xtype);
111: }
112:
113: public final boolean implies(final boolean xnot, final Class xcl) {
114: return type.implies(xnot, xcl);
115: }
116:
117: public final boolean impliedBy(final Type xtype) {
118: return type.impliedBy(xtype);
119: }
120:
121: public final boolean impliedBy(final boolean xnot, final Class xcl) {
122: return type.impliedBy(xnot, xcl);
123: }
124:
125: public final String toString(final boolean verbose) {
126: return type.toString(verbose);
127: }
128:
129: /**
130: * Convert fields to <code>setClass</code> format.
131: */
132: public final String getString(boolean verbose) {
133: return type.toString(verbose);
134: }
135:
136: public final boolean execute(final Object o) {
137: if (o == null) {
138: return not;
139: } else if (cl == Object.class) {
140: return (!(not));
141: } else {
142: // (o instanceof cl)
143: boolean b = cl.isAssignableFrom(o.getClass());
144: return (not ? (!(b)) : b);
145: }
146: }
147:
148: public final void accept(TreeVisitor visitor) {
149: // (instanceofop)
150: visitor.visitWord(type.toString(visitor.isVerbose()));
151: visitor.visitEnd();
152: }
153: }
|