001: /* Copyright 2006 Wolfgang S. Kechel
002:
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014: */
015:
016: package org.hecl;
017:
018: /**
019: * The <code>DoubleThing</code> class represents a Thing that contains
020: * a double value.
021: */
022: public//#if cldc == 1.0
023: abstract//#endif
024: class DoubleThing extends FractionalThing {
025: public String thingclass() {
026: return "double";
027: }
028:
029: /**
030: * Creates a new <code>DoubleThing</code> instance equal to 0.
031: *
032: */
033: //#if javaversion >= 1.5 || cldc > 1.0
034: public DoubleThing() {
035: set(0.0);
036: }
037:
038: /**
039: * Creates a new <code>DoubleThing</code> instance with value i.
040: *
041: * @param d
042: * a <code>double</code> value
043: */
044: public DoubleThing(double d) {
045: set(d);
046: }
047:
048: /**
049: * Creates a new <code>DoubleThing</code> instance from boolean b where true
050: * is 1 and false is 0.
051: *
052: * @param b
053: * a <code>boolean</code> value
054: */
055: public DoubleThing(boolean b) {
056: set(b == true ? 1.0 : 0.0);
057: }
058:
059: /**
060: * Creates a new <code>DoubleThing</code> instance from string s.
061: *
062: * @param s
063: * a <code>String</code> value
064: */
065: public DoubleThing(String s) {
066: set(Double.parseDouble(s));
067: }
068:
069: /**
070: * The <code>create</code> method creates and returns a newly allocated
071: * Thing with a DoubleThing internal representation.
072: *
073: * @param d
074: * a <code>double</code> value
075: * @return a <code>Thing</code> value
076: */
077: public static Thing create(double d) {
078: return new Thing(new DoubleThing(d));
079: }
080:
081: /**
082: * The <code>create</code> method creates and returns a newly allocated
083: * Thing with a DoubleThing internal representation.
084: *
085: * @param b
086: * an <code>boolean</code> value
087: * @return a <code>Thing</code> value
088: */
089: public static Thing create(boolean b) {
090: return new Thing(new DoubleThing(b));
091: }
092:
093: /**
094: * <code>set</code> transforms the given Thing into a DoubleThing,
095: * internally.
096: *
097: * @param thing
098: * a <code>Thing</code> value
099: * @exception HeclException
100: * if an error occurs
101: */
102: private static void set(Thing thing) throws HeclException {
103: RealThing realthing = thing.getVal();
104:
105: if (realthing instanceof DoubleThing)
106: return;
107:
108: if (NumberThing.isNumber(realthing)) {
109: // It's already a number
110: thing.setVal(new DoubleThing(((NumberThing) realthing)
111: .doubleValue()));
112: } else {
113: /* Otherwise, try and parse the string representation. */
114: thing.setVal(new DoubleThing(thing.toString()));
115: }
116: }
117:
118: /**
119: * <code>get</code> attempts to fetch a double value from a Thing.
120: *
121: * @param thing
122: * a <code>Thing</code> value
123: * @return a <code>double</code> value
124: * @exception HeclException
125: * if an error occurs
126: */
127: public static double get(Thing thing) throws HeclException {
128: set(thing);
129: return ((DoubleThing) thing.getVal()).doubleValue();
130: }
131:
132: public byte byteValue() {
133: return (byte) val;
134: }
135:
136: public short shortValue() {
137: return (short) val;
138: }
139:
140: public int intValue() {
141: return (int) val;
142: }
143:
144: public long longValue() {
145: return (long) val;
146: }
147:
148: public float floatValue() {
149: return (float) val;
150: }
151:
152: public double doubleValue() {
153: return (double) val;
154: }
155:
156: /**
157: * <code>set</code> sets the internal value of a DoubleThing to i.
158: *
159: * @param d
160: * a <code>double</code> value
161: */
162: public void set(double d) {
163: val = d;
164: }
165:
166: /**
167: * <code>deepcopy</code> makes a copy.
168: *
169: * @return a <code>RealThing</code> value
170: */
171: public RealThing deepcopy() {
172: return new DoubleThing(val);
173: }
174:
175: /**
176: * <code>getStringRep</code> creates a string representation of the
177: * DoubleThing.
178: *
179: * @return a <code>String</code> value
180: */
181: public String getStringRep() {
182: return Double.toString(val);
183: }
184:
185: private double val;
186: //#endif
187: }
|