initial load
[DTRules.git] / DTRules / src / main / java / com / dtrules / interpreter / RDouble.java
blobee8ef3c6cd6a163a27ead2a1f31527edd3efde84
1 /*
2 * $Id$
3 *
4 * Copyright 2004-2007 MTBJ, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
19 package com.dtrules.interpreter;
21 import com.dtrules.infrastructure.RulesException;
23 /**
24 * @author Paul Snow
27 public class RDouble extends ARObject {
28 final double value;
30 static RDouble mone = new RDouble(-1.0);
31 static RDouble zero = new RDouble(0.0);
32 static RDouble one = new RDouble(1.0);
34 private RDouble(double v){
35 value = v;
38 static public double getDoubleValue(String s) throws RulesException {
39 if(s==null || s.trim().length()==0){
40 return 0.0D;
42 try {
43 double v = Double.parseDouble(s);
44 return v;
45 } catch (NumberFormatException e) {
46 throw new RulesException("Conversion Error","RDouble.getDoubleValue()","Could not covert the string '"+s+"' to a double: "+e.getMessage());
49 /**
50 * Parses the given string, and returns an RDouble Object
51 * @param s String to parse
52 * @return RDouble value for the given string
53 * @throws RulesException
55 static public RDouble getRDoubleValue(String s) throws RulesException {
56 return getRDoubleValue(getDoubleValue(s));
61 static public RDouble getRDoubleValue(double v){
62 if(v == -1.0) return mone;
63 if(v == 0.0) return zero;
64 if(v == 1.0) return one;
66 return new RDouble(v);
69 static public RDouble getRDoubleValue(int v){
70 return getRDoubleValue((double) v);
73 static public RDouble getRDoubleValue(long v){
74 return getRDoubleValue((double) v);
77 public RDouble rDoubleValue() {
78 return this;
81 public double doubleValue() {
82 return value;
85 public int intValue() {
86 return (int) value;
90 public long longValue() {
91 return (long) value;
94 public RInteger rIntegerValue() {
95 return RInteger.getRIntegerValue((long)value);
98 /**
99 * Returns the type for this object.
101 public int type() {
102 return iDouble;
105 public String stringValue() {
106 return Double.toString(value);
110 * returns 0 if both are equal. -1 if this object is less than the argument.
111 * 1 if this object is greater than the argument
113 public int compare(IRObject irObject) throws RulesException {
114 return (this.value==irObject.doubleValue())?0:((this.value<irObject.doubleValue())?-1:0);
117 @Override
118 public boolean equals(IRObject o) throws RulesException {
119 return value == o.doubleValue();
122 public String toString() {
123 return Double.toString(value);