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 boolean class field
028: * extractors. This class centralizes type conversions.
029: *
030: * @author etirelli
031: */
032: public abstract class BaseBooleanClassFieldExtractor extends
033: BaseClassFieldExtractor {
034:
035: private static final long serialVersionUID = 400L;
036:
037: public BaseBooleanClassFieldExtractor(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 BaseBooleanClassFieldExtractor(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 getBooleanValue(workingMemory, object) ? Boolean.TRUE
057: : Boolean.FALSE;
058: }
059:
060: public abstract boolean getBooleanValue(
061: InternalWorkingMemory workingMemory, Object object);
062:
063: public byte getByteValue(InternalWorkingMemory workingMemory,
064: final Object object) {
065: throw new RuntimeDroolsException(
066: "Conversion to byte not supported from boolean");
067: }
068:
069: public char getCharValue(InternalWorkingMemory workingMemory,
070: final Object object) {
071: throw new RuntimeDroolsException(
072: "Conversion to char not supported from boolean");
073: }
074:
075: public double getDoubleValue(InternalWorkingMemory workingMemory,
076: final Object object) {
077: throw new RuntimeDroolsException(
078: "Conversion to double not supported from boolean");
079: }
080:
081: public float getFloatValue(InternalWorkingMemory workingMemory,
082: final Object object) {
083: throw new RuntimeDroolsException(
084: "Conversion to float not supported from boolean");
085: }
086:
087: public int getIntValue(InternalWorkingMemory workingMemory,
088: final Object object) {
089: throw new RuntimeDroolsException(
090: "Conversion to int not supported from boolean");
091: }
092:
093: public long getLongValue(InternalWorkingMemory workingMemory,
094: final Object object) {
095: throw new RuntimeDroolsException(
096: "Conversion to long not supported from boolean");
097: }
098:
099: public short getShortValue(InternalWorkingMemory workingMemory,
100: final Object object) {
101: throw new RuntimeDroolsException(
102: "Conversion to short not supported from boolean");
103: }
104:
105: public boolean isNullValue(InternalWorkingMemory workingMemory,
106: final Object object) {
107: return false;
108: }
109:
110: public Method getNativeReadMethod() {
111: try {
112: return this .getClass().getDeclaredMethod(
113: "getBooleanValue",
114: new Class[] { InternalWorkingMemory.class,
115: Object.class });
116: } catch (final Exception e) {
117: throw new RuntimeDroolsException(
118: "This is a bug. Please report to development team: "
119: + e.getMessage(), e);
120: }
121: }
122:
123: public int getHashCode(InternalWorkingMemory workingMemory,
124: final Object object) {
125: return getBooleanValue(workingMemory, object) ? 1231 : 1237;
126: }
127:
128: }
|