001: /* Copyright 2004-2006 David N. Welton, 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>IntThing</code> class represents an integer Thing.
020: *
021: * @author <a href="mailto:wolfgang.kechel@data2c.com">Wolfgang S. Kechel</a>
022: * @version 1.0
023: */
024: public class IntThing extends IntegralThing {
025: public static IntThing ZERO = new IntThing(0);
026: public static IntThing ONE = new IntThing(1);
027: public static IntThing NEGONE = new IntThing(-1);
028:
029: /**
030: * Creates a new <code>IntThing</code> instance equal to 0.
031: *
032: */
033: public IntThing() {
034: val = 0;
035: }
036:
037: /**
038: * Creates a new <code>IntThing</code> instance with value i.
039: *
040: * @param i
041: * an <code>int</code> value
042: */
043: public IntThing(int i) {
044: val = i;
045: }
046:
047: /**
048: * Creates a new <code>IntThing</code> instance from boolean b where true
049: * is 1 and false is 0.
050: *
051: * @param b
052: * a <code>boolean</code> value
053: */
054: public IntThing(boolean b) {
055: val = b == true ? 1 : 0;
056: }
057:
058: /**
059: * Creates a new <code>IntThing</code> instance from string s.
060: *
061: * @param s
062: * a <code>String</code> value
063: */
064: public IntThing(String s) {
065: set(Integer.parseInt(s));
066: }
067:
068: public String thingclass() {
069: return "int";
070: }
071:
072: /**
073: * The <code>create</code> method creates and returns a newly allocated
074: * Thing with an IntThing internal representation.
075: *
076: * @param i
077: * an <code>int</code> value
078: * @return a <code>Thing</code> value
079: */
080: public static Thing create(int i) {
081: return new Thing(new IntThing(i));
082: }
083:
084: /**
085: * The <code>create</code> method creates and returns a newly allocated
086: * Thing with an IntThing internal representation.
087: *
088: * @param b
089: * an <code>boolean</code> value
090: * @return a <code>Thing</code> value
091: */
092: public static Thing create(boolean b) {
093: return new Thing(new IntThing(b));
094: }
095:
096: /**
097: * <code>setIntFromAny</code> transforms the given Thing into an IntThing,
098: * internally.
099: *
100: * @param thing
101: * a <code>Thing</code> value
102: * @exception HeclException
103: * if an error occurs
104: */
105: private static void set(Thing thing) throws HeclException {
106: RealThing realthing = thing.getVal();
107:
108: if (realthing instanceof IntThing)
109: return;
110:
111: if (NumberThing.isNumber(realthing)) {
112: // It's already a number
113: thing.setVal(new IntThing(((NumberThing) realthing)
114: .intValue()));
115: } else {
116: /* If it's not an intthing already, we make it from its
117: * string rep. */
118: thing.setVal(new IntThing(thing.toString()));
119: }
120: }
121:
122: /**
123: * <code>get</code> attempts to fetch an integer value from a Thing.
124: *
125: * @param thing
126: * a <code>Thing</code> value
127: * @return an <code>int</code> value
128: * @exception HeclException
129: * if an error occurs
130: */
131: public static int get(Thing thing) throws HeclException {
132: return NumberThing.asNumber(thing).intValue();
133: }
134:
135: public byte byteValue() {
136: return (byte) val;
137: }
138:
139: public short shortValue() {
140: return (short) val;
141: }
142:
143: public int intValue() {
144: return val;
145: }
146:
147: public long longValue() {
148: return (long) val;
149: }
150:
151: public float floatValue() {
152: return (float) val;
153: }
154:
155: public double doubleValue() {
156: return (double) val;
157: }
158:
159: /**
160: * <code>set</code> sets the internal value of an IntThing to i.
161: *
162: * @param i
163: * an <code>int</code> value
164: */
165: public void set(int i) {
166: val = i;
167: }
168:
169: /**
170: * <code>deepcopy</code> makes a copy.
171: *
172: * @return a <code>RealThing</code> value
173: */
174: public RealThing deepcopy() {
175: return new IntThing(val);
176: }
177:
178: /**
179: * <code>getStringRep</code> creates a string representation of the
180: * IntThing.
181: *
182: * @return a <code>String</code> value
183: */
184: public String getStringRep() {
185: return Integer.toString(val);
186: }
187:
188: private int val;
189: }
|