001: package org.drools.facttemplates;
002:
003: import java.lang.reflect.Method;
004:
005: import org.drools.RuntimeDroolsException;
006: import org.drools.base.ValueType;
007: import org.drools.common.InternalWorkingMemory;
008: import org.drools.spi.FieldExtractor;
009: import org.drools.util.ClassUtils;
010:
011: public class FactTemplateFieldExtractor implements FieldExtractor {
012:
013: private static final long serialVersionUID = 400L;
014: private FactTemplate factTemplate;
015: private int fieldIndex;
016:
017: public FactTemplateFieldExtractor(final FactTemplate factTemplate,
018: final int fieldIndex) {
019: this .factTemplate = factTemplate;
020: this .fieldIndex = fieldIndex;
021: }
022:
023: public ValueType getValueType() {
024: return this .factTemplate.getFieldTemplate(this .fieldIndex)
025: .getValueType();
026: }
027:
028: public Object getValue(InternalWorkingMemory workingMemory,
029: final Object object) {
030: return ((Fact) object).getFieldValue(this .fieldIndex);
031: }
032:
033: public int getIndex() {
034: return this .fieldIndex;
035: }
036:
037: public Class getExtractToClass() {
038: return Fact.class;//this.factTemplate.getFieldTemplate( fieldIndex ).getValueType().getClass();
039: }
040:
041: public String getExtractToClassName() {
042: return ClassUtils.canonicalName(Fact.class);//this.factTemplate.getFieldTemplate( fieldIndex ).getValueType().getClass();
043: }
044:
045: public boolean getBooleanValue(InternalWorkingMemory workingMemory,
046: final Object object) {
047: return ((Boolean) ((Fact) object)
048: .getFieldValue(this .fieldIndex)).booleanValue();
049: }
050:
051: public byte getByteValue(InternalWorkingMemory workingMemory,
052: final Object object) {
053: return ((Number) ((Fact) object).getFieldValue(this .fieldIndex))
054: .byteValue();
055: }
056:
057: public char getCharValue(InternalWorkingMemory workingMemory,
058: final Object object) {
059: return ((Character) ((Fact) object)
060: .getFieldValue(this .fieldIndex)).charValue();
061: }
062:
063: public double getDoubleValue(InternalWorkingMemory workingMemory,
064: final Object object) {
065: return ((Number) ((Fact) object).getFieldValue(this .fieldIndex))
066: .doubleValue();
067: }
068:
069: public float getFloatValue(InternalWorkingMemory workingMemory,
070: final Object object) {
071: return ((Number) ((Fact) object).getFieldValue(this .fieldIndex))
072: .floatValue();
073: }
074:
075: public int getIntValue(InternalWorkingMemory workingMemory,
076: final Object object) {
077: return ((Number) ((Fact) object).getFieldValue(this .fieldIndex))
078: .intValue();
079: }
080:
081: public long getLongValue(InternalWorkingMemory workingMemory,
082: final Object object) {
083: return ((Number) ((Fact) object).getFieldValue(this .fieldIndex))
084: .longValue();
085: }
086:
087: public short getShortValue(InternalWorkingMemory workingMemory,
088: final Object object) {
089: return ((Number) ((Fact) object).getFieldValue(this .fieldIndex))
090: .shortValue();
091: }
092:
093: public Method getNativeReadMethod() {
094: try {
095: return this .getClass().getDeclaredMethod("getValue",
096: new Class[] { Object.class });
097: } catch (final Exception e) {
098: throw new RuntimeDroolsException(
099: "This is a bug. Please report to development team: "
100: + e.getMessage(), e);
101: }
102: }
103:
104: public int getHashCode(InternalWorkingMemory workingMemory,
105: final Object object) {
106: return getValue(workingMemory, object).hashCode();
107: }
108:
109: public boolean isGlobal() {
110: return false;
111: }
112:
113: public boolean isNullValue(InternalWorkingMemory workingMemory,
114: Object object) {
115: return ((Fact) object).getFieldValue(this.fieldIndex) == null;
116: }
117: }
|