fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class Ref<T> {
  8. private T item = null;
  9. public Ref() {}
  10. public Ref(T item) { this.item = item; }
  11. public T get() { return item; }
  12. public void set(T newValue) { item = newValue; }
  13. }
  14.  
  15. class RefPair<A, B> {
  16. private final Ref<A> aRef;
  17. private final Ref<B> bRef;
  18.  
  19. public RefPair() {
  20. this(new Ref<A>(), new Ref<B>());
  21. }
  22.  
  23. private RefPair(Ref<A> aRef, Ref<B> bRef) {
  24. this.aRef = aRef;
  25. this.bRef = bRef;
  26. }
  27.  
  28. public RefPair<B, A> inverted() { return new RefPair<>(bRef, aRef); }
  29.  
  30. public A getFirst() { return aRef.get(); }
  31. public void setFirst(A newA) { aRef.set(newA); }
  32. public B getSecond() { return bRef.get(); }
  33. public void setSecond(B newB) { bRef.set(newB); }
  34. }
  35.  
  36. enum Flipper {
  37. A(makePair()),
  38. B(makePair()),
  39. Y(B.pair.inverted()),
  40. Z(A.pair.inverted());
  41.  
  42. private final RefPair<Flipper, Flipper> pair;
  43.  
  44. private Flipper(RefPair<Flipper, Flipper> pair) {
  45. this.pair = pair;
  46. pair.setFirst(this);
  47. }
  48.  
  49. public Flipper flip() { return pair.getSecond(); }
  50.  
  51. private static RefPair<Flipper, Flipper> makePair() {
  52. return new RefPair();
  53. }
  54. }
  55.  
  56. /* Name of the class has to be "Main" only if the class is public. */
  57. class Ideone
  58. {
  59. public static void main (String[] args) throws java.lang.Exception
  60. {
  61. System.out.println(Flipper.A);
  62. System.out.println(Flipper.A.flip());
  63. System.out.println(Flipper.Z);
  64. System.out.println(Flipper.Z.flip());
  65. }
  66. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
A
Z
Z
A