class NodeCheck {

    public static boolean isValidNode(final Node n) {
        if (n == null) {
            return false;
        }
        // node degree is 2
        int degree = n.getNumberConnectedLines();
        if (degree == DEGREE_2) {
            Iterator<? extends Line> iter = n.getConnectedLines();
            Line line1 = iter.next();
            Line line2 = iter.next();
            // if isPair then we have a dead-end street
            // otherwise we can travel along the streets without coming back to
            // the start point
            return isPair(line1, line2);
        }
        // node degree is 4
        if (degree == DEGREE_4) {
            Iterator<? extends Line> connectedLines = n.getConnectedLines();
            List<Line> lines = new ArrayList<Line>();
            while (connectedLines.hasNext()) {
                lines.add(connectedLines.next());
            }

            // find two pairs within this array
            // if there are two pairs then the node is avoidable otherwise we
            // are connecting more than
            // two nodes and this node needs to be considered
            Line line = lines.get(0);
            boolean found = false;
            int pos = 1;
            // find a pair containing the first line and delete both lines
            while (!found && pos < lines.size()) {
                if (isPair(line, lines.get(pos))) {
                    found = true;
                    lines.remove(pos);
                    lines.remove(0);
                }
                pos++;
            }
            // if no pair containing the first line can be found then we have a
            // considerable node
            if (!found) {
                return true;
            }
            // check the remaining edges if they form a pair and if so, then
            // avoid the node
            if (isPair(lines.get(0), lines.get(1))) {
                return false;
            }
            return true;
        }
        return true;
    }

    private  NodeCheck();

}