/* Copyright 2004-2006 David N. Welton, Wolfgang S. Kechel

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

package org.hecl;

/**
 * The IntThing class represents an integer Thing.
 *
 * @author Wolfgang S. Kechel
 * @version 1.0
 */
public class IntThing extends IntegralThing {
    public static IntThing ZERO = new IntThing(0);
    public static IntThing ONE = new IntThing(1);
    public static IntThing NEGONE = new IntThing(-1);

    /**
     * Creates a new IntThing instance equal to 0.
     *
     */
    public IntThing() {
	val = 0;
    }

    /**
     * Creates a new IntThing instance with value i.
     *
     * @param i
     *            an int value
     */
    public IntThing(int i) {
	val = i;
    }

    /**
     * Creates a new IntThing instance from boolean b where true
     * is 1 and false is 0.
     *
     * @param b
     *            a boolean value
     */
    public IntThing(boolean b) {
        val = b == true ? 1 : 0;
    }

    /**
     * Creates a new IntThing instance from string s.
     *
     * @param s
     *            a String value
     */
    public IntThing(String s) {
        set(Integer.parseInt(s));
    }

    @Override
    public String thingclass() {
	return "int";
    }

    /**
     * The create method creates and returns a newly allocated
     * Thing with an IntThing internal representation.
     *
     * @param i
     *            an int value
     * @return a Thing value
     */
    public static Thing create(int i) {
        return new Thing(new IntThing(i));
    }

    /**
     * The create method creates and returns a newly allocated
     * Thing with an IntThing internal representation.
     *
     * @param b
     *            an boolean value
     * @return a Thing value
     */
    public static Thing create(boolean b) {
        return new Thing(new IntThing(b));
    }

    /**
     * setIntFromAny transforms the given Thing into an IntThing,
     * internally.
     *
     * @param thing
     *            a Thing value
     * @exception HeclException
     *                if an error occurs
     */
    private static void set(Thing thing) throws HeclException {
        RealThing realthing = thing.getVal();

        if (realthing instanceof IntThing)
	    return;

	if(NumberThing.isNumber(realthing)) {
	    // It's already a number
	    thing.setVal(new IntThing(((NumberThing)realthing).intValue()));
	} else {
            /* If it's not an intthing already, we make it from its
	     * string rep. */
            thing.setVal(new IntThing(thing.toString()));
        }
    }

    /**
     * get attempts to fetch an integer value from a Thing.
     *
     * @param thing
     *            a Thing value
     * @return an int value
     * @exception HeclException
     *                if an error occurs
     */
    public static int get(Thing thing) throws HeclException {
        return NumberThing.asNumber(thing).intValue();
    }

    @Override
    public byte byteValue() {
	return (byte)val;
    }

    @Override
    public short shortValue() {
	return (short)val;
    }

    @Override
    public int intValue() {
	return val;
    }

    @Override
    public long longValue() {
	return (long)val;
    }

    @Override
    public float floatValue() {
	return (float)val;
    }

    @Override
    public double doubleValue() {
	return (double)val;
    }

    /**
     * set sets the internal value of an IntThing to i.
     *
     * @param i
     *            an int value
     */
    public void set(int i) {
        val = i;
    }

    /**
     * deepcopy makes a copy.
     *
     * @return a RealThing value
     */
    @Override
    public RealThing deepcopy() {
        return new IntThing(val);
    }

    /**
     * getStringRep creates a string representation of the
     * IntThing.
     *
     * @return a String value
     */
    @Override
    public String getStringRep() {
        return Integer.toString(val);
    }

    private int val;
}

 

Ads help maintain this website.