CuboidUtil { public static int getLongestDepth(Set<Long> cuboidSet) { Map<Long, List<Long>> directChildrenCache = CuboidStatsUtil.createDirectChildrenCache(cuboidSet); List<Long> cuboids = Lists.newArrayList(cuboidSet); Collections.sort(cuboids, new Comparator<Long>() { @Override public int compare(Long o1, Long o2) { return -Long.compare(o1, o2); } }); int longestDepth = 0; Map<Long, Integer> cuboidDepthMap = Maps.newHashMap(); for (Long cuboid : cuboids) { int parentDepth = cuboidDepthMap.get(cuboid) == null ? 0 : cuboidDepthMap.get(cuboid); for (Long childCuboid : directChildrenCache.get(cuboid)) { if (cuboidDepthMap.get(childCuboid) == null || cuboidDepthMap.get(childCuboid) < parentDepth + 1) { cuboidDepthMap.put(childCuboid, parentDepth + 1); if (longestDepth < parentDepth + 1) { longestDepth = parentDepth + 1; } } } } return longestDepth; }  static Integer[][] getCuboidBitSet(Long[] cuboidIds, int nRowKey); static int getLongestDepth(Set<Long> cuboidSet);  }
@Test public void testGetLongestDepth() { Stopwatch sw = new Stopwatch(); Set<Long> cuboidSet1 = Sets.newHashSet(7L, 6L, 5L, 4L, 3L, 2L, 1L); sw.start(); assertEquals(2, CuboidUtil.getLongestDepth(cuboidSet1)); System.out.println("Time cost for GetLongestDepth: " + sw.elapsed(TimeUnit.MILLISECONDS) + "ms"); Set<Long> cuboidSet2 = Sets.newHashSet(1024L, 1666L, 1667L, 1728L, 1730L, 1731L, 1760L, 1762L, 1763L, 1776L, 1778L, 1779L, 1784L, 1788L, 1790L, 1791L, 1920L, 1922L, 1923L, 1984L, 1986L, 1987L, 2016L, 2018L, 2019L, 2032L, 2034L, 2035L, 2040L, 2044L, 2046L, 2047L); sw.reset(); sw.start(); assertEquals(8, CuboidUtil.getLongestDepth(cuboidSet2)); System.out.println("Time cost for GetLongestDepth: " + sw.elapsed(TimeUnit.MILLISECONDS) + "ms"); Set<Long> cuboidSet3 = Sets.newHashSet(31L, 11L, 5L, 3L, 1L); sw.reset(); sw.start(); assertEquals(3, CuboidUtil.getLongestDepth(cuboidSet3)); System.out.println("Time cost for GetLongestDepth: " + sw.elapsed(TimeUnit.MILLISECONDS) + "ms"); sw.stop(); }