/* --------------------------------------------------------- *
* __________ D E L T A S C R I P T *
* (_________() *
* / === / - A fast, dynamic scripting language *
* | == | - Version 4.13.11.0 *
* / === / - Developed by Adam R. Nelson *
* | = = | - 2011-2013 *
* / === / - Distributed under GNU LGPL v3 *
* (________() - http://github.com/ar-nelson/deltascript *
* *
* --------------------------------------------------------- */
package com.sector91.delta.script.objects;
import com.sector91.delta.script.DScriptErr;
import com.sector91.delta.script.DeltaScript;
import com.sector91.delta.script.annotations.*;
import com.sector91.delta.script.objects.reflect.DS_JavaClass;
/**
* <p>A DeltaScript integer value, of arbitrary precision.</p>
*
* @author Adam R. Nelson
* @version 4.13.11.0
*/
@DSType("Integer")
public abstract class DS_Integer extends DS_Scalar
{
private static final long serialVersionUID =
DeltaScript.VERSION.majorVersion();
public static final String TYPE_NAME = "Integer";
private static final DS_JavaClass DSCLASS = DS_JavaClass.fromClass(
DS_Integer.class);
@DSInaccessible
@Override public abstract DS_Integer random();
@DSInaccessible
@Override public abstract DS_Integer negate();
@DSInaccessible
@Override public abstract DS_Integer absolute();
@DSInaccessible
@Override public abstract DS_Integer increment(int amount);
// API Methods
// ----------------------------------------------------
@DSName("ceil") @DSDynamicField
@Override public DS_Integer ceil() {return this;}
@DSName("floor") @DSDynamicField
@Override public DS_Integer floor() {return this;}
@DSName("round") @DSDynamicField
@Override public DS_Integer round() {return this;}
@DSName("sqrt") @DSDynamicField
@Override public abstract DS_Decimal sqrt();
@DSName("times")
public DS_Object times(DS_Callable callable) throws DScriptErr
{
DS_Object ret = DS_Blank.BLANK;
final long value = longValue();
for (long i=0; i<value; i++)
ret = callable.call();
return ret;
}
/**
* <p>Exists mostly for compatibility with {@link DS_Decimal#roundTo(int)},
* but it can be used to round off lower digits of an integer if a negative
* value is given for <code>places</code>.</p>
*
* @param places The number of decimal places to round to.
* @return A new integer, equal to this integer rounded to the given number
* of decimal places.
* @see {@link DS_Decimal#roundTo(int)}
*/
@DSName("roundTo")
public DS_Scalar roundTo(int places)
{
if (places >= 0) return this;
final double exp = Math.pow(10, places);
return ScalarFactory.fromNumber(Math.round(doubleValue()*exp)/exp,
getNumberType());
}
@DSInaccessible
@Override public final boolean isIntegral()
{return true;}
@DSName("NaN") @DSDynamicField
@Override public final boolean isNaN()
{return false;}
@DSName("infinite") @DSDynamicField
@Override public boolean isInfinite()
{return false;}
// DS_Object Methods
// ----------------------------------------------------
public String getTypeName()
{return TYPE_NAME;}
@Override protected DS_JavaClass getDeltaScriptClass()
{return DSCLASS;}
}