# Is my java sol readable?

• ``````import java.util.EnumSet;
``````

import java.util.EnumMap;

public class Solution {
public static enum Direction {

``````    NORTH, WEST, SOUTH, EAST;

private Direction previus;
private Direction next;
private EnumSet<Direction> bariers;

static {
NORTH.previus = EAST;
WEST.previus = NORTH;
SOUTH.previus = WEST;
EAST.previus = SOUTH;

NORTH.next = WEST;
WEST.next = SOUTH;
SOUTH.next = EAST;
EAST.next = NORTH;

NORTH.bariers = EnumSet.of(WEST, EAST);
WEST.bariers = EnumSet.of(NORTH, SOUTH);
SOUTH.bariers = EnumSet.of(WEST, EAST);
EAST.bariers = EnumSet.of(NORTH, SOUTH);
}

public Direction getPrevius() {
return previus;
}

public Direction getNext() {
return next;
}

public EnumSet<Direction> getBariers() {
return bariers;
}
}

public static class Edge {
public Edge(int xb, int yb, int xe, int ye) {
this.xb = xb;
this.yb = yb;
this.xe = xe;
this.ye = ye;
}

public boolean intersected(Edge other) { // (-2, -2, 2, -2) (-2, 1, -2, -2)
int x = xb == xe? xb:other.xb;  // -2
int xmin = xb != xe? Math.min(xb, xe):Math.min(other.xb, other.xe); // -2
int xmax = xb != xe? Math.max(xb, xe):Math.max(other.xb, other.xe); // 2

int y = yb == ye? yb:other.yb; // -2
int ymin = yb != ye? Math.min(ye, yb):Math.min(other.ye, other.yb); // 1
int ymax = yb != ye? Math.max(ye, yb):Math.max(other.ye, other.yb); // -2

return xmin <= x && x <= xmax && ymin <= y && y <= ymax;
}

static public Edge create(Edge prev, int l, Direction direction) {
int xb = prev.xe;
int yb = prev.ye;

switch (direction) {
case NORTH: return new Edge(xb, yb, xb, yb + l);
case WEST:  return new Edge(xb, yb, xb - l, yb);
case SOUTH: return new Edge(xb, yb, xb, yb - l);
case EAST:  return new Edge(xb, yb, xb + l, yb);
default:    return null;
}
}

private final int xb;
private final int yb;
private final int xe;
private final int ye;
}

public boolean isSelfCrossing(int[] x) { // [1,2,3,4]
if (x.length < 4)
return false;

Edge firstNorth = new Edge(0, 0, 0, x[0]); // (0, 0, 0, 1)
Edge firstWest = Edge.create(firstNorth, x[1], Direction.WEST); // (0, 1, -2, 1)
Edge firstSouth = Edge.create(firstWest, x[2], Direction.SOUTH); // (-2, 1, -2, -2)

EnumMap<Direction, Edge> previus = new EnumMap<Direction, Edge>(Direction.class);
previus.put(Direction.NORTH, firstNorth);
previus.put(Direction.WEST, firstWest);

Direction direction = Direction.EAST;
Edge last = firstSouth;

for (int i = 3; i < x.length; i++) {
Edge current = Edge.create(last, x[i], direction); // (-2, -2, 2, -2)
if (current.xe == 0 && current.ye == 0)
return true;

for(Direction barier: direction.getBariers()) { // NORTH, SOUTH
Edge barierEnge = previus.get(barier);
if (barierEnge != null && current.intersected(previus.get(barier)))
return true;
}

previus.put(direction.getPrevius(), last);
direction = direction.getNext();
last = current;
}

return false;
}
``````

}

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.