/* 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 LongThing class represents a 'long' Thing.
*
* @version 1.0
*/
public class LongThing extends FractionalThing {
/* The internal value. */
private long val;
/**
* Creates a new LongThing instance equal to 0.
*
*/
public LongThing() {
this(0);
}
/**
* Creates a new LongThing instance with value l.
*
* @param l a long value
*/
public LongThing(long l) {
set(l);
}
/**
* Creates a new LongThing instance from boolean b
* where true is 1 and false is 0.
*
* @param b a boolean value
*/
public LongThing(boolean b) {
this(b == true ? 1 : 0);
}
/**
* Creates a new LongThing instance from string s.
*
* @param s a String value
* @throws NumberFormatException when argum,ent does not adhere to
* integral number syntax.
*/
public LongThing(String s) throws NumberFormatException {
this(Long.parseLong(s));
}
@Override
public String thingclass() {
return "long";
}
/**
* The create method creates and returns a newly allocated
* Thing with an LongThing internal representation.
*
* @param i an int value
* @return a Thing value
*/
public static Thing create(int i) {
return create((long)i);
}
/**
* The create method creates and returns a newly allocated
* Thing with an LongThing internal representation.
*
* @param l an long value
* @return a Thing value
*/
public static Thing create(long l) {
return new Thing(new LongThing(l));
}
/**
* The create method creates and returns a newly
* allocated Thing with an LongThing internal representation.
*
* @param b
* an boolean value
* @return a Thing value
*/
public static Thing create(boolean b) {
return create(b == true ? 1 : 0);
}
/**
* set transforms the given Thing into an LongThing,
* 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 LongThing)
return;
if(NumberThing.isNumber(thing)) {
// It's already a number
thing.setVal(new LongThing(((NumberThing)realthing).longValue()));
} else {
// If it's not a longthing already, we make it from its string rep.
thing.setVal(new LongThing(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 long get(Thing thing) throws HeclException {
set(thing);
return ((LongThing)thing.getVal()).longValue();
}
@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 val;
}
@Override
public float floatValue() {
return (float)val;
}
@Override
public double doubleValue() {
return (double)val;
}
public boolean isLong() {
return true;
}
/**
* set sets the internal value of an LongThing to l.
*
* @param l
* a long value
*/
public void set(long l) {
val = l;
}
/**
* deepcopy makes a copy.
*
* @return a RealThing value
*/
@Override
public RealThing deepcopy() {
return new LongThing(val);
}
/**
* getStringRep creates a string representation of the
* LongThing.
*
* @return a String value
*/
@Override
public String getStringRep() {
return Long.toString(val);
}
}
Ads help maintain this website.