/* 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 NumberThing class is what all numeric Thing types
 * are derived from.
 *
 * @author David N. Welton
 * @version 1.0
 */
public abstract class NumberThing implements RealThing {
    // Satisfy RealThing...
    @Override
    public abstract RealThing deepcopy() throws HeclException;
    @Override
    public abstract String getStringRep();

    /**
     * isNumber returns true if Thing t is represented as
     * a number internally.
     *
     * @param t a Thing value
     * @return a boolean value
     */
    public static boolean isNumber(Thing t) {
	return t.getVal() instanceof NumberThing;
    }

    public static boolean isNumber(RealThing rt) {
	return rt instanceof NumberThing;
    }
    
    /**
     * isIntegral returns true if Thing t is a whole
     * number (an int or long), or false if it isn't.
     *
     * @param t a Thing value
     * @return a boolean value
     */
    public static boolean isIntegral(Thing t) {
	RealThing rt = t.getVal();
	return isNumber(rt) && ((NumberThing)rt).isIntegral();
    }


    /**
     * isFractional returns true if Thing t is a
     * fractional (floating point) number, false if it isn't.
     *
     * @param t a Thing value
     * @return a boolean value
     */
    public static boolean isFractional(Thing t) {
	RealThing rt = t.getVal();
	return isNumber(rt) && ((NumberThing)rt).isFractional();
    }

    public abstract boolean isIntegral();
    public abstract boolean isFractional();


    /**
     * byteValue returns the number as a byte.
     *
     * @return a byte value
     */
    public abstract byte byteValue();


    /**
     * shortValue returns the number as a short.
     *
     * @return a short value
     */
    public abstract short shortValue();

    /**
     * intValue returns the number as an int.
     *
     * @return an int value
     */
    public abstract int intValue();


    /**
     * longValue returns the value as a long.
     *
     * @return a long value
     */
    public abstract long longValue();
//#if javaversion >= 1.5 || cldc > 1.0

    /**
     * floatValue returns the value as a float.  Not
     * available in CLDC 1.0
     *
     * @return a float value
     */
    public abstract float floatValue();


    /**
     * doubleValue returns the value as a double.  Not
     * available in CLDC 1.0
     *
     * @return a double value
     */
    public abstract double doubleValue();
//#endif

    /**
     * asNumber attempts to transform Thing t into a
     * number of some sort.  It first tries to get an int from t, then
     * a long, and finally a double.
     *
     * @param t a Thing value
     * @return a NumberThing value
     * @throws NumberFormatException when argument does not adhere to number
     * format syntax.
     */
    public static NumberThing asNumber(Thing t)
	throws NumberFormatException {
	if(isNumber(t))
	    return (NumberThing)(t.getVal());
	String s = t.toString();
	try {
	    return new IntThing(s);
	}
	catch(NumberFormatException ne) {
	    try {
		return new LongThing(s);
	    }
	    catch(NumberFormatException ne2) {
		return new DoubleThing(s);
	    }
	}
    }

    /**
     * The create method creates a new Thing from
     * NumberThing n.
     *
     * @param n a NumberThing value
     * @return a Thing value
     */
    public static Thing create(NumberThing n) {
	return new Thing(n);
    }
}

 

Ads help maintain this website.