### Question:
SimilarNodeTreeTransformator extends AbstractTreeTransformingProcessor { public static ISerializedObjectTree transformTree(ISerializedObjectTree tree) { ISerializedObjectTree ret = tree; if (!tree.children().isEmpty()) { List<ISerializedObjectTree> transformed = new ArrayList<ISerializedObjectTree>(); for (ISerializedObjectTree t : tree.children()) { transformed.add(transformTree(t)); } TreeTypeMap typeMap = new TreeTypeMap(transformed); if (typeMap.hasLessEntries(transformed.size())) { List<ISerializedObjectTree> result = typeMap.compressedResult(); ret = new ImmutableTree(ret.id(), ret.type(), ret.label(), ret.size(), result); } else { if (!sameEntries(tree.children(), transformed)) { ret = new ImmutableTree(ret.id(), ret.type(), ret.label(), ret.size(), transformed); } } } return ret; } SimilarNodeTreeTransformator(ISerializedObjectTreeProcessor parent); static ISerializedObjectTree transformTree(ISerializedObjectTree tree);  }### Answer:
@Test public void dontChangeAnything() { ImmutableTree source = new ImmutableTree(new ObjectId(1), Root.class, "root", 10, noChildren()); ISerializedObjectTree result = SimilarNodeTreeTransformator.transformTree(source); ImmutableTree expected = new ImmutableTree(new ObjectId(1), Root.class, "root", 10, noChildren()); Trees.assertEqualsTree(expected, result); }
@Test public void compressSubNodes() { ImmutableTree source = new ImmutableTree(new ObjectId(1), Root.class, "root", 10, sameChildren5Times()); ISerializedObjectTree result = SimilarNodeTreeTransformator.transformTree(source); List<ISerializedObjectTree> oneSubNode = new ArrayList<>(); List<ISerializedObjectTree> threeSubNodes = new ArrayList<>(); threeSubNodes.add(new ImmutableTree(null, A.class, "a", 15, noChildren())); threeSubNodes.add(new ImmutableTree(null, B.class, "b", 20, noChildren())); threeSubNodes.add(new ImmutableTree(null, C.class, "c", 25, noChildren())); oneSubNode.add(new ImmutableTree(null, Has3.class, "has3-0|has3-1|has3-2|has3-3|has3-4", 50, threeSubNodes)); ImmutableTree expected = new ImmutableTree(new ObjectId(1), Root.class, "root", 10, oneSubNode); Trees.assertEqualsTree(expected, result); }
@Test public void testCompression() throws IOException { IReportOutput reportOutput=new LoggerReportOutput(); ISerializedObjectTree source = Trees.fromResource(getClass(), "sample1"); assertEquals("asSample", source.label()); ISerializedObjectTree result = SimilarNodeTreeTransformator.transformTree(source); new TreeSizeReport(reportOutput).process(result); ISerializedObjectTree match = Trees.fromResource(getClass(), "sample1-match"); new TreeSizeReport(reportOutput).process(match); Trees.assertEqualsTree(match, result); }
@Test public void secondNodeHasMoreChilds() throws IOException { IReportOutput reportOutput=new LoggerReportOutput(); ISerializedObjectTree source = Trees.fromResource(getClass(), "nodeChildSize"); assertEquals("asSample", source.label()); new TreeSizeReport(reportOutput).process(source); ISerializedObjectTree result = SimilarNodeTreeTransformator.transformTree(source); new TreeSizeReport(reportOutput).process(result); ISerializedObjectTree match = Trees.fromResource(getClass(), "nodeChildSize-match"); new TreeSizeReport(reportOutput).process(match); Trees.assertEqualsTree(match, result); }