initial load
[DTRules.git] / DTRules / src / main / java / com / dtrules / interpreter / RInteger.java
blob1092059a8ec88af59ebadccb6179ae13ca64d5e3
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 * Integers are longs.
25 * @author ps24876
28 public class RInteger extends ARObject {
29 final long value;
31 static RInteger mone = new RInteger(-1);
32 static RInteger zero = new RInteger( 0);
33 static RInteger one = new RInteger( 1);
35 private RInteger(long i){
36 value = i;
39 /**
40 * Parses the given string, and returns an RInteger Object
41 * @param s String to parse
42 * @return RInteger value for the given string
43 * @throws RulesException
45 static public long getIntegerValue(String s) throws RulesException {
46 if(s==null || (s = s.trim()).length()==0 || s.equalsIgnoreCase("null")){
47 return 0L;
49 try {
50 long v = Long.parseLong(s);
51 return v;
52 } catch (NumberFormatException e) {
53 throw new RulesException("Conversion Error","RInteger.getIntegerValue()","Could not covert the string '"+s+"' to an integer: "+e.getMessage());
57 static public RInteger getRIntegerValue(String s) throws RulesException {
58 return getRIntegerValue(getIntegerValue(s));
61 static public RInteger getRIntegerValue(long i){
63 if(i == -1) return mone;
64 if(i == 0) return zero;
65 if(i == 1) return one;
66 return new RInteger(i);
69 static public RInteger getRIntegerValue(double i){
70 return getRIntegerValue((long) i);
73 static public RInteger getRIntegerValue(int i){
74 return getRIntegerValue((long) i);
77 public int type() {
78 return iInteger;
81 public double doubleValue() {
82 return (double)value;
85 public int intValue() {
86 return (int)value;
89 public long longValue() throws RulesException {
90 return (long) value;
93 public boolean equals(IRObject o) throws RulesException {
94 return value == o.intValue();
97 public String postFix() {
98 return stringValue();
101 public RInteger rIntegerValue() throws RulesException {
102 return this;
105 public String stringValue() {
106 String str = Long.toString(value);
107 return str;
110 public String toString(){
111 return stringValue();
115 * returns 0 if both are equal. -1 if this object is less than the argument.
116 * 1 if this object is greater than the argument
118 public int compare(IRObject irObject) throws RulesException {
119 return (this.value==irObject.intValue())?0:((this.value<irObject.intValue())?-1:0);