001: /*
002: * Copyright 2005 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.base.extractors;
018:
019: import java.lang.reflect.Method;
020:
021: import org.drools.RuntimeDroolsException;
022: import org.drools.base.BaseClassFieldExtractor;
023: import org.drools.base.ValueType;
024: import org.drools.common.InternalWorkingMemory;
025:
026: /**
027: * A Base class for primitive byte class field
028: * extractors. This class centralizes type conversions.
029: *
030: * @author etirelli
031: */
032: public abstract class BaseByteClassFieldExtractor extends
033: BaseClassFieldExtractor {
034:
035: private static final long serialVersionUID = 400L;
036:
037: public BaseByteClassFieldExtractor(final Class clazz,
038: final String fieldName) {
039: super (clazz, fieldName);
040: }
041:
042: /**
043: * This constructor is not supposed to be used from outside the class hirarchy
044: *
045: * @param index
046: * @param fieldType
047: * @param valueType
048: */
049: protected BaseByteClassFieldExtractor(final int index,
050: final Class fieldType, final ValueType valueType) {
051: super (index, fieldType, valueType);
052: }
053:
054: public Object getValue(InternalWorkingMemory workingMemory,
055: final Object object) {
056: return new Long(getByteValue(workingMemory, object));
057: }
058:
059: public boolean getBooleanValue(InternalWorkingMemory workingMemory,
060: final Object object) {
061: throw new RuntimeDroolsException(
062: "Conversion to boolean not supported from byte");
063: }
064:
065: public abstract byte getByteValue(
066: InternalWorkingMemory workingMemory, Object object);
067:
068: public char getCharValue(InternalWorkingMemory workingMemory,
069: final Object object) {
070: throw new RuntimeDroolsException(
071: "Conversion to char not supported from byte");
072: }
073:
074: public double getDoubleValue(InternalWorkingMemory workingMemory,
075: final Object object) {
076: return getByteValue(workingMemory, object);
077: }
078:
079: public float getFloatValue(InternalWorkingMemory workingMemory,
080: final Object object) {
081: return getByteValue(workingMemory, object);
082: }
083:
084: public int getIntValue(InternalWorkingMemory workingMemory,
085: final Object object) {
086: return getByteValue(workingMemory, object);
087: }
088:
089: public long getLongValue(InternalWorkingMemory workingMemory,
090: final Object object) {
091: return getByteValue(workingMemory, object);
092: }
093:
094: public short getShortValue(InternalWorkingMemory workingMemory,
095: final Object object) {
096: return getByteValue(workingMemory, object);
097: }
098:
099: public boolean isNullValue(InternalWorkingMemory workingMemory,
100: final Object object) {
101: return false;
102: }
103:
104: public Method getNativeReadMethod() {
105: try {
106: return this .getClass().getDeclaredMethod(
107: "getByteValue",
108: new Class[] { InternalWorkingMemory.class,
109: Object.class });
110: } catch (final Exception e) {
111: throw new RuntimeDroolsException(
112: "This is a bug. Please report to development team: "
113: + e.getMessage(), e);
114: }
115: }
116:
117: public int getHashCode(InternalWorkingMemory workingMemory,
118: final Object object) {
119: return getByteValue(workingMemory, object);
120: }
121: }
|