serene-simple/src/main/java/serene/simple/SNumber.java

80 lines
1.6 KiB
Java

package serene.simple;
import serene.simple.SereneException;
public class SNumber {
public static interface IOps {
public IOps add(Object x);
public IOps add(Double x);
public IOps add(Long x);
public Object value();
}
public static class LongNumber implements IOps {
private long v;
public LongNumber() {
this.v = 0;
}
public LongNumber(long v) {
this.v = v;
}
public IOps add(Object x) throws SereneException {
if (x instanceof Long) {
return this.add((long) x);
}
else if (x instanceof Double) {
return this.add((Double) x);
}
throw new SereneException("Can't cast anything beside Long and Double");
}
public IOps add(Long x) {
return new LongNumber(this.v + x);
}
public IOps add(Double x) {
DoubleNumber y = new DoubleNumber(x);
return y.add(this.v);
}
public Object value() {
return this.v;
}
}
public static class DoubleNumber implements IOps {
private Double v;
public DoubleNumber(Double v) {
this.v = v;
}
public IOps add(Object x) throws SereneException {
if (x instanceof Long) {
return this.add((long) x);
}
else if (x instanceof Double) {
return this.add((Double) x);
}
throw new SereneException("Can't cast anything beside Long and Double");
}
public IOps add(Double x) {
return new DoubleNumber(this.v + x);
}
public IOps add(Long x) {
return new DoubleNumber(this.v + x.doubleValue());
}
public Object value() {
return this.v;
}
}
}