Created
August 2, 2017 17:54
-
-
Save pythoneer/f7d4478b8f7ea9f7cbb8be837eddad4e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class Main { | |
//Java has no `add` interface | |
interface Addition<T> { | |
T add(T other); | |
T init(); | |
} | |
//fucked up wrapper for Integers, Java has no `add` interface | |
public static class AInteger implements Addition<AInteger> { | |
private int inner; | |
public AInteger(int inner) { | |
this.inner = inner; | |
} | |
public int getInner() { | |
return inner; | |
} | |
public void setInner(int inner) { | |
this.inner = inner; | |
} | |
@Override | |
public AInteger add(AInteger other) { | |
return new AInteger(this.inner + other.inner); | |
} | |
@Override | |
public AInteger init() { | |
return new AInteger(0); | |
} | |
@Override | |
public String toString() { | |
return String.valueOf(inner); | |
} | |
} | |
//fucked up wrapper for Floats, Java has no `add` interface | |
public static class AFloat implements Addition<AFloat> { | |
private float inner; | |
public AFloat(float inner) { | |
this.inner = inner; | |
} | |
public float getInner() { | |
return inner; | |
} | |
public void setInner(float inner) { | |
this.inner = inner; | |
} | |
@Override | |
public AFloat add(AFloat other) { | |
return new AFloat(this.inner + other.inner); | |
} | |
@Override | |
public AFloat init() { | |
return new AFloat(0); | |
} | |
@Override | |
public String toString() { | |
return String.valueOf(inner); | |
} | |
} | |
//an Iterator over types that can be added | |
interface AIterator<T extends Addition> { | |
T next(); | |
} | |
//an Iterator from 1 - 10 with integers | |
public static class OneToTen implements AIterator<AInteger> { | |
private AInteger state = new AInteger(0); | |
@Override | |
public AInteger next() { | |
if (state.getInner() < 10) { | |
state.setInner(state.getInner() + 1); | |
return state; | |
} else { | |
return state; | |
} | |
} | |
} | |
//an Iterator from ~3 - ~5 with floats | |
public static class ThreeToFive implements AIterator<AFloat> { | |
private AFloat state = new AFloat(3); | |
@Override | |
public AFloat next() { | |
if( state.getInner() < 5) { | |
state.setInner(state.getInner() + 0.2f); | |
return state; | |
} else { | |
return state; | |
} | |
} | |
} | |
public static void main(String[] args) { | |
final OneToTen oneToTen = new OneToTen(); | |
//needs an AInteger specimen and is not typesafe. I could also pass an AFloat and save it in | |
//an AFloat (to preserve the bounds) which would result in a ClassCastException at *.add()`unchecked cast` | |
final AInteger sumI = showIterator(oneToTen, new AInteger(10)); | |
System.out.println("sum: " + sumI); | |
System.out.println("\n"); | |
final ThreeToFive threeToFive = new ThreeToFive(); | |
final AFloat sumF = showIterator(threeToFive, new AFloat(10.0f)); //needs an AFloat specimen | |
System.out.println("sum: " + sumF); | |
} | |
//can print an AIterator and sum up what it is emitting | |
public static <T extends Addition> T showIterator(AIterator ai, T elem) { | |
T sum = (T) elem.init(); //unchecked cast! init only works for an instance of T | |
for(int i = 0; i < 12; i++) { | |
final Addition next = ai.next(); | |
sum = (T) sum.add(next); //unchecked cast! | |
System.out.println("ai: " + next); | |
} | |
return sum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment