001: package org.drools.base.field;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.Collection;
020:
021: import org.drools.RuntimeDroolsException;
022: import org.drools.spi.FieldValue;
023:
024: public class ObjectFieldImpl implements FieldValue {
025:
026: private static final long serialVersionUID = 400L;
027: private final Object value;
028:
029: private final boolean isCollection;
030: private final boolean isNumber;
031: private final boolean isBoolean;
032: private final boolean isCharacter;
033:
034: public ObjectFieldImpl(final Object value) {
035: this .value = value;
036: this .isCollection = value instanceof Collection;
037: this .isNumber = value instanceof Number;
038: this .isBoolean = value instanceof Boolean;
039: this .isCharacter = value instanceof Character;
040: }
041:
042: public Object getValue() {
043: return this .value;
044: }
045:
046: public String toString() {
047: return this .value == null ? "null" : this .value.toString();
048: }
049:
050: public boolean getBooleanValue() {
051: if (isBoolean) {
052: return ((Boolean) this .value).booleanValue();
053: }
054: throw new RuntimeDroolsException(
055: "Conversion to boolean not supported for type: "
056: + this .value.getClass());
057: }
058:
059: public byte getByteValue() {
060: if (isNumber) {
061: return ((Number) this .value).byteValue();
062: }
063: throw new RuntimeDroolsException(
064: "Conversion to byte not supported for type: "
065: + this .value.getClass());
066: }
067:
068: public char getCharValue() {
069: if (isCharacter) {
070: return ((Character) this .value).charValue();
071: }
072: throw new RuntimeDroolsException(
073: "Conversion to char not supported for type: "
074: + this .value.getClass());
075: }
076:
077: public double getDoubleValue() {
078: if (isNumber) {
079: return ((Number) this .value).doubleValue();
080: }
081: throw new RuntimeDroolsException(
082: "Conversion to double not supported for type: "
083: + this .value.getClass());
084: }
085:
086: public float getFloatValue() {
087: if (isNumber) {
088: return ((Number) this .value).floatValue();
089: }
090: throw new RuntimeDroolsException(
091: "Conversion to float not supported for type: "
092: + this .value.getClass());
093: }
094:
095: public int getIntValue() {
096: if (isNumber) {
097: return ((Number) this .value).intValue();
098: }
099: throw new RuntimeDroolsException(
100: "Conversion to int not supported for type: "
101: + this .value.getClass());
102: }
103:
104: public long getLongValue() {
105: if (isNumber) {
106: return ((Number) this .value).longValue();
107: }
108: throw new RuntimeDroolsException(
109: "Conversion to long not supported for type: "
110: + this .value.getClass());
111: }
112:
113: public short getShortValue() {
114: if (isNumber) {
115: return ((Number) this .value).shortValue();
116: }
117: throw new RuntimeDroolsException(
118: "Conversion to short not supported for type: "
119: + this .value.getClass());
120: }
121:
122: public boolean equals(final Object object) {
123: if (this == object) {
124: return true;
125: }
126: if (object == null || !(object instanceof ObjectFieldImpl)) {
127: return false;
128: }
129: final ObjectFieldImpl other = (ObjectFieldImpl) object;
130:
131: return (((this .value == null) && (other.value == null)) || ((this .value != null) && (this .value
132: .equals(other.value))));
133: }
134:
135: public int hashCode() {
136: if (this .value != null) {
137: return this .value.hashCode();
138: } else {
139: return 0;
140: }
141: }
142:
143: public boolean isNull() {
144: return value == null;
145: }
146:
147: public boolean isBooleanField() {
148: return false;
149: }
150:
151: public boolean isFloatNumberField() {
152: return false;
153: }
154:
155: public boolean isIntegerNumberField() {
156: return false;
157: }
158:
159: public boolean isObjectField() {
160: return true;
161: }
162:
163: public boolean isCollectionField() {
164: return this.isCollection;
165: }
166: }
|