001: /**
002: * Copyright 2007 Jens Dietrich Licensed under the Apache License, Version 2.0 (the "License");
003: * you may not use this file except in compliance with the License.
004: * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
005: * Unless required by applicable law or agreed to in writing, software distributed under the
006: * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
007: * either express or implied. See the License for the specific language governing permissions
008: * and limitations under the License.
009: */package nz.org.take;
010:
011: /**
012: * Constant terms. Basically those are just arbitrary (wrapped) objects.
013: * @author <a href="http://www-ist.massey.ac.nz/JBDietrich/">Jens Dietrich</a>
014: */
015:
016: public class Constant extends AbstractAnnotatable implements Term {
017:
018: private Object object = null;
019: // the type can be overridden, by default the type of the object is used
020: private Class type = null;
021: // this is a string that is used to proxy the object
022: private String ref = null;
023:
024: public Object getObject() {
025: return object;
026: }
027:
028: public void setObject(Object o) {
029: checkTypeConsistency(o, type);
030: this .object = o;
031: }
032:
033: public Class getType() {
034: if (type == null) {
035: if (object == null)
036: return null;
037: else
038: return object.getClass();
039: }
040: return type;
041: }
042:
043: public void setType(Class t) {
044: checkTypeConsistency(object, t);
045: this .type = t;
046: }
047:
048: private void checkTypeConsistency(Object o, Class t) {
049: if (o != null && t != null && !t.isAssignableFrom(o.getClass()))
050: throw new IllegalArgumentException(
051: "Object "
052: + o
053: + " and type "
054: + t
055: + " are inconsistent, the type must be a supertype of the object type");
056: }
057:
058: public String getRef() {
059: return ref;
060: }
061:
062: public void setRef(String ref) {
063: this .ref = ref;
064: }
065:
066: /**
067: * Whether this is just a placeholder to access the object or a real object.
068: * @return
069: */
070: public boolean isProxy() {
071: return this .object == null && this .ref != null;
072: }
073:
074: /**
075: * Whether this is a literal, in particular a string.
076: * @return
077: */
078: public boolean isLiteral() {
079: Class type = getType();
080: if (this .object == null)
081: return false; // proxy
082: else
083: return (type == String.class) || type.isPrimitive()
084: || java.lang.Number.class.isAssignableFrom(type)
085: || type == Boolean.class;
086: }
087:
088: public String toString() {
089: StringBuffer b = new StringBuffer();
090: if (isProxy())
091: b.append('[');
092: b.append(isProxy() ? ref : object);
093: if (isProxy())
094: b.append(']');
095: return b.toString();
096: }
097:
098: public void accept(KnowledgeBaseVisitor visitor) {
099: visitor.visit(this );
100: visitor.endVisit(this );
101: }
102:
103: // return a Java literal prepresenting this object, or null if this isn't possible
104: public String getLiteral() {
105: if (object != null) {
106: if (object instanceof String)
107: return "\"" + object + "\"";
108: else if (object instanceof Number
109: || object instanceof Boolean
110: || object.getClass().isPrimitive())
111: return String.valueOf(object);
112: }
113: return null;
114: }
115:
116: @Override
117: public int hashCode() {
118: final int PRIME = 31;
119: int result = 1;
120: result = PRIME * result
121: + ((object == null) ? 0 : object.hashCode());
122: result = PRIME * result + ((ref == null) ? 0 : ref.hashCode());
123: result = PRIME * result
124: + ((type == null) ? 0 : type.hashCode());
125: return result;
126: }
127:
128: @Override
129: public boolean equals(Object obj) {
130: if (this == obj)
131: return true;
132: if (obj == null)
133: return false;
134: if (getClass() != obj.getClass())
135: return false;
136: final Constant other = (Constant) obj;
137: if (object == null) {
138: if (other.object != null)
139: return false;
140: } else if (!object.equals(other.object))
141: return false;
142: if (ref == null) {
143: if (other.ref != null)
144: return false;
145: } else if (!ref.equals(other.ref))
146: return false;
147: if (type == null) {
148: if (other.type != null)
149: return false;
150: } else if (!type.equals(other.type))
151: return false;
152: return true;
153: }
154: }
|