mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-22 07:20:09 -08:00
Rewrite NumberInterface in Kotlin.
This commit is contained in:
parent
e0ccb67ad3
commit
bc475a22f9
292
core/src/main/java/org/nwapw/abacus/number/NumberInterface.java → core/src/main/kotlin/org/nwapw/abacus/number/NumberInterface.kt
Executable file → Normal file
292
core/src/main/java/org/nwapw/abacus/number/NumberInterface.java → core/src/main/kotlin/org/nwapw/abacus/number/NumberInterface.kt
Executable file → Normal file
|
@ -1,73 +1,41 @@
|
|||
package org.nwapw.abacus.number;
|
||||
package org.nwapw.abacus.number
|
||||
|
||||
import org.nwapw.abacus.exception.ComputationInterruptedException;
|
||||
import org.nwapw.abacus.exception.ComputationInterruptedException
|
||||
|
||||
/**
|
||||
* An interface used to represent a number.
|
||||
*/
|
||||
public abstract class NumberInterface implements Comparable<NumberInterface> {
|
||||
abstract class NumberInterface: Comparable<NumberInterface> {
|
||||
|
||||
/**
|
||||
* Check if the thread was interrupted and
|
||||
* throw an exception to end the computation.
|
||||
*/
|
||||
private static void checkInterrupted() {
|
||||
if (Thread.currentThread().isInterrupted())
|
||||
throw new ComputationInterruptedException();
|
||||
private fun checkInterrupted(){
|
||||
if(Thread.currentThread().isInterrupted)
|
||||
throw ComputationInterruptedException()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the integer representation of this number, discarding any fractional part,
|
||||
* if int can hold the value.
|
||||
*
|
||||
* @return the integer value of this number.
|
||||
*/
|
||||
abstract fun intValue(): Int
|
||||
/**
|
||||
* Same as Math.signum().
|
||||
*
|
||||
* @return 1 if this number is positive, -1 if this number is negative, 0 if this number is 0.
|
||||
*/
|
||||
abstract fun signum(): Int
|
||||
|
||||
/**
|
||||
* The maximum precision to which this number operates.
|
||||
*
|
||||
* @return the precision.
|
||||
*/
|
||||
public abstract int getMaxPrecision();
|
||||
|
||||
abstract val maxPrecision: Int
|
||||
/**
|
||||
* Multiplies this number by another, returning
|
||||
* a new number instance.
|
||||
*
|
||||
* @param multiplier the multiplier
|
||||
* @return the result of the multiplication.
|
||||
* Returns the smallest error this instance can tolerate depending
|
||||
* on its precision and value.
|
||||
*/
|
||||
protected abstract NumberInterface multiplyInternal(NumberInterface multiplier);
|
||||
|
||||
/**
|
||||
* Multiplies this number by another, returning
|
||||
* a new number instance. Also, checks if the
|
||||
* thread has been interrupted, and if so, throws
|
||||
* an exception.
|
||||
*
|
||||
* @param multiplier the multiplier
|
||||
* @return the result of the multiplication.
|
||||
*/
|
||||
public final NumberInterface multiply(NumberInterface multiplier) {
|
||||
checkInterrupted();
|
||||
return multiplyInternal(multiplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides this number by another, returning
|
||||
* a new number instance.
|
||||
*
|
||||
* @param divisor the divisor
|
||||
* @return the result of the division.
|
||||
*/
|
||||
protected abstract NumberInterface divideInternal(NumberInterface divisor);
|
||||
|
||||
/**
|
||||
* Divides this number by another, returning
|
||||
* a new number instance. Also, checks if the
|
||||
* thread has been interrupted, and if so, throws
|
||||
* an exception.
|
||||
*
|
||||
* @param divisor the divisor
|
||||
* @return the result of the division.
|
||||
*/
|
||||
public final NumberInterface divide(NumberInterface divisor) {
|
||||
checkInterrupted();
|
||||
return divideInternal(divisor);
|
||||
}
|
||||
abstract val maxError: NumberInterface
|
||||
|
||||
/**
|
||||
* Adds this number to another, returning
|
||||
|
@ -76,22 +44,7 @@ public abstract class NumberInterface implements Comparable<NumberInterface> {
|
|||
* @param summand the summand
|
||||
* @return the result of the summation.
|
||||
*/
|
||||
protected abstract NumberInterface addInternal(NumberInterface summand);
|
||||
|
||||
/**
|
||||
* Adds this number to another, returning
|
||||
* a new number instance. Also, checks if the
|
||||
* thread has been interrupted, and if so, throws
|
||||
* an exception.
|
||||
*
|
||||
* @param summand the summand
|
||||
* @return the result of the summation.
|
||||
*/
|
||||
public final NumberInterface add(NumberInterface summand) {
|
||||
checkInterrupted();
|
||||
return addInternal(summand);
|
||||
}
|
||||
|
||||
abstract fun addInternal(summand: NumberInterface): NumberInterface
|
||||
/**
|
||||
* Subtracts another number from this number,
|
||||
* a new number instance.
|
||||
|
@ -99,30 +52,111 @@ public abstract class NumberInterface implements Comparable<NumberInterface> {
|
|||
* @param subtrahend the subtrahend.
|
||||
* @return the result of the subtraction.
|
||||
*/
|
||||
protected abstract NumberInterface subtractInternal(NumberInterface subtrahend);
|
||||
|
||||
abstract fun subtractInternal(subtrahend: NumberInterface): NumberInterface
|
||||
/**
|
||||
* Subtracts another number from this number,
|
||||
* a new number instance. Also, checks if the
|
||||
* thread has been interrupted, and if so, throws
|
||||
* an exception.
|
||||
* Multiplies this number by another, returning
|
||||
* a new number instance.
|
||||
*
|
||||
* @param subtrahend the subtrahend.
|
||||
* @return the result of the subtraction.
|
||||
* @param multiplier the multiplier
|
||||
* @return the result of the multiplication.
|
||||
*/
|
||||
public final NumberInterface subtract(NumberInterface subtrahend) {
|
||||
checkInterrupted();
|
||||
return subtractInternal(subtrahend);
|
||||
}
|
||||
|
||||
abstract fun multiplyInternal(multiplier: NumberInterface): NumberInterface
|
||||
/**
|
||||
* Divides this number by another, returning
|
||||
* a new number instance.
|
||||
*
|
||||
* @param divisor the divisor
|
||||
* @return the result of the division.
|
||||
*/
|
||||
abstract fun divideInternal(divisor: NumberInterface): NumberInterface
|
||||
/**
|
||||
* Returns a new instance of this number with
|
||||
* the sign flipped.
|
||||
*
|
||||
* @return the new instance.
|
||||
*/
|
||||
protected abstract NumberInterface negateInternal();
|
||||
abstract fun negateInternal(): NumberInterface
|
||||
/**
|
||||
* Raises this number to an integer power.
|
||||
*
|
||||
* @param exponent the exponent to which to take the number.
|
||||
* @return the resulting value.
|
||||
*/
|
||||
abstract fun intPowInternal(pow: Int): NumberInterface
|
||||
/**
|
||||
* Returns the least integer greater than or equal to the number.
|
||||
*
|
||||
* @return the least integer greater or equal to the number, if int can hold the value.
|
||||
*/
|
||||
abstract fun ceilingInternal(): NumberInterface
|
||||
/**
|
||||
* Return the greatest integer less than or equal to the number.
|
||||
*
|
||||
* @return the greatest integer smaller or equal the number.
|
||||
*/
|
||||
abstract fun floorInternal(): NumberInterface
|
||||
/**
|
||||
* Returns the fractional part of the number.
|
||||
*
|
||||
* @return the fractional part of the number.
|
||||
*/
|
||||
abstract fun fractionalPartInternal(): NumberInterface
|
||||
|
||||
/**
|
||||
* Adds this number to another, returning
|
||||
* a new number instance. Also, checks if the
|
||||
* thread has been interrupted, and if so, throws
|
||||
* an exception.
|
||||
*
|
||||
* @param summand the summand
|
||||
* @return the result of the summation.
|
||||
*/
|
||||
fun add(summand: NumberInterface): NumberInterface {
|
||||
checkInterrupted()
|
||||
return addInternal(summand)
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts another number from this number,
|
||||
* a new number instance. Also, checks if the
|
||||
* thread has been interrupted, and if so, throws
|
||||
* an exception.
|
||||
*
|
||||
* @param subtrahend the subtrahend.
|
||||
* @return the result of the subtraction.
|
||||
*/
|
||||
fun subtract(subtrahend: NumberInterface): NumberInterface {
|
||||
checkInterrupted()
|
||||
return subtractInternal(subtrahend)
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies this number by another, returning
|
||||
* a new number instance. Also, checks if the
|
||||
* thread has been interrupted, and if so, throws
|
||||
* an exception.
|
||||
*
|
||||
* @param multiplier the multiplier
|
||||
* @return the result of the multiplication.
|
||||
*/
|
||||
fun multiply(multiplier: NumberInterface): NumberInterface {
|
||||
checkInterrupted()
|
||||
return multiplyInternal(multiplier)
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides this number by another, returning
|
||||
* a new number instance. Also, checks if the
|
||||
* thread has been interrupted, and if so, throws
|
||||
* an exception.
|
||||
*
|
||||
* @param divisor the divisor
|
||||
* @return the result of the division.
|
||||
*/
|
||||
fun divide(divisor: NumberInterface): NumberInterface {
|
||||
checkInterrupted()
|
||||
return divideInternal(divisor)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new instance of this number with
|
||||
|
@ -132,19 +166,11 @@ public abstract class NumberInterface implements Comparable<NumberInterface> {
|
|||
*
|
||||
* @return the new instance.
|
||||
*/
|
||||
public final NumberInterface negate() {
|
||||
checkInterrupted();
|
||||
return negateInternal();
|
||||
fun negate(): NumberInterface {
|
||||
checkInterrupted()
|
||||
return negateInternal()
|
||||
}
|
||||
|
||||
/**
|
||||
* Raises this number to an integer power.
|
||||
*
|
||||
* @param exponent the exponent to which to take the number.
|
||||
* @return the resulting value.
|
||||
*/
|
||||
protected abstract NumberInterface intPowInternal(int exponent);
|
||||
|
||||
/**
|
||||
* Raises this number to an integer power. Also, checks if the
|
||||
* thread has been interrupted, and if so, throws
|
||||
|
@ -153,25 +179,11 @@ public abstract class NumberInterface implements Comparable<NumberInterface> {
|
|||
* @param exponent the exponent to which to take the number.
|
||||
* @return the resulting value.
|
||||
*/
|
||||
public final NumberInterface intPow(int exponent) {
|
||||
checkInterrupted();
|
||||
return intPowInternal(exponent);
|
||||
fun intPow(exponent: Int): NumberInterface {
|
||||
checkInterrupted()
|
||||
return intPowInternal(exponent)
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as Math.signum().
|
||||
*
|
||||
* @return 1 if this number is positive, -1 if this number is negative, 0 if this number is 0.
|
||||
*/
|
||||
public abstract int signum();
|
||||
|
||||
/**
|
||||
* Returns the least integer greater than or equal to the number.
|
||||
*
|
||||
* @return the least integer greater or equal to the number, if int can hold the value.
|
||||
*/
|
||||
protected abstract NumberInterface ceilingInternal();
|
||||
|
||||
/**
|
||||
* Returns the least integer greater than or equal to the number.
|
||||
* Also, checks if the thread has been interrupted, and if so, throws
|
||||
|
@ -179,18 +191,11 @@ public abstract class NumberInterface implements Comparable<NumberInterface> {
|
|||
*
|
||||
* @return the least integer bigger or equal to the number.
|
||||
*/
|
||||
public final NumberInterface ceiling() {
|
||||
checkInterrupted();
|
||||
return ceilingInternal();
|
||||
fun ceiling(): NumberInterface {
|
||||
checkInterrupted()
|
||||
return ceilingInternal()
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the greatest integer less than or equal to the number.
|
||||
*
|
||||
* @return the greatest integer smaller or equal the number.
|
||||
*/
|
||||
protected abstract NumberInterface floorInternal();
|
||||
|
||||
/**
|
||||
* Return the greatest integer less than or equal to the number.
|
||||
* Also, checks if the thread has been interrupted, and if so, throws
|
||||
|
@ -198,18 +203,11 @@ public abstract class NumberInterface implements Comparable<NumberInterface> {
|
|||
*
|
||||
* @return the greatest int smaller than or equal to the number.
|
||||
*/
|
||||
public final NumberInterface floor() {
|
||||
checkInterrupted();
|
||||
return floorInternal();
|
||||
fun floor(): NumberInterface {
|
||||
checkInterrupted()
|
||||
return floorInternal()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the fractional part of the number.
|
||||
*
|
||||
* @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,
|
||||
|
@ -217,27 +215,11 @@ public abstract class NumberInterface implements Comparable<NumberInterface> {
|
|||
*
|
||||
* @return the fractional part of the number.
|
||||
*/
|
||||
public final NumberInterface fractionalPart() {
|
||||
checkInterrupted();
|
||||
return fractionalPartInternal();
|
||||
fun fractionalPart(): NumberInterface {
|
||||
checkInterrupted()
|
||||
return fractionalPartInternal()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the integer representation of this number, discarding any fractional part,
|
||||
* if int can hold the value.
|
||||
*
|
||||
* @return the integer value of this number.
|
||||
*/
|
||||
public abstract int intValue();
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
@ -246,8 +228,6 @@ public abstract class NumberInterface implements Comparable<NumberInterface> {
|
|||
* @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);
|
||||
}
|
||||
operator fun rangeTo(other: NumberInterface) = NumberRangeBuilder(this, other)
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user