Some days I manage to really amuse myself with my work. Today I've added some flare to a method that may otherwise be really boring. So let me introduce you to my new favorite method:
/** * Flattens the sub directories of <tt>roots</tt> into a single array. * * @param roots The root directories. * * @return The [sorted] subdirectories of <tt>roots</tt>. */ private static File[] merge(File ... roots) { int x = 0; int k = 0; File[][] forest = new File[roots.length][]; for (File sapling : roots) { File[] tree = sapling.listFiles(); if (null != tree) { int leaves = tree.length; if (leaves > 0) { forest[x++] = tree; k += leaves; } } } File[] woods = new File[k]; for (k = 0; --x >= 0;) { File[] tree = forest[x]; int leaves = tree.length; System.arraycopy(tree, 0, woods, k, leaves); k += leaves; } Arrays.sort(woods, chipper); return woods; }
It cracks me up sometimes that I get paid to have this much fun.
No comments:
Post a Comment