001: package org.drools.base.extractors;
002:
003: import java.lang.reflect.Method;
004:
005: import org.drools.RuntimeDroolsException;
006: import org.drools.base.BaseClassFieldExtractor;
007: import org.drools.base.ValueType;
008: import org.drools.common.InternalWorkingMemory;
009:
010: public abstract class BaseObjectClassFieldExtractor extends
011: BaseClassFieldExtractor {
012:
013: private static final long serialVersionUID = 400L;
014:
015: protected BaseObjectClassFieldExtractor(final int index,
016: final Class fieldType, final ValueType valueType) {
017: super (index, fieldType, valueType);
018: }
019:
020: public BaseObjectClassFieldExtractor(final Class clazz,
021: final String fieldName) {
022: super (clazz, fieldName);
023: }
024:
025: public abstract Object getValue(
026: InternalWorkingMemory workingMemory, Object object);
027:
028: public boolean getBooleanValue(InternalWorkingMemory workingMemory,
029: final Object object) {
030: // this can be improved by generating specific
031: // bytecode generation in the subclass, avoiding the if instanceof
032: final Object value = getValue(workingMemory, object);
033:
034: if (value instanceof Boolean) {
035: return ((Boolean) value).booleanValue();
036: }
037: throw new RuntimeDroolsException(
038: "Conversion to boolean not supported from "
039: + value.getClass().getName());
040: }
041:
042: public byte getByteValue(InternalWorkingMemory workingMemory,
043: final Object object) {
044: // this can be improved by generating specific
045: // bytecode generation in the subclass, avoiding the if instanceof
046: final Object value = getValue(workingMemory, object);
047:
048: if (value instanceof Number) {
049: return ((Number) value).byteValue();
050: }
051: throw new RuntimeDroolsException(
052: "Conversion to byte not supported from "
053: + value.getClass().getName());
054: }
055:
056: public char getCharValue(InternalWorkingMemory workingMemory,
057: final Object object) {
058: // this can be improved by generating specific
059: // bytecode generation in the subclass, avoiding the if instanceof
060: final Object value = getValue(workingMemory, object);
061:
062: if (value instanceof Character) {
063: return ((Character) value).charValue();
064: } else if (value instanceof String
065: && ((String) value).length() == 1) {
066: return ((String) value).charAt(0);
067: }
068: throw new RuntimeDroolsException(
069: "Conversion to char not supported from "
070: + value.getClass().getName());
071: }
072:
073: public double getDoubleValue(InternalWorkingMemory workingMemory,
074: final Object object) {
075: // this can be improved by generating specific
076: // bytecode generation in the subclass, avoiding the if instanceof
077: final Object value = getValue(workingMemory, object);
078:
079: if (value instanceof Number) {
080: return ((Number) value).doubleValue();
081: }
082: throw new RuntimeDroolsException(
083: "Conversion to double not supported from "
084: + value.getClass().getName());
085: }
086:
087: public float getFloatValue(InternalWorkingMemory workingMemory,
088: final Object object) {
089: // this can be improved by generating specific
090: // bytecode generation in the subclass, avoiding the if instanceof
091: final Object value = getValue(workingMemory, object);
092:
093: if (value instanceof Number) {
094: return ((Number) value).floatValue();
095: }
096: throw new RuntimeDroolsException(
097: "Conversion to float not supported from "
098: + value.getClass().getName());
099: }
100:
101: public int getIntValue(InternalWorkingMemory workingMemory,
102: final Object object) {
103: // this can be improved by generating specific
104: // bytecode generation in the subclass, avoiding the if instanceof
105: final Object value = getValue(workingMemory, object);
106:
107: if (value instanceof Number) {
108: return ((Number) value).intValue();
109: }
110: throw new RuntimeDroolsException(
111: "Conversion to int not supported from "
112: + value.getClass().getName());
113: }
114:
115: public long getLongValue(InternalWorkingMemory workingMemory,
116: final Object object) {
117: // this can be improved by generating specific
118: // bytecode generation in the subclass, avoiding the if instanceof
119: final Object value = getValue(workingMemory, object);
120:
121: if (value instanceof Number) {
122: return ((Number) value).longValue();
123: }
124: throw new RuntimeDroolsException(
125: "Conversion to long not supported from "
126: + value.getClass().getName());
127: }
128:
129: public short getShortValue(InternalWorkingMemory workingMemory,
130: final Object object) {
131: // this can be improved by generating specific
132: // bytecode generation in the subclass, avoiding the if instanceof
133: final Object value = getValue(workingMemory, object);
134:
135: if (value instanceof Number) {
136: return ((Number) value).shortValue();
137: }
138: throw new RuntimeDroolsException(
139: "Conversion to short not supported from "
140: + value.getClass().getName());
141: }
142:
143: public boolean isNullValue(InternalWorkingMemory workingMemory,
144: final Object object) {
145: if (object == null) {
146: return true;
147: } else {
148: return getValue(workingMemory, object) == null;
149: }
150: }
151:
152: public Method getNativeReadMethod() {
153: try {
154: return this .getClass().getDeclaredMethod(
155: "getValue",
156: new Class[] { InternalWorkingMemory.class,
157: Object.class });
158: } catch (final Exception e) {
159: throw new RuntimeDroolsException(
160: "This is a bug. Please report to development team: "
161: + e.getMessage(), e);
162: }
163: }
164:
165: public int getHashCode(InternalWorkingMemory workingMemory,
166: final Object object) {
167: final Object value = getValue(workingMemory, object);
168: return (value != null) ? value.hashCode() : 0;
169: }
170:
171: }
|