private static String calcShapeGraphSize(ShapeImpl shape) { class Visitor implements DebugShapeVisitor<Integer> { final Set<Shape> visitedShapes = new HashSet<>(); int invalidShapeCount; int branchCount = 1; int leafCount; @Override public Integer visitShape(ShapeImpl s, Map<? extends Transition, ? extends ShapeImpl> transitions) { if (!visitedShapes.add(s)) { return 0; } int shapeCount = 1; if (!s.isValid()) { invalidShapeCount++; } if (s.isLeaf()) { leafCount++; } branchCount += Math.max(0, transitions.size() - 1); for (Map.Entry<? extends Transition, ? extends ShapeImpl> entry : transitions.entrySet()) { shapeCount += this.visitShape(entry.getValue()); } return shapeCount; } } Visitor v = new Visitor(); int shapeCount = v.visitShape(shape); assert shapeCount == v.visitedShapes.size(); return shapeCount + (v.invalidShapeCount != 0 ? (", " + INVALID + v.invalidShapeCount) : "") + ", " + BRANCH + v.branchCount + ", " + LEAF + v.leafCount; }
private static String calcShapeGraphSize(ShapeImpl shape) { class Visitor implements DebugShapeVisitor<Integer> { final Set<Shape> visitedShapes = new HashSet<>(); int invalidShapeCount; int branchCount = 1; int leafCount; @Override public Integer visitShape(ShapeImpl s, Map<? extends Transition, ? extends ShapeImpl> transitions) { if (!visitedShapes.add(s)) { return 0; } int shapeCount = 1; if (!s.isValid()) { invalidShapeCount++; } if (s.isLeaf()) { leafCount++; } branchCount += Math.max(0, transitions.size() - 1); for (Map.Entry<? extends Transition, ? extends ShapeImpl> entry : transitions.entrySet()) { shapeCount += this.visitShape(entry.getValue()); } return shapeCount; } } Visitor v = new Visitor(); int shapeCount = v.visitShape(shape); assert shapeCount == v.visitedShapes.size(); return shapeCount + (v.invalidShapeCount != 0 ? (", " + INVALID + v.invalidShapeCount) : "") + ", " + BRANCH + v.branchCount + ", " + LEAF + v.leafCount; }