1
0
mirror of https://github.com/DanilaFe/abacus synced 2026-01-26 00:25:20 +00:00

Convert NumberInterface into abstract class, and check for interruption.

This commit is contained in:
2017-08-05 13:54:06 -07:00
parent 23a3eb88f1
commit cd60c9d52f
4 changed files with 104 additions and 37 deletions

View File

@@ -3,14 +3,22 @@ package org.nwapw.abacus.number;
/**
* An interface used to represent a number.
*/
public interface NumberInterface {
public abstract class 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();
}
/**
* The maximum precision to which this number operates.
*
* @return the precision.
*/
int getMaxPrecision();
public abstract int getMaxPrecision();
/**
* Multiplies this number by another, returning
@@ -19,7 +27,12 @@ public interface NumberInterface {
* @param multiplier the multiplier
* @return the result of the multiplication.
*/
NumberInterface multiply(NumberInterface multiplier);
protected abstract NumberInterface multiplyInternal(NumberInterface multiplier);
public final NumberInterface multiply(NumberInterface multiplier){
checkInterrupted();
return multiplyInternal(multiplier);
}
/**
* Divides this number by another, returning
@@ -28,7 +41,12 @@ public interface NumberInterface {
* @param divisor the divisor
* @return the result of the division.
*/
NumberInterface divide(NumberInterface divisor);
protected abstract NumberInterface divideInternal(NumberInterface divisor);
public final NumberInterface divide(NumberInterface divisor){
checkInterrupted();
return divideInternal(divisor);
}
/**
* Adds this number to another, returning
@@ -37,7 +55,12 @@ public interface NumberInterface {
* @param summand the summand
* @return the result of the summation.
*/
NumberInterface add(NumberInterface summand);
protected abstract NumberInterface addInternal(NumberInterface summand);
public final NumberInterface add(NumberInterface summand){
checkInterrupted();
return addInternal(summand);
}
/**
* Subtracts another number from this number,
@@ -46,7 +69,12 @@ public interface NumberInterface {
* @param subtrahend the subtrahend.
* @return the result of the subtraction.
*/
NumberInterface subtract(NumberInterface subtrahend);
protected abstract NumberInterface subtractInternal(NumberInterface subtrahend);
public final NumberInterface subtract(NumberInterface subtrahend){
checkInterrupted();
return subtractInternal(subtrahend);
}
/**
* Returns a new instance of this number with
@@ -54,7 +82,12 @@ public interface NumberInterface {
*
* @return the new instance.
*/
NumberInterface negate();
protected abstract NumberInterface negateInternal();
public final NumberInterface negate(){
checkInterrupted();
return negateInternal();
}
/**
* Raises this number to an integer power.
@@ -62,7 +95,12 @@ public interface NumberInterface {
* @param exponent the exponent to which to take the number.
* @return the resulting value.
*/
NumberInterface intPow(int exponent);
protected abstract NumberInterface intPowInternal(int exponent);
public final NumberInterface intPow(int exponent){
checkInterrupted();
return intPowInternal(exponent);
}
/**
* Compares this number to another.
@@ -70,35 +108,50 @@ public interface NumberInterface {
* @param number the number to compare to.
* @return same as Integer.compare();
*/
int compareTo(NumberInterface number);
public abstract int compareTo(NumberInterface number);
/**
* Same as Math.signum().
*
* @return 1 if this number is positive, -1 if this number is negative, 0 if this number is 0.
*/
int signum();
public abstract int signum();
/**
* Returns the least integer greater than or equal to the number.
*
* @return the least integer >= the number, if int can hold the value.
*/
NumberInterface ceiling();
protected abstract NumberInterface ceilingInternal();
public final NumberInterface ceiling(){
checkInterrupted();
return ceilingInternal();
}
/**
* Return the greatest integer less than or equal to the number.
*
* @return the greatest int >= the number, if int can hold the value.
*/
NumberInterface floor();
protected abstract NumberInterface floorInternal();
public final NumberInterface floor(){
checkInterrupted();
return floorInternal();
}
/**
* Returns the fractional part of the number.
*
* @return the fractional part of the number.
*/
NumberInterface fractionalPart();
protected abstract NumberInterface fractionalPartInternal();
public final NumberInterface fractionalPart(){
checkInterrupted();
return fractionalPartInternal();
}
/**
* Returns the integer representation of this number, discarding any fractional part,
@@ -106,7 +159,7 @@ public interface NumberInterface {
*
* @return
*/
int intValue();
public abstract int intValue();
/**
* Promotes this class to another number class.
@@ -114,6 +167,11 @@ public interface NumberInterface {
* @param toClass the class to promote to.
* @return the resulting new instance.
*/
NumberInterface promoteTo(Class<? extends NumberInterface> toClass);
protected abstract NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass);
public final NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
checkInterrupted();
return promoteToInternal(toClass);
}
}