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
|
||||||
|
|
||||||
/**
|
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
|
* Check if the thread was interrupted and
|
||||||
* throw an exception to end the computation.
|
* throw an exception to end the computation.
|
||||||
*/
|
*/
|
||||||
private static void checkInterrupted() {
|
private fun checkInterrupted(){
|
||||||
if (Thread.currentThread().isInterrupted())
|
if(Thread.currentThread().isInterrupted)
|
||||||
throw new ComputationInterruptedException();
|
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.
|
* 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
|
* Returns the smallest error this instance can tolerate depending
|
||||||
* a new number instance.
|
* on its precision and value.
|
||||||
*
|
|
||||||
* @param multiplier the multiplier
|
|
||||||
* @return the result of the multiplication.
|
|
||||||
*/
|
*/
|
||||||
protected abstract NumberInterface multiplyInternal(NumberInterface multiplier);
|
abstract val maxError: NumberInterface
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds this number to another, returning
|
* Adds this number to another, returning
|
||||||
|
@ -76,22 +44,7 @@ public abstract class NumberInterface implements Comparable<NumberInterface> {
|
||||||
* @param summand the summand
|
* @param summand the summand
|
||||||
* @return the result of the summation.
|
* @return the result of the summation.
|
||||||
*/
|
*/
|
||||||
protected abstract NumberInterface addInternal(NumberInterface summand);
|
abstract fun addInternal(summand: NumberInterface): 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.
|
|
||||||
*/
|
|
||||||
public final NumberInterface add(NumberInterface summand) {
|
|
||||||
checkInterrupted();
|
|
||||||
return addInternal(summand);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subtracts another number from this number,
|
* Subtracts another number from this number,
|
||||||
* a new number instance.
|
* a new number instance.
|
||||||
|
@ -99,30 +52,111 @@ public abstract class NumberInterface implements Comparable<NumberInterface> {
|
||||||
* @param subtrahend the subtrahend.
|
* @param subtrahend the subtrahend.
|
||||||
* @return the result of the subtraction.
|
* @return the result of the subtraction.
|
||||||
*/
|
*/
|
||||||
protected abstract NumberInterface subtractInternal(NumberInterface subtrahend);
|
abstract fun subtractInternal(subtrahend: NumberInterface): NumberInterface
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subtracts another number from this number,
|
* Multiplies this number by another, returning
|
||||||
* a new number instance. Also, checks if the
|
* a new number instance.
|
||||||
* thread has been interrupted, and if so, throws
|
|
||||||
* an exception.
|
|
||||||
*
|
*
|
||||||
* @param subtrahend the subtrahend.
|
* @param multiplier the multiplier
|
||||||
* @return the result of the subtraction.
|
* @return the result of the multiplication.
|
||||||
*/
|
*/
|
||||||
public final NumberInterface subtract(NumberInterface subtrahend) {
|
abstract fun multiplyInternal(multiplier: NumberInterface): NumberInterface
|
||||||
checkInterrupted();
|
/**
|
||||||
return subtractInternal(subtrahend);
|
* 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
|
* Returns a new instance of this number with
|
||||||
* the sign flipped.
|
* the sign flipped.
|
||||||
*
|
*
|
||||||
* @return the new instance.
|
* @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
|
* Returns a new instance of this number with
|
||||||
|
@ -132,19 +166,11 @@ public abstract class NumberInterface implements Comparable<NumberInterface> {
|
||||||
*
|
*
|
||||||
* @return the new instance.
|
* @return the new instance.
|
||||||
*/
|
*/
|
||||||
public final NumberInterface negate() {
|
fun negate(): NumberInterface {
|
||||||
checkInterrupted();
|
checkInterrupted()
|
||||||
return negateInternal();
|
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
|
* Raises this number to an integer power. Also, checks if the
|
||||||
* thread has been interrupted, and if so, throws
|
* 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.
|
* @param exponent the exponent to which to take the number.
|
||||||
* @return the resulting value.
|
* @return the resulting value.
|
||||||
*/
|
*/
|
||||||
public final NumberInterface intPow(int exponent) {
|
fun intPow(exponent: Int): NumberInterface {
|
||||||
checkInterrupted();
|
checkInterrupted()
|
||||||
return intPowInternal(exponent);
|
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.
|
* Returns the least integer greater than or equal to the number.
|
||||||
* Also, checks if the thread has been interrupted, and if so, throws
|
* 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.
|
* @return the least integer bigger or equal to the number.
|
||||||
*/
|
*/
|
||||||
public final NumberInterface ceiling() {
|
fun ceiling(): NumberInterface {
|
||||||
checkInterrupted();
|
checkInterrupted()
|
||||||
return ceilingInternal();
|
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.
|
* Return the greatest integer less than or equal to the number.
|
||||||
* Also, checks if the thread has been interrupted, and if so, throws
|
* 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.
|
* @return the greatest int smaller than or equal to the number.
|
||||||
*/
|
*/
|
||||||
public final NumberInterface floor() {
|
fun floor(): NumberInterface {
|
||||||
checkInterrupted();
|
checkInterrupted()
|
||||||
return floorInternal();
|
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).
|
* Returns the fractional part of the number, specifically x - floor(x).
|
||||||
* Also, checks if the thread has been interrupted,
|
* 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.
|
* @return the fractional part of the number.
|
||||||
*/
|
*/
|
||||||
public final NumberInterface fractionalPart() {
|
fun fractionalPart(): NumberInterface {
|
||||||
checkInterrupted();
|
checkInterrupted()
|
||||||
return fractionalPartInternal();
|
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.
|
* 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 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.
|
* @param other the value at the bottom of the range.
|
||||||
* @return the resulting range builder.
|
* @return the resulting range builder.
|
||||||
*/
|
*/
|
||||||
public NumberRangeBuilder rangeTo(NumberInterface other){
|
operator fun rangeTo(other: NumberInterface) = NumberRangeBuilder(this, other)
|
||||||
return new NumberRangeBuilder(this, other);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user