001: package org.drools.base;
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.math.BigDecimal;
020: import java.math.BigInteger;
021:
022: import org.drools.base.field.BooleanFieldImpl;
023: import org.drools.base.field.DoubleFieldImpl;
024: import org.drools.base.field.LongFieldImpl;
025: import org.drools.base.field.ObjectFieldImpl;
026: import org.drools.spi.FieldValue;
027:
028: public class FieldFactory {
029: private static final FieldFactory INSTANCE = new FieldFactory();
030:
031: public static FieldFactory getInstance() {
032: return FieldFactory.INSTANCE;
033: }
034:
035: private FieldFactory() {
036:
037: }
038:
039: public static FieldValue getFieldValue(final String value,
040: ValueType valueType) {
041: FieldValue field = null;
042: if (value == null) {
043: valueType = ValueType.NULL_TYPE;
044: }
045:
046: if (valueType == ValueType.NULL_TYPE) {
047: field = new ObjectFieldImpl(null);
048: } else if (valueType == ValueType.PCHAR_TYPE) {
049: field = new LongFieldImpl(value.charAt(0));
050: } else if (valueType == ValueType.PBYTE_TYPE) {
051: field = new LongFieldImpl(Long.parseLong(value));
052: } else if (valueType == ValueType.PSHORT_TYPE) {
053: field = new LongFieldImpl(Long.parseLong(value));
054: } else if (valueType == ValueType.PINTEGER_TYPE) {
055: field = new LongFieldImpl(Long
056: .parseLong(stripNumericType(value)));
057: } else if (valueType == ValueType.PLONG_TYPE) {
058: field = new LongFieldImpl(Long
059: .parseLong(stripNumericType(value)));
060: } else if (valueType == ValueType.PFLOAT_TYPE) {
061: field = new DoubleFieldImpl(Float
062: .parseFloat(stripNumericType(value)));
063: } else if (valueType == ValueType.PDOUBLE_TYPE) {
064: field = new DoubleFieldImpl(Double
065: .parseDouble(stripNumericType(value)));
066: } else if (valueType == ValueType.PBOOLEAN_TYPE) {
067: field = new BooleanFieldImpl(Boolean.valueOf(value)
068: .booleanValue());
069: } else if (valueType == ValueType.CHAR_TYPE) {
070: field = new ObjectFieldImpl(new Character(value.charAt(0)));
071: } else if (valueType == ValueType.BYTE_TYPE) {
072: field = new ObjectFieldImpl(new Byte(value));
073: } else if (valueType == ValueType.SHORT_TYPE) {
074: field = new ObjectFieldImpl(new Short(value));
075: } else if (valueType == ValueType.INTEGER_TYPE) {
076: field = new ObjectFieldImpl(new Integer(
077: stripNumericType(value)));
078: } else if (valueType == ValueType.LONG_TYPE) {
079: field = new ObjectFieldImpl(new Long(
080: stripNumericType(value)));
081: } else if (valueType == ValueType.FLOAT_TYPE) {
082: field = new ObjectFieldImpl(new Float(
083: stripNumericType(value)));
084: } else if (valueType == ValueType.DOUBLE_TYPE) {
085: field = new ObjectFieldImpl(new Double(
086: stripNumericType(value)));
087: } else if (valueType == ValueType.BOOLEAN_TYPE) {
088: field = new ObjectFieldImpl(Boolean.valueOf(value));
089: } else if (valueType == ValueType.STRING_TYPE) {
090: field = new ObjectFieldImpl(value.intern());
091: } else if (valueType == ValueType.DATE_TYPE) {
092: //MN: I think its fine like this, seems to work !
093: field = new ObjectFieldImpl(value);
094: } else if (valueType == ValueType.ARRAY_TYPE) {
095: //MN: I think its fine like this.
096: field = new ObjectFieldImpl(value);
097: } else if (valueType == ValueType.OBJECT_TYPE) {
098: field = new ObjectFieldImpl(value);
099: } else if (valueType == ValueType.BIG_DECIMAL_TYPE) {
100: field = new ObjectFieldImpl(new BigDecimal(value));
101: } else if (valueType == ValueType.BIG_INTEGER_TYPE) {
102: field = new ObjectFieldImpl(new BigInteger(value));
103: }
104:
105: return field;
106: }
107:
108: public static FieldValue getFieldValue(final Object value,
109: ValueType valueType) {
110: FieldValue field = null;
111: if (value == null) {
112: valueType = ValueType.NULL_TYPE;
113: }
114:
115: if (valueType == ValueType.NULL_TYPE) {
116: field = new ObjectFieldImpl(null);
117: } else if (valueType == ValueType.PCHAR_TYPE) {
118: field = new LongFieldImpl(((Character) value).charValue());
119: } else if (valueType == ValueType.PBYTE_TYPE) {
120: field = new LongFieldImpl(((Number) value).byteValue());
121: } else if (valueType == ValueType.PSHORT_TYPE) {
122: field = new LongFieldImpl(((Number) value).shortValue());
123: } else if (valueType == ValueType.PINTEGER_TYPE) {
124: field = new LongFieldImpl(((Number) value).intValue());
125: } else if (valueType == ValueType.PLONG_TYPE) {
126: field = new LongFieldImpl(((Number) value).longValue());
127: } else if (valueType == ValueType.PFLOAT_TYPE) {
128: field = new DoubleFieldImpl(((Number) value).floatValue());
129: } else if (valueType == ValueType.PDOUBLE_TYPE) {
130: field = new DoubleFieldImpl(((Number) value).doubleValue());
131: } else if (valueType == ValueType.PBOOLEAN_TYPE) {
132: field = new BooleanFieldImpl(((Boolean) value)
133: .booleanValue());
134: } else if (valueType == ValueType.CHAR_TYPE) {
135: field = new ObjectFieldImpl(value);
136: } else if (valueType == ValueType.BYTE_TYPE) {
137: field = new ObjectFieldImpl(value);
138: } else if (valueType == ValueType.SHORT_TYPE) {
139: field = new ObjectFieldImpl(value);
140: } else if (valueType == ValueType.INTEGER_TYPE) {
141: field = new ObjectFieldImpl(value);
142: } else if (valueType == ValueType.LONG_TYPE) {
143: field = new ObjectFieldImpl(value);
144: } else if (valueType == ValueType.FLOAT_TYPE) {
145: field = new ObjectFieldImpl(value);
146: } else if (valueType == ValueType.DOUBLE_TYPE) {
147: field = new ObjectFieldImpl(value);
148: } else if (valueType == ValueType.BOOLEAN_TYPE) {
149: field = new ObjectFieldImpl(value);
150: } else if (valueType == ValueType.STRING_TYPE) {
151: field = new ObjectFieldImpl(value);
152: } else if (valueType == ValueType.DATE_TYPE) {
153: //MN: I think its fine like this, seems to work !
154: field = new ObjectFieldImpl(value);
155: } else if (valueType == ValueType.ARRAY_TYPE) {
156: //MN: I think its fine like this.
157: field = new ObjectFieldImpl(value);
158: } else if (valueType == ValueType.OBJECT_TYPE) {
159: field = new ObjectFieldImpl(value);
160: } else if (valueType == ValueType.BIG_DECIMAL_TYPE) {
161: field = new ObjectFieldImpl(value);
162: } else if (valueType == ValueType.BIG_INTEGER_TYPE) {
163: field = new ObjectFieldImpl(value);
164: }
165:
166: return field;
167: }
168:
169: public static FieldValue getFieldValue(final Object value) {
170: return new ObjectFieldImpl(value);
171: }
172:
173: public static FieldValue getFieldValue(final byte value) {
174: return new LongFieldImpl(value);
175: }
176:
177: public static FieldValue getFieldValue(final short value) {
178: return new LongFieldImpl(value);
179: }
180:
181: public static FieldValue getFieldValue(final char value) {
182: return new LongFieldImpl(value);
183: }
184:
185: public static FieldValue getFieldValue(final int value) {
186: return new LongFieldImpl(value);
187: }
188:
189: public static FieldValue getFieldValue(final long value) {
190: return new LongFieldImpl(value);
191: }
192:
193: public static FieldValue getFieldValue(final boolean value) {
194: return new BooleanFieldImpl(value);
195: }
196:
197: public static FieldValue getFieldValue(final float value) {
198: return new DoubleFieldImpl(value);
199: }
200:
201: public static FieldValue getFieldValue(final double value) {
202: return new DoubleFieldImpl(value);
203: }
204:
205: private static String stripNumericType(String value) {
206: // incase a user adds a f or l, strip it as its not needed
207: if (Character.getType(value.charAt(value.length() - 1)) != Character.DECIMAL_DIGIT_NUMBER) {
208: value = value.substring(0, value.length() - 1);
209: }
210:
211: return value;
212: }
213:
214: }
|