70

I'm programming a game in java which is made up of a grid of tiles. I wan't to be able to inuitively define the edges of the tiles and how they relate to each other, e.g. to get the opposite edge of a tile, I want to be able to just type TOP.opposite(). However, when using enums to define these edges I end up having to forward reference at least two of them in the contstructor:

public enum Edge {

   TOP(Edge.BOTTOM), //illegal forward reference
   BOTTOM(Edge.TOP),
   LEFT(Edge.RIGHT), //illegal forward reference
   RIGHT(Edge.LEFT);

   private Edge opposite;

   private Edge(Edge opp){
      this.opposite = opp;
   }

   public Edge opposite(){
      return this.opposite;
   }
}

Is there any way of getting round this problem using enums which is just as simple?

MattLBeck
  • 5,701
  • 7
  • 40
  • 56
  • For those who are curios about the source of this problem, I leave the link to [JSL doc section](https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.3.3) that explains why you may experience this. – Mikhail2048 Aug 10 '22 at 19:43

11 Answers11

93

You can do this which is not as intuitive.

public enum Edge {
    TOP, BOTTOM, LEFT, RIGHT;
    private Edge opposite;

    static {
        TOP.opposite = BOTTOM;
        BOTTOM.opposite = TOP;
        LEFT.opposite = RIGHT;
        RIGHT.opposite = LEFT;
    }
    public Edge opposite(){
        return this.opposite;
    }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Unfortunately, if the enum is needed in the static initializer of another class, then you can get the exception `class .....EnumName not an enum`. I will have to use Maps... – P.Péter Feb 18 '15 at 16:55
  • @P.Péter There should be no problem using an Enum anywhere in another class, static initializer or anywhere else. – Peter Lawrey Feb 18 '15 at 17:18
  • @PeterLawrey You are right, I made another mistake which resulted in the mentioned exception. Your solution works just fine! – P.Péter Feb 24 '15 at 15:21
  • Different when the property is final and public. – TheRealChx101 Mar 27 '17 at 01:56
  • @MattLBeck Is it guaranteed that reference on enum instance has already run the static block when client receives it? I suspect you are leaking not fully iniliazed enum instance. The line `TOP.opposite = BOTTOM` Just after that if client calls TOP.opposite.opposite you will get in some cases undefined behaviour. – Rob May 18 '17 at 08:02
  • * clients calls `Edge.TOP.opposite().opposite()` – Rob May 18 '17 at 08:13
26
enum Edge {
    TOP {
        @Override
        public Edge opposite() {
            return BOTTOM;
        }
    },
    BOTTOM {
        @Override
        public Edge opposite() {
            return TOP;
        }
    },
    LEFT {
        @Override
        public Edge opposite() {
            return RIGHT;
        }
    },
    RIGHT {
        @Override
        public Edge opposite() {
            return LEFT;
        }
    };

    public abstract Edge opposite();
}
Vitaliy Oliynyk
  • 429
  • 5
  • 3
13
public enum Edge {

    TOP,
    BOTTOM(Edge.TOP),
    LEFT,
    RIGHT(Edge.LEFT);

    private Edge opposite;

    private Edge() {

    }
    private Edge(Edge opp) {
        this.opposite = opp;
        opp.opposite = this;
    }

    public Edge opposite() {
        return this.opposite;
    }
}
eyelash
  • 3,197
  • 25
  • 33
11

You can also make use of an static innerclass inside the enum:

public enum EnumTest     
{     
NORTH( Orientation.VERTICAL ),     
SOUTH( Orientation.VERTICAL ),     
EAST( Orientation.HORIZONTAL ),     
WEST( Orientation.HORIZONTAL );     

private static class Orientation  
{  
private static final String VERTICAL = null;     
private static final String HORIZONTAL = null;     
}
}

Stolen from here :)

jacktrades
  • 7,224
  • 13
  • 56
  • 83
9

Here's another way

public enum Edge {

    TOP("BOTTOM"),
    BOTTOM("TOP"),
    LEFT("RIGHT"),
    RIGHT("LEFT");

    private String opposite;

    private Edge(String opposite){
        this.opposite = opposite;
    }

    public Edge opposite(){
        return valueOf(opposite);
    }

}

Peter Lawrey's solution is however more efficient and compiletime safe.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
7

Add a method opposite to return enum object

You could just define a method, opposite().

In modern Java, switch expression

In modern Java, we can use a switch expression. The compiler ensures that we have covered all possible cases.

enum Edge
{
    TOP, BOTTOM, LEFT, RIGHT;

    public Edge opposite ( )
    {
        return switch ( this )
                {
                    case TOP -> BOTTOM;
                    case BOTTOM -> TOP;
                    case LEFT -> RIGHT;
                    case RIGHT -> LEFT;
                };
    }
}

Usage:

System.out.println( Edge.TOP.opposite() );

BOTTOM

In earlier Java, switch

In older Java, use syntax seen in the following code.

Notice the need for a default case, in case you ever add an element to the enum or you inadvertently delete a case from the switch.

public enum Edge {
    TOP,
    BOTTOM,
    LEFT,
    RIGHT;

    public Edge opposite() {
        switch (this) {
            case TOP:
                return BOTTOM;
            case BOTTOM:
                return TOP;
            case LEFT:
                return RIGHT;
            case RIGHT:
                return LEFT;
            default:
                throw new RuntimeException("Oh dear");
        }
    }
}
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
3

You could use an internal Map instead to define these associations. This works if at the point of initializing the Map, you already have all enum values created:

public enum Edge {

  TOP,
  BOTTOM,
  LEFT,
  RIGHT;

  private static final Map<Edge, Edge> opposites = 
        new EnumMap<Edge, Edge>(Edge.class);
  static {
    opposites.put(TOP, BOTTOM);
    opposites.put(BOTTOM, TOP);
    opposites.put(LEFT, RIGHT);
    opposites.put(RIGHT, LEFT);
  }

  public Edge opposite(){
    return opposites.get(this);
  }
}
Péter Török
  • 114,404
  • 31
  • 268
  • 329
3

You can create a static Map where key is the original enum and the value the opposite edge. Initialize it in a static block and the return the mapping from the opposite() method.

private static Map<Edge, Edge> oppostiteMapping;

static {
  oppositeMapping = new EnumMap<Edge, Edge>();
  oppositeMapping.put(TOP, BOTTOM);
  ...
}

public Edge opposite() {
    return oppositeMapping.get(this);
} 

EDIT: as proposed in comment better to use EnumMap, so I upgraded accordingly

Btw. this approach is generally useful when you create something like static fromString() method etc.

Jan Zyka
  • 17,460
  • 16
  • 70
  • 118
2

My method is by using ordinal. This is a simple example, but for a much more complex example see below.

public enum Edge {
    // Don't change the order! This class uses ordinal() in an arithmetic context.
    TOP,    // = 0
    LEFT,   // = 1
    RIGHT,  // = 2
    BOTTOM; // = 3

    public Edge other() {
        return values()[3 - ordinal()];
    }
}

Although using ordinal is discouraged for being fragile, using ordinal in the same enum as it's defined in is less fragile, and it's further mitigated here with a comment. Though the example above is quite trivial, the next example is less so. Compare the original way and the way using ordinal:

From 98 lines:

public enum Axes {
    NONE,
    HORIZONTAL,
    VERTICAL,
    BOTH;

    public Axes add(Axes axes) {
        switch (axes) {
            case HORIZONTAL:
                if (this == NONE)
                    return HORIZONTAL;
                if (this == VERTICAL)
                    return BOTH;
                break;
            case VERTICAL:
                if (this == NONE)
                    return VERTICAL;
                if (this == HORIZONTAL)
                    return BOTH;
                break;
            case BOTH:
                return BOTH;
            default:
                throw new AssertionError(axes);
        }
        return this;
    }

    public Axes remove(Axes axes) {
        switch (axes) {
            case HORIZONTAL:
                if (this == HORIZONTAL)
                    return NONE;
                if (this == BOTH)
                    return VERTICAL;
                break;
            case VERTICAL:
                if (this == VERTICAL)
                    return NONE;
                if (this == BOTH)
                    return HORIZONTAL;
                break;
            case BOTH:
                return NONE;
            default:
                throw new AssertionError(axes);
        }
        return this;
    }

    public Axes toggle(Axes axes) {
        switch (axes) {
            case NONE:
                return this;
            case HORIZONTAL:
                switch (this) {
                    case NONE:
                        return HORIZONTAL;
                    case HORIZONTAL:
                        return NONE;
                    case VERTICAL:
                        return BOTH;
                    case BOTH:
                        return VERTICAL;
                    default:
                        throw new AssertionError(axes);
                }
            case VERTICAL:
                switch (this) {
                    case NONE:
                        return VERTICAL;
                    case HORIZONTAL:
                        return BOTH;
                    case VERTICAL:
                        return NONE;
                    case BOTH:
                        return HORIZONTAL;
                    default:
                        throw new AssertionError(axes);
                }
            case BOTH:
                switch (this) {
                    case NONE:
                        return BOTH;
                    case HORIZONTAL:
                        return VERTICAL;
                    case VERTICAL:
                        return HORIZONTAL;
                    case BOTH:
                        return NONE;
                    default:
                        throw new AssertionError(axes);
                }
            default:
                throw new AssertionError(axes);
        }
    }
}

to 19 lines:

public enum Axes {
    // Don't change the order! This class uses ordinal() as a 2-bit bitmask.
    NONE,       // = 0 = 0b00
    HORIZONTAL, // = 1 = 0b01
    VERTICAL,   // = 2 = 0b10
    BOTH;       // = 3 = 0b11

    public Axes add(Axes axes) {
        return values()[ordinal() | axes.ordinal()];
    }

    public Axes remove(Axes axes) {
        return values()[ordinal() & ~axes.ordinal()];
    }

    public Axes toggle(Axes axes) {
        return values()[ordinal() ^ axes.ordinal()];
    }
}
Mark Jeronimus
  • 9,278
  • 3
  • 37
  • 50
0

I preferred this:

public enum Edge {
   TOP,
   BOTTOM,
   LEFT,
   RIGHT;

   private Link link;

   private Link getLink() {
     if (link == null) {
        link = Link.valueOf(name());
     }
     return link;
   }

   public Edge opposite() {
      return getLink().opposite();
   }
}

public enum Link {
   TOP(Edge.BOTTOM),
   BOTTOM(Edge.TOP),
   LEFT(Edge.RIGHT),
   RIGHT(Edge.LEFT);

   private Edge opposite;

   private Link(Edge opp) {
      this.opposite = opp;
   }

   public Edge opposite() {
      return this.opposite;
   }
}
vitali_y
  • 400
  • 6
  • 13
0

With Java 8 lambdas:

public enum Edge {
  TOP(() -> Edge.BOTTOM),
  BOTTOM(() -> Edge.TOP),
  LEFT(() -> Edge.RIGHT),
  RIGHT(() -> Edge.LEFT);

  private Supplier<Edge> opposite;

  private Edge(Supplier<Edge> opposite) {
    this.opposite = opposite;
  }

  public Edge opposite() {
    return opposite.get();
  }
}
Greg Dennis
  • 65
  • 1
  • 1
  • 2
    While theoretically this makes sense and seems like a lacy way to solve the problem this still results in the compiler error "illegal forward reference" – Spenhouet Oct 21 '17 at 16:38
  • I have to agree with @Spenhouet, I'm here because of the error showing up after using lambdas – funder7 Oct 02 '20 at 12:45
  • 1
    But I've solved it by using the solution in this answer: https://stackoverflow.com/questions/23608885/how-to-define-static-constants-in-a-java-enum – funder7 Oct 02 '20 at 12:52