Last active
March 24, 2019 07:09
-
-
Save gloryofrobots/f1026b63edb6c17c881f7d5136945063 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package draft; | |
public class GenericValue2 { | |
enum Type {INT, DOUBLE}; | |
interface Val { | |
public String str(); | |
public Type type(); | |
} | |
interface Operations<T extends Val> { | |
T add(T one, T two); | |
} | |
class IntOperations implements Operations<IntVal> { | |
public IntVal add(IntVal one, IntVal two) { | |
return new IntVal(one.value + two.value); | |
} | |
} | |
class DoubleOps implements Operations<DoubleVal> { | |
public DoubleVal add(DoubleVal one, DoubleVal two) { | |
return new DoubleVal(one.value + two.value); | |
} | |
} | |
class IntVal implements Val{ | |
public int value; | |
IntVal(int value) { | |
this.value = value; | |
} | |
public String str() { | |
return "<int " + Integer.toString(value) + " >"; | |
} | |
public Type type() { | |
return Type.INT; | |
} | |
} | |
class DoubleVal implements Val { | |
public double value; | |
DoubleVal(double value) { | |
this.value = value; | |
} | |
public String str() { | |
return "<double " + Double.toString(value) + " >"; | |
} | |
public Type type() { | |
return Type.DOUBLE; | |
} | |
} | |
class Converter{ | |
Value arg1(Value v1, Value v2) { | |
Type type1 = v1.type(); | |
Type type2 = v2.type(); | |
if (type1 == type2) { | |
return v1; | |
} | |
if (type1 == Type.INT && type2 == Type.DOUBLE) { | |
IntVal val = (IntVal) v1.getValue(); | |
Val res = new DoubleVal(val.value); | |
return new Value(res); | |
} | |
else { | |
return v1; | |
} | |
} | |
Value arg2(Value v1, Value v2) { | |
Type type1 = v1.type(); | |
Type type2 = v2.type(); | |
if (type1 == type2) { | |
return v2; | |
} | |
if (type1 == Type.DOUBLE && type2 == Type.INT) { | |
IntVal val = (IntVal) v2.getValue(); | |
Val res = new DoubleVal(val.value); | |
return new Value(res); | |
} | |
else { | |
return v2; | |
} | |
} | |
} | |
class Value { | |
Val val; | |
Operations ops; | |
Converter conv = new Converter(); | |
Value(Val v) { | |
setValue(v); | |
} | |
Val getValue() { | |
return val; | |
} | |
Value createValue(Val v) { | |
return new Value(v); | |
} | |
Type type() { | |
return val.type(); | |
} | |
public void setValue(Val v) { | |
val = v; | |
Type type = val.type(); | |
if (type == Type.INT) { | |
ops = new IntOperations(); | |
} else if(type == Type.DOUBLE) { | |
ops = new DoubleOps(); | |
} | |
} | |
boolean isSameType(Value other) { | |
return type() == other.type(); | |
} | |
public Value add(Value other) { | |
if (!isSameType(other)) { | |
Value A = conv.arg1(this, other); | |
Value B = conv.arg2(this, other); | |
return A.add(B); | |
} | |
Val v = ops.add(val, other.val); | |
return createValue(v); | |
} | |
public String toString() { | |
return val.str(); | |
} | |
} | |
class Value2 extends Value { | |
Value2(Val v) { | |
super(v); | |
} | |
} | |
public Value doubleValue(double v) { | |
return new Value(new DoubleVal(v)); | |
} | |
public Value intValue(int v) { | |
return new Value(new IntVal(v)); | |
} | |
public Value2 intValue2(int v) { | |
return new Value2(new IntVal(v)); | |
} | |
public static void main(String[] args) { | |
GenericValue2 test = new GenericValue2(); | |
Value v1 = test.doubleValue(8.056); | |
Value vi1 = test.intValue(10); | |
System.out.println(vi1.add(v1)); | |
System.out.println(v1.add(vi1)); | |
System.out.println(vi1.add(vi1)); | |
Value v2 = test.doubleValue(10.2); | |
Value v3 = v1.add(v2); | |
System.out.println(v3); | |
Value2 vi2 = test.intValue2(50); | |
Value vi3 = vi2.add(vi2); | |
System.out.println(vi3); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment