mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-23 07:50:09 -08:00
Convert NumberInterface into abstract class, and check for interruption.
This commit is contained in:
parent
8465a2d918
commit
37a759288a
|
@ -0,0 +1,9 @@
|
||||||
|
package org.nwapw.abacus.number;
|
||||||
|
|
||||||
|
public class ComputationInterruptedException extends RuntimeException {
|
||||||
|
|
||||||
|
public ComputationInterruptedException(){
|
||||||
|
super("Computation interrupted by user.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -3,7 +3,7 @@ package org.nwapw.abacus.number;
|
||||||
/**
|
/**
|
||||||
* An implementation of NumberInterface using a double.
|
* An implementation of NumberInterface using a double.
|
||||||
*/
|
*/
|
||||||
public class NaiveNumber implements NumberInterface {
|
public class NaiveNumber extends NumberInterface {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The number zero.
|
* The number zero.
|
||||||
|
@ -42,32 +42,32 @@ public class NaiveNumber implements NumberInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface multiply(NumberInterface multiplier) {
|
public NumberInterface multiplyInternal(NumberInterface multiplier) {
|
||||||
return new NaiveNumber(value * ((NaiveNumber) multiplier).value);
|
return new NaiveNumber(value * ((NaiveNumber) multiplier).value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface divide(NumberInterface divisor) {
|
public NumberInterface divideInternal(NumberInterface divisor) {
|
||||||
return new NaiveNumber(value / ((NaiveNumber) divisor).value);
|
return new NaiveNumber(value / ((NaiveNumber) divisor).value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface add(NumberInterface summand) {
|
public NumberInterface addInternal(NumberInterface summand) {
|
||||||
return new NaiveNumber(value + ((NaiveNumber) summand).value);
|
return new NaiveNumber(value + ((NaiveNumber) summand).value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface subtract(NumberInterface subtrahend) {
|
public NumberInterface subtractInternal(NumberInterface subtrahend) {
|
||||||
return new NaiveNumber(value - ((NaiveNumber) subtrahend).value);
|
return new NaiveNumber(value - ((NaiveNumber) subtrahend).value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface negate() {
|
public NumberInterface negateInternal() {
|
||||||
return new NaiveNumber(-value);
|
return new NaiveNumber(-value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface intPow(int exponent) {
|
public NumberInterface intPowInternal(int exponent) {
|
||||||
if (exponent == 0) {
|
if (exponent == 0) {
|
||||||
return NaiveNumber.ONE;
|
return NaiveNumber.ONE;
|
||||||
}
|
}
|
||||||
|
@ -95,17 +95,17 @@ public class NaiveNumber implements NumberInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface ceiling() {
|
public NumberInterface ceilingInternal() {
|
||||||
return new NaiveNumber(Math.ceil(value));
|
return new NaiveNumber(Math.ceil(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface floor() {
|
public NumberInterface floorInternal() {
|
||||||
return new NaiveNumber(Math.floor(value));
|
return new NaiveNumber(Math.floor(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface fractionalPart() {
|
public NumberInterface fractionalPartInternal() {
|
||||||
return new NaiveNumber(value - Math.floor(value));
|
return new NaiveNumber(value - Math.floor(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ public class NaiveNumber implements NumberInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
public NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass) {
|
||||||
if (toClass == this.getClass()) return this;
|
if (toClass == this.getClass()) return this;
|
||||||
else if (toClass == PreciseNumber.class) {
|
else if (toClass == PreciseNumber.class) {
|
||||||
return new PreciseNumber(Double.toString(value));
|
return new PreciseNumber(Double.toString(value));
|
||||||
|
|
|
@ -3,14 +3,22 @@ package org.nwapw.abacus.number;
|
||||||
/**
|
/**
|
||||||
* An interface used to represent a 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.
|
* The maximum precision to which this number operates.
|
||||||
*
|
*
|
||||||
* @return the precision.
|
* @return the precision.
|
||||||
*/
|
*/
|
||||||
int getMaxPrecision();
|
public abstract int getMaxPrecision();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Multiplies this number by another, returning
|
* Multiplies this number by another, returning
|
||||||
|
@ -19,7 +27,12 @@ public interface NumberInterface {
|
||||||
* @param multiplier the multiplier
|
* @param multiplier the multiplier
|
||||||
* @return the result of the multiplication.
|
* @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
|
* Divides this number by another, returning
|
||||||
|
@ -28,7 +41,12 @@ public interface NumberInterface {
|
||||||
* @param divisor the divisor
|
* @param divisor the divisor
|
||||||
* @return the result of the division.
|
* @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
|
* Adds this number to another, returning
|
||||||
|
@ -37,7 +55,12 @@ public interface NumberInterface {
|
||||||
* @param summand the summand
|
* @param summand the summand
|
||||||
* @return the result of the summation.
|
* @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,
|
* Subtracts another number from this number,
|
||||||
|
@ -46,7 +69,12 @@ public interface NumberInterface {
|
||||||
* @param subtrahend the subtrahend.
|
* @param subtrahend the subtrahend.
|
||||||
* @return the result of the subtraction.
|
* @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
|
* Returns a new instance of this number with
|
||||||
|
@ -54,7 +82,12 @@ public interface NumberInterface {
|
||||||
*
|
*
|
||||||
* @return the new instance.
|
* @return the new instance.
|
||||||
*/
|
*/
|
||||||
NumberInterface negate();
|
protected abstract NumberInterface negateInternal();
|
||||||
|
|
||||||
|
public final NumberInterface negate(){
|
||||||
|
checkInterrupted();
|
||||||
|
return negateInternal();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Raises this number to an integer power.
|
* Raises this number to an integer power.
|
||||||
|
@ -62,7 +95,12 @@ public interface 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.
|
||||||
*/
|
*/
|
||||||
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.
|
* Compares this number to another.
|
||||||
|
@ -70,35 +108,50 @@ public interface NumberInterface {
|
||||||
* @param number the number to compare to.
|
* @param number the number to compare to.
|
||||||
* @return same as Integer.compare();
|
* @return same as Integer.compare();
|
||||||
*/
|
*/
|
||||||
int compareTo(NumberInterface number);
|
public abstract int compareTo(NumberInterface number);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Same as Math.signum().
|
* Same as Math.signum().
|
||||||
*
|
*
|
||||||
* @return 1 if this number is positive, -1 if this number is negative, 0 if this number is 0.
|
* @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.
|
* Returns the least integer greater than or equal to the number.
|
||||||
*
|
*
|
||||||
* @return the least integer >= the number, if int can hold the value.
|
* @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 integer less than or equal to the number.
|
||||||
*
|
*
|
||||||
* @return the greatest int >= the number, if int can hold the value.
|
* @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.
|
* Returns the fractional part of the number.
|
||||||
*
|
*
|
||||||
* @return 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,
|
* Returns the integer representation of this number, discarding any fractional part,
|
||||||
|
@ -106,7 +159,7 @@ public interface NumberInterface {
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
int intValue();
|
public abstract int intValue();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Promotes this class to another number class.
|
* Promotes this class to another number class.
|
||||||
|
@ -114,6 +167,11 @@ public interface NumberInterface {
|
||||||
* @param toClass the class to promote to.
|
* @param toClass the class to promote to.
|
||||||
* @return the resulting new instance.
|
* @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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import java.math.RoundingMode;
|
||||||
* A number that uses a BigDecimal to store its value,
|
* A number that uses a BigDecimal to store its value,
|
||||||
* leading to infinite possible precision.
|
* leading to infinite possible precision.
|
||||||
*/
|
*/
|
||||||
public class PreciseNumber implements NumberInterface {
|
public class PreciseNumber extends NumberInterface {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The number one.
|
* The number one.
|
||||||
|
@ -52,27 +52,27 @@ public class PreciseNumber implements NumberInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface multiply(NumberInterface multiplier) {
|
public NumberInterface multiplyInternal(NumberInterface multiplier) {
|
||||||
return new PreciseNumber(this.value.multiply(((PreciseNumber) multiplier).value));
|
return new PreciseNumber(this.value.multiply(((PreciseNumber) multiplier).value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface divide(NumberInterface divisor) {
|
public NumberInterface divideInternal(NumberInterface divisor) {
|
||||||
return new PreciseNumber(value.divide(((PreciseNumber) divisor).value, this.getMaxPrecision(), RoundingMode.HALF_UP));
|
return new PreciseNumber(value.divide(((PreciseNumber) divisor).value, this.getMaxPrecision(), RoundingMode.HALF_UP));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface add(NumberInterface summand) {
|
public NumberInterface addInternal(NumberInterface summand) {
|
||||||
return new PreciseNumber(value.add(((PreciseNumber) summand).value));
|
return new PreciseNumber(value.add(((PreciseNumber) summand).value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface subtract(NumberInterface subtrahend) {
|
public NumberInterface subtractInternal(NumberInterface subtrahend) {
|
||||||
return new PreciseNumber(value.subtract(((PreciseNumber) subtrahend).value));
|
return new PreciseNumber(value.subtract(((PreciseNumber) subtrahend).value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface intPow(int exponent) {
|
public NumberInterface intPowInternal(int exponent) {
|
||||||
if (exponent == 0) {
|
if (exponent == 0) {
|
||||||
return PreciseNumber.ONE;
|
return PreciseNumber.ONE;
|
||||||
}
|
}
|
||||||
|
@ -99,7 +99,7 @@ public class PreciseNumber implements NumberInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface ceiling() {
|
public NumberInterface ceilingInternal() {
|
||||||
String str = value.toPlainString();
|
String str = value.toPlainString();
|
||||||
int decimalIndex = str.indexOf('.');
|
int decimalIndex = str.indexOf('.');
|
||||||
if (decimalIndex != -1) {
|
if (decimalIndex != -1) {
|
||||||
|
@ -109,7 +109,7 @@ public class PreciseNumber implements NumberInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface floor() {
|
public NumberInterface floorInternal() {
|
||||||
String str = value.toPlainString();
|
String str = value.toPlainString();
|
||||||
int decimalIndex = str.indexOf('.');
|
int decimalIndex = str.indexOf('.');
|
||||||
if (decimalIndex != -1) {
|
if (decimalIndex != -1) {
|
||||||
|
@ -119,7 +119,7 @@ public class PreciseNumber implements NumberInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface fractionalPart() {
|
public NumberInterface fractionalPartInternal() {
|
||||||
String str = value.toPlainString();
|
String str = value.toPlainString();
|
||||||
int decimalIndex = str.indexOf('.');
|
int decimalIndex = str.indexOf('.');
|
||||||
if (decimalIndex != -1) {
|
if (decimalIndex != -1) {
|
||||||
|
@ -134,12 +134,12 @@ public class PreciseNumber implements NumberInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface negate() {
|
public NumberInterface negateInternal() {
|
||||||
return new PreciseNumber(value.negate());
|
return new PreciseNumber(value.negate());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
public NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass) {
|
||||||
if (toClass == this.getClass()) {
|
if (toClass == this.getClass()) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user