Last active
January 3, 2016 19:49
-
-
Save deinspanjer/8511393 to your computer and use it in GitHub Desktop.
Example of a problem where I'm trying to have a class implement an interface but return a couple of extra methods, but I cannot use that class in places where an interface method expects the return type to be the base interface. Edit: Now with a solution.
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
import java.util.*; | |
public class Test { | |
public static interface Position {} | |
public static interface Axis { | |
public List<? extends Position> getPositions(); | |
} | |
public static class GoodAxis implements Axis { | |
private List<Position> positions = new ArrayList<>(); | |
@Override | |
public List<Position> getPositions() { | |
return positions; | |
} | |
} | |
public static class MyPosition implements Position { | |
public String exclaim() { return "YAY!"; } | |
} | |
public static class MyBadPositionList extends AbstractList<MyPosition> { | |
@Override | |
public MyPosition get(int index) { | |
return null; | |
} | |
@Override | |
public int size() { | |
return 0; | |
} | |
public String getExclamationForPosition(int index) { | |
return get(index).exclaim(); | |
} | |
} | |
/* XXX: If uncommented, javac reports incompatible types. | |
public static class BadAxis implements Axis { | |
private MyBadPositionList myPositions = new MyBadPositionList(); | |
@Override | |
public List<Position> getPositions() { | |
return myPositions; | |
} | |
} | |
*/ | |
/* XXX: If uncommented, javac reports an unchecked conversion | |
public static class MyOkayPositionList<T> extends AbstractList<MyPosition> { | |
@Override | |
public MyPosition get(int index) { | |
return null; | |
} | |
@Override | |
public int size() { | |
return 0; | |
} | |
public String getExclamationForPosition(int index) { | |
return get(index).exclaim(); | |
} | |
} | |
public static class OkayAxis implements Axis { | |
private MyOkayPositionList<MyPosition> myPositions = new MyOkayPositionList<MyPosition>(); | |
@Override | |
public List<MyPosition> getPositions() { | |
// XXX: javac reports unchecked conversion | |
return myPositions; | |
} | |
} | |
*/ | |
public static class MyGoodPositionList extends AbstractList<Position> { | |
private List<MyPosition> list = new ArrayList<>(); | |
@Override | |
public Position get(int index) { | |
return list.get(index); | |
} | |
@Override | |
public int size() { | |
return list.size(); | |
} | |
public String getExclamationForPosition(int index) { | |
return list.get(index).exclaim(); | |
} | |
public List<Position> getDowncastPositions() { | |
return new AbstractList<Position>() { | |
@Override | |
public Position get(int index) { | |
return list.get(index); | |
} | |
@Override | |
public int size() { | |
return size(); | |
} | |
}; | |
} | |
} | |
public static class MyGoodAxis implements Axis { | |
private MyGoodPositionList myPositions = new MyGoodPositionList(); | |
@Override | |
public List<Position> getPositions() { | |
return myPositions.getDowncastPositions(); | |
} | |
public String getExclamationForPosition(int index) { | |
return myPositions.getExclamationForPosition(index); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment