001: /*
002: * Copyright 2006 JBoss Inc
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.drools.spi;
018:
019: import java.lang.reflect.Method;
020: import java.util.Map;
021:
022: import org.drools.RuntimeDroolsException;
023: import org.drools.base.ClassObjectType;
024: import org.drools.base.ValueType;
025: import org.drools.common.InternalWorkingMemory;
026: import org.drools.util.ClassUtils;
027:
028: /**
029: * This is a global variable extractor used to get a global variable value
030: *
031: * @author etirelli
032: */
033: public class GlobalExtractor implements Extractor {
034:
035: private static final long serialVersionUID = 400L;
036: private final String key;
037: private final ObjectType objectType;
038:
039: public GlobalExtractor(final String key, final Map map) {
040: this .key = key;
041: this .objectType = new ClassObjectType((Class) map.get(this .key));
042: }
043:
044: public Object getValue(InternalWorkingMemory workingMemory,
045: final Object object) {
046: return workingMemory.getGlobal(key);
047: }
048:
049: public ObjectType getObjectType() {
050: return this .objectType;
051: }
052:
053: public Class getExtractToClass() {
054: return this .objectType.getValueType().getClassType();
055: }
056:
057: public String getExtractToClassName() {
058: return ClassUtils.canonicalName(this .objectType.getValueType()
059: .getClassType());
060: }
061:
062: public ValueType getValueType() {
063: return this .objectType.getValueType();
064: }
065:
066: public boolean getBooleanValue(InternalWorkingMemory workingMemory,
067: final Object object) {
068: if (this .objectType.getValueType().isBoolean()) {
069: return ((Boolean) workingMemory.getGlobal(key))
070: .booleanValue();
071: }
072: throw new ClassCastException("Not possible to convert global '"
073: + key + "' into a boolean.");
074: }
075:
076: public byte getByteValue(InternalWorkingMemory workingMemory,
077: final Object object) {
078: if (this .objectType.getValueType().isNumber()) {
079: return ((Number) workingMemory.getGlobal(key)).byteValue();
080: }
081: throw new ClassCastException("Not possible to convert global '"
082: + key + "' into a byte.");
083: }
084:
085: public char getCharValue(InternalWorkingMemory workingMemory,
086: final Object object) {
087: if (this .objectType.getValueType().isChar()) {
088: return ((Character) workingMemory.getGlobal(key))
089: .charValue();
090: }
091: throw new ClassCastException("Not possible to convert global '"
092: + key + "' into a char.");
093: }
094:
095: public double getDoubleValue(InternalWorkingMemory workingMemory,
096: final Object object) {
097: if (this .objectType.getValueType().isNumber()) {
098: return ((Number) workingMemory.getGlobal(key))
099: .doubleValue();
100: }
101: throw new ClassCastException("Not possible to convert global '"
102: + key + "' into a double.");
103: }
104:
105: public float getFloatValue(InternalWorkingMemory workingMemory,
106: final Object object) {
107: if (this .objectType.getValueType().isNumber()) {
108: return ((Number) workingMemory.getGlobal(key)).floatValue();
109: }
110: throw new ClassCastException("Not possible to convert global '"
111: + key + "' into a float.");
112: }
113:
114: public int getIntValue(InternalWorkingMemory workingMemory,
115: final Object object) {
116: if (this .objectType.getValueType().isNumber()) {
117: return ((Number) workingMemory.getGlobal(key)).intValue();
118: }
119: throw new ClassCastException("Not possible to convert global '"
120: + key + "' into an int.");
121: }
122:
123: public long getLongValue(InternalWorkingMemory workingMemory,
124: final Object object) {
125: if (this .objectType.getValueType().isNumber()) {
126: return ((Number) workingMemory.getGlobal(key)).longValue();
127: }
128: throw new ClassCastException("Not possible to convert global '"
129: + key + "' into a long.");
130: }
131:
132: public short getShortValue(InternalWorkingMemory workingMemory,
133: final Object object) {
134: if (this .objectType.getValueType().isNumber()) {
135: return ((Number) workingMemory.getGlobal(key)).shortValue();
136: }
137: throw new ClassCastException("Not possible to convert global '"
138: + key + "' into a short.");
139: }
140:
141: public Method getNativeReadMethod() {
142: try {
143: return this .getClass().getDeclaredMethod(
144: "getValue",
145: new Class[] { InternalWorkingMemory.class,
146: Object.class });
147: } catch (final Exception e) {
148: throw new RuntimeDroolsException(
149: "This is a bug. Please report to development team: "
150: + e.getMessage(), e);
151: }
152: }
153:
154: public int getHashCode(InternalWorkingMemory workingMemory,
155: final Object object) {
156: final Object value = getValue(workingMemory, object);
157: return (value != null) ? value.hashCode() : 0;
158: }
159:
160: public int hashCode() {
161: return this .objectType.hashCode();
162: }
163:
164: public boolean equals(final Object obj) {
165: if (this == obj) {
166: return true;
167: }
168: if (!(obj instanceof GlobalExtractor)) {
169: return false;
170: }
171: final GlobalExtractor other = (GlobalExtractor) obj;
172: return (key == null ? other.key == null : key.equals(other.key))
173: && (this .objectType == null ? other.objectType == null
174: : this .objectType.equals(other.objectType));
175: }
176:
177: public boolean isNullValue(InternalWorkingMemory workingMemory,
178: Object object) {
179: final Object value = getValue(workingMemory, object);
180: return value == null;
181: }
182:
183: public boolean isGlobal() {
184: return true;
185: }
186: }
|