001: package org.drools.base.extractors;
002:
003: import java.lang.reflect.Method;
004:
005: import org.drools.base.ValueType;
006: import org.drools.common.InternalWorkingMemory;
007: import org.drools.spi.Extractor;
008: import org.drools.util.ClassUtils;
009:
010: public class ArrayExtractor implements Extractor {
011: private final Extractor arrayExtractor;
012: private final int index;
013: private final Class type;
014:
015: public ArrayExtractor(Extractor arrayExtractor, int index,
016: Class type) {
017: this .arrayExtractor = arrayExtractor;
018: this .index = index;
019: this .type = type;
020: }
021:
022: public Class getExtractToClass() {
023: return type;
024: }
025:
026: public String getExtractToClassName() {
027: return ClassUtils.canonicalName(type);
028: }
029:
030: public boolean getBooleanValue(InternalWorkingMemory workingMemory,
031: Object object) {
032: Object[] array = (Object[]) this .arrayExtractor.getValue(
033: workingMemory, object);
034: return ((Boolean) array[this .index]).booleanValue();
035: }
036:
037: public byte getByteValue(InternalWorkingMemory workingMemory,
038: Object object) {
039: Object[] array = (Object[]) this .arrayExtractor.getValue(
040: workingMemory, object);
041: return ((Byte) array[this .index]).byteValue();
042: }
043:
044: public char getCharValue(InternalWorkingMemory workingMemory,
045: Object object) {
046: Object[] array = (Object[]) this .arrayExtractor.getValue(
047: workingMemory, object);
048: return ((Character) array[this .index]).charValue();
049: }
050:
051: public double getDoubleValue(InternalWorkingMemory workingMemory,
052: Object object) {
053: Object[] array = (Object[]) this .arrayExtractor.getValue(
054: workingMemory, object);
055: return ((Double) array[this .index]).doubleValue();
056: }
057:
058: public float getFloatValue(InternalWorkingMemory workingMemory,
059: Object object) {
060: Object[] array = (Object[]) this .arrayExtractor.getValue(
061: workingMemory, object);
062: return ((Float) array[this .index]).floatValue();
063: }
064:
065: public int getIntValue(InternalWorkingMemory workingMemory,
066: Object object) {
067: Object[] array = (Object[]) this .arrayExtractor.getValue(
068: workingMemory, object);
069: return ((Integer) array[this .index]).intValue();
070: }
071:
072: public long getLongValue(InternalWorkingMemory workingMemory,
073: Object object) {
074: Object[] array = (Object[]) this .arrayExtractor.getValue(
075: workingMemory, object);
076: return ((Long) array[this .index]).longValue();
077: }
078:
079: public Method getNativeReadMethod() {
080: throw new UnsupportedOperationException(
081: "cannot call a method on an array extractor");
082: }
083:
084: public short getShortValue(InternalWorkingMemory workingMemory,
085: Object object) {
086: Object[] array = (Object[]) this .arrayExtractor.getValue(
087: workingMemory, object);
088: return ((Short) array[this .index]).shortValue();
089: }
090:
091: public Object getValue(InternalWorkingMemory workingMemory,
092: Object object) {
093: Object[] array = (Object[]) this .arrayExtractor.getValue(
094: workingMemory, object);
095: return array[this .index];
096: }
097:
098: public ValueType getValueType() {
099: return ValueType.OBJECT_TYPE;
100: }
101:
102: public boolean isNullValue(InternalWorkingMemory workingMemory,
103: Object object) {
104: Object[] array = (Object[]) this .arrayExtractor.getValue(
105: workingMemory, object);
106: return array[this .index] == null;
107: }
108:
109: public int getHashCode(InternalWorkingMemory workingMemory,
110: Object object) {
111: Object[] array = (Object[]) this .arrayExtractor.getValue(
112: workingMemory, object);
113: return array[this .index].hashCode();
114: }
115:
116: public int hashCode() {
117: final int PRIME = 31;
118: int result = 1;
119: result = PRIME
120: * result
121: + ((arrayExtractor == null) ? 0 : arrayExtractor
122: .hashCode());
123: result = PRIME * result + index;
124: return result;
125: }
126:
127: public boolean equals(Object obj) {
128: if (this == obj)
129: return true;
130: if (obj == null)
131: return false;
132: if (getClass() != obj.getClass())
133: return false;
134: final ArrayExtractor other = (ArrayExtractor) obj;
135: if (arrayExtractor == null) {
136: if (other.arrayExtractor != null)
137: return false;
138: } else if (!arrayExtractor.equals(other.arrayExtractor))
139: return false;
140: if (index != other.index)
141: return false;
142: return true;
143: }
144:
145: public boolean isGlobal() {
146: return false;
147: }
148: }
|