/* Copyright 2006 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 DoubleThing class represents a Thing that contains
 * a double value.
 */
public
class DoubleThing extends FractionalThing {
    @Override
    public String thingclass() {
	return "double";
    }

    /**
     * Creates a new DoubleThing instance equal to 0.
     *
     */
//#if javaversion >= 1.5 || cldc > 1.0
    public DoubleThing() {
	set(0.0);
    }

    /**
     * Creates a new DoubleThing instance with value i.
     * 
     * @param d
     *            a double value
     */
    public DoubleThing(double d) {
	set(d);
    }

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

    /**
     * Creates a new DoubleThing instance from string s.
     * 
     * @param s a String value
     * @throws NumberFormatException when argum,ent does not adhere to
     * floating point number syntax.
     */
    public DoubleThing(String s) throws NumberFormatException {
        set(Double.parseDouble(s));
    }

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

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

    /**
     * set transforms the given Thing into a DoubleThing,
     * 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 DoubleThing)
	    return;

	if(NumberThing.isNumber(realthing)) {
	    // It's already a number
	    thing.setVal(new DoubleThing(((NumberThing)realthing).doubleValue()));
	} else {
	    /* Otherwise, try and parse the string representation. */
            thing.setVal(new DoubleThing(thing.toString()));
	}
    }

    /**
     * get attempts to fetch a double value from a Thing.
     *
     * @param thing
     *            a Thing value
     * @return a double value
     * @exception HeclException
     *                if an error occurs
     */
    public static double get(Thing thing) throws HeclException {
        set(thing);
	return ((DoubleThing)thing.getVal()).doubleValue();
    }


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

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

    @Override
    public int intValue() {
	return (int)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 a DoubleThing to i.
     * 
     * @param d
     *            a double value
     */
    public void set(double d) {
        val = d;
    }

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

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


    private double val;
//#endif
}

 

Ads help maintain this website.