Rewrite NumberInterface in Kotlin.

This commit is contained in:
Danila Fedorin 2017-09-21 10:18:29 -07:00
parent e0ccb67ad3
commit bc475a22f9
1 changed files with 233 additions and 253 deletions

View File

@ -1,253 +1,233 @@
package org.nwapw.abacus.number; package org.nwapw.abacus.number
import org.nwapw.abacus.exception.ComputationInterruptedException; import org.nwapw.abacus.exception.ComputationInterruptedException
/** abstract class NumberInterface: Comparable<NumberInterface> {
* An interface used to represent a number.
*/ /**
public abstract class NumberInterface implements Comparable<NumberInterface> { * Check if the thread was interrupted and
* throw an exception to end the computation.
/** */
* Check if the thread was interrupted and private fun checkInterrupted(){
* throw an exception to end the computation. if(Thread.currentThread().isInterrupted)
*/ throw ComputationInterruptedException()
private static void checkInterrupted() { }
if (Thread.currentThread().isInterrupted())
throw new ComputationInterruptedException(); /**
} * Returns the integer representation of this number, discarding any fractional part,
* if int can hold the value.
/** *
* The maximum precision to which this number operates. * @return the integer value of this number.
* */
* @return the precision. abstract fun intValue(): Int
*/ /**
public abstract int getMaxPrecision(); * Same as Math.signum().
*
/** * @return 1 if this number is positive, -1 if this number is negative, 0 if this number is 0.
* Multiplies this number by another, returning */
* a new number instance. abstract fun signum(): Int
*
* @param multiplier the multiplier /**
* @return the result of the multiplication. * The maximum precision to which this number operates.
*/ */
protected abstract NumberInterface multiplyInternal(NumberInterface multiplier); abstract val maxPrecision: Int
/**
/** * Returns the smallest error this instance can tolerate depending
* Multiplies this number by another, returning * on its precision and value.
* a new number instance. Also, checks if the */
* thread has been interrupted, and if so, throws abstract val maxError: NumberInterface
* an exception.
* /**
* @param multiplier the multiplier * Adds this number to another, returning
* @return the result of the multiplication. * a new number instance.
*/ *
public final NumberInterface multiply(NumberInterface multiplier) { * @param summand the summand
checkInterrupted(); * @return the result of the summation.
return multiplyInternal(multiplier); */
} abstract fun addInternal(summand: NumberInterface): NumberInterface
/**
/** * Subtracts another number from this number,
* Divides this number by another, returning * a new number instance.
* a new number instance. *
* * @param subtrahend the subtrahend.
* @param divisor the divisor * @return the result of the subtraction.
* @return the result of the division. */
*/ abstract fun subtractInternal(subtrahend: NumberInterface): NumberInterface
protected abstract NumberInterface divideInternal(NumberInterface divisor); /**
* Multiplies this number by another, returning
/** * a new number instance.
* Divides this number by another, returning *
* a new number instance. Also, checks if the * @param multiplier the multiplier
* thread has been interrupted, and if so, throws * @return the result of the multiplication.
* an exception. */
* abstract fun multiplyInternal(multiplier: NumberInterface): NumberInterface
* @param divisor the divisor /**
* @return the result of the division. * Divides this number by another, returning
*/ * a new number instance.
public final NumberInterface divide(NumberInterface divisor) { *
checkInterrupted(); * @param divisor the divisor
return divideInternal(divisor); * @return the result of the division.
} */
abstract fun divideInternal(divisor: NumberInterface): NumberInterface
/** /**
* Adds this number to another, returning * Returns a new instance of this number with
* a new number instance. * the sign flipped.
* *
* @param summand the summand * @return the new instance.
* @return the result of the summation. */
*/ abstract fun negateInternal(): NumberInterface
protected abstract NumberInterface addInternal(NumberInterface summand); /**
* Raises this number to an integer power.
/** *
* Adds this number to another, returning * @param exponent the exponent to which to take the number.
* a new number instance. Also, checks if the * @return the resulting value.
* thread has been interrupted, and if so, throws */
* an exception. abstract fun intPowInternal(pow: Int): NumberInterface
* /**
* @param summand the summand * Returns the least integer greater than or equal to the number.
* @return the result of the summation. *
*/ * @return the least integer greater or equal to the number, if int can hold the value.
public final NumberInterface add(NumberInterface summand) { */
checkInterrupted(); abstract fun ceilingInternal(): NumberInterface
return addInternal(summand); /**
} * Return the greatest integer less than or equal to the number.
*
/** * @return the greatest integer smaller or equal the number.
* Subtracts another number from this number, */
* a new number instance. abstract fun floorInternal(): NumberInterface
* /**
* @param subtrahend the subtrahend. * Returns the fractional part of the number.
* @return the result of the subtraction. *
*/ * @return the fractional part of the number.
protected abstract NumberInterface subtractInternal(NumberInterface subtrahend); */
abstract fun fractionalPartInternal(): NumberInterface
/**
* Subtracts another number from this number, /**
* a new number instance. Also, checks if the * Adds this number to another, returning
* thread has been interrupted, and if so, throws * a new number instance. Also, checks if the
* an exception. * thread has been interrupted, and if so, throws
* * an exception.
* @param subtrahend the subtrahend. *
* @return the result of the subtraction. * @param summand the summand
*/ * @return the result of the summation.
public final NumberInterface subtract(NumberInterface subtrahend) { */
checkInterrupted(); fun add(summand: NumberInterface): NumberInterface {
return subtractInternal(subtrahend); checkInterrupted()
} return addInternal(summand)
}
/**
* Returns a new instance of this number with /**
* the sign flipped. * Subtracts another number from this number,
* * a new number instance. Also, checks if the
* @return the new instance. * thread has been interrupted, and if so, throws
*/ * an exception.
protected abstract NumberInterface negateInternal(); *
* @param subtrahend the subtrahend.
* @return the result of the subtraction.
/** */
* Returns a new instance of this number with fun subtract(subtrahend: NumberInterface): NumberInterface {
* the sign flipped. Also, checks if the checkInterrupted()
* thread has been interrupted, and if so, throws return subtractInternal(subtrahend)
* an exception. }
*
* @return the new instance. /**
*/ * Multiplies this number by another, returning
public final NumberInterface negate() { * a new number instance. Also, checks if the
checkInterrupted(); * thread has been interrupted, and if so, throws
return negateInternal(); * an exception.
} *
* @param multiplier the multiplier
/** * @return the result of the multiplication.
* Raises this number to an integer power. */
* fun multiply(multiplier: NumberInterface): NumberInterface {
* @param exponent the exponent to which to take the number. checkInterrupted()
* @return the resulting value. return multiplyInternal(multiplier)
*/ }
protected abstract NumberInterface intPowInternal(int exponent);
/**
/** * Divides this number by another, returning
* Raises this number to an integer power. Also, checks if the * a new number instance. Also, checks if the
* thread has been interrupted, and if so, throws * thread has been interrupted, and if so, throws
* an exception. * an exception.
* *
* @param exponent the exponent to which to take the number. * @param divisor the divisor
* @return the resulting value. * @return the result of the division.
*/ */
public final NumberInterface intPow(int exponent) { fun divide(divisor: NumberInterface): NumberInterface {
checkInterrupted(); checkInterrupted()
return intPowInternal(exponent); return divideInternal(divisor)
} }
/** /**
* Same as Math.signum(). * Returns a new instance of this number with
* * the sign flipped. Also, checks if the
* @return 1 if this number is positive, -1 if this number is negative, 0 if this number is 0. * thread has been interrupted, and if so, throws
*/ * an exception.
public abstract int signum(); *
* @return the new instance.
/** */
* Returns the least integer greater than or equal to the number. fun negate(): NumberInterface {
* checkInterrupted()
* @return the least integer greater or equal to the number, if int can hold the value. return negateInternal()
*/ }
protected abstract NumberInterface ceilingInternal();
/**
/** * Raises this number to an integer power. Also, checks if the
* Returns the least integer greater than or equal to the number. * thread has been interrupted, and if so, throws
* Also, checks if the thread has been interrupted, and if so, throws * an exception.
* an exception. *
* * @param exponent the exponent to which to take the number.
* @return the least integer bigger or equal to the number. * @return the resulting value.
*/ */
public final NumberInterface ceiling() { fun intPow(exponent: Int): NumberInterface {
checkInterrupted(); checkInterrupted()
return ceilingInternal(); return intPowInternal(exponent)
} }
/** /**
* Return the greatest integer less than or equal to the number. * Returns the least integer greater than or equal to the number.
* * Also, checks if the thread has been interrupted, and if so, throws
* @return the greatest integer smaller or equal the number. * an exception.
*/ *
protected abstract NumberInterface floorInternal(); * @return the least integer bigger or equal to the number.
*/
/** fun ceiling(): NumberInterface {
* Return the greatest integer less than or equal to the number. checkInterrupted()
* Also, checks if the thread has been interrupted, and if so, throws return ceilingInternal()
* an exception. }
*
* @return the greatest int smaller than or equal to the number. /**
*/ * Return the greatest integer less than or equal to the number.
public final NumberInterface floor() { * Also, checks if the thread has been interrupted, and if so, throws
checkInterrupted(); * an exception.
return floorInternal(); *
} * @return the greatest int smaller than or equal to the number.
*/
/** fun floor(): NumberInterface {
* Returns the fractional part of the number. checkInterrupted()
* return floorInternal()
* @return the fractional part of the number. }
*/
protected abstract NumberInterface fractionalPartInternal(); /**
* Returns the fractional part of the number, specifically x - floor(x).
/** * Also, checks if the thread has been interrupted,
* Returns the fractional part of the number, specifically x - floor(x). * and if so, throws an exception.
* Also, checks if the thread has been interrupted, *
* and if so, throws an exception. * @return the fractional part of the number.
* */
* @return the fractional part of the number. fun fractionalPart(): NumberInterface {
*/ checkInterrupted()
public final NumberInterface fractionalPart() { return fractionalPartInternal()
checkInterrupted(); }
return fractionalPartInternal();
} /**
* Returns a NumberRangeBuilder object, which is used to create a range.
/** * The reason that this returns a builder and not an actual range is that
* Returns the integer representation of this number, discarding any fractional part, * the NumberRange needs to promote values passed to it, which
* if int can hold the value. * requires an abacus instance.
* * @param other the value at the bottom of the range.
* @return the integer value of this number. * @return the resulting range builder.
*/ */
public abstract int intValue(); operator fun rangeTo(other: NumberInterface) = NumberRangeBuilder(this, other)
/** }
* Returns the smallest error this instance can tolerate depending
* on its precision and value.
*
* @return the smallest error that should be permitted in calculations.
*/
public abstract NumberInterface getMaxError();
/**
* Returns a NumberRangeBuilder object, which is used to create a range.
* The reason that this returns a builder and not an actual range is that
* the NumberRange needs to promote values passed to it, which
* requires an abacus instance.
* @param other the value at the bottom of the range.
* @return the resulting range builder.
*/
public NumberRangeBuilder rangeTo(NumberInterface other){
return new NumberRangeBuilder(this, other);
}
}