AssembleUtil { @SuppressWarnings("unchecked") public static <T extends Comparable<? super T>>T[] retain(final T[] a, final T[] b, final int i, final int j, final int r) { for (int d = 0;; ++d) { int comparison = 0; if (i + d == a.length || j + d == b.length || (comparison = a[i + d].compareTo(b[j + d])) != 0) { final T[] retained; if (i + d == a.length || j + d == b.length) retained = r + d == 0 ? null : (T[])Array.newInstance(a.getClass().getComponentType(), r + d); else if (comparison < 0) retained = retain(a, b, i + d + 1, j + d, r + d); else retained = retain(a, b, i + d, j + d + 1, r + d); if (d > 0) System.arraycopy(a, i, retained, r, d); return retained; } } } private  AssembleUtil(); static boolean hasFileInJar(final File jarFile, final String name); static String readFileFromJar(final File jarFile, final String name); static Set<String> getLocations(final Class<?> ... classes); static byte[] readBytes(final URL url); static byte[] readBytes(final InputStream in); static String toIndentedString(final Object[] a); static String toIndentedString(final Collection<?> l); static String toIndentedString(final Map<?,?> m); static boolean recurseDir(final File dir, final Predicate<File> predicate); static FileVisitResult recurseDir(final File dir, final Function<File,FileVisitResult> function); static int compare(final T[] a, final T[] b); static int compare(final List<T> a, final List<T> b); static boolean containsAll(final T[] a, final T[] b); static boolean containsAll(final T[] a, final T[] b, final Comparator<T> c); @SuppressWarnings("unchecked") static T[] retain(final T[] a, final T[] b, final int i, final int j, final int r); static T[] sort(final T[] array); static List<T> sort(final List<T> list); static String getNameId(final Object obj); static String getSimpleNameId(final Object obj); static String toString(final Object[] a, final String del); static String toString(final Collection<?> c, final String del); static void absorbProperties(final String command); static void forEachClass(final URL[] urls, final T arg, final BiConsumer<String,T> consumer); static URL toURL(final File file); static URL[] toURLs(final File ... files); static URL[] toURLs(final List<File> files); static URL[] toURLs(final Collection<File> files); static String convertToRegex(String pattern); static String classNameToResource(final String className); static String classNameToResource(final Class<?> cls); static String resourceToClassName(final String resource); static boolean isSystemProperty(final String key, final String deprecatedKey); static Pattern convertToNameRegex(String pattern); static File getSourceLocation(final URL url, final String resourcePath); static String getName(final String path); static File[] classPathToFiles(final String classpath); static URL toURL(final String path);  }
@Test public void testRetain() { String[] a, b, r; a = new String[] {"a", "b", "c", "d"}; b = new String[] {"a", "b", "c", "d"}; r = AssembleUtil.retain(a, b, 0, 0, 0); assertArrayEquals(a, r); a = new String[] {"b", "c", "d"}; b = new String[] {"a", "b", "c", "d"}; r = AssembleUtil.retain(a, b, 0, 0, 0); assertArrayEquals(a, r); a = new String[] {"a", "b", "c", "d"}; b = new String[] {"b", "c", "d"}; r = AssembleUtil.retain(a, b, 0, 0, 0); assertArrayEquals(b, r); a = new String[] {"a", "b", "c"}; b = new String[] {"a", "b", "c", "d"}; r = AssembleUtil.retain(a, b, 0, 0, 0); assertArrayEquals(a, r); a = new String[] {"a", "b", "c", "d"}; b = new String[] {"a", "b", "c"}; r = AssembleUtil.retain(a, b, 0, 0, 0); assertArrayEquals(b, r); a = new String[] {"a", "b", "d"}; b = new String[] {"a", "b", "c", "d"}; r = AssembleUtil.retain(a, b, 0, 0, 0); assertArrayEquals(a, r); a = new String[] {"a", "b", "c", "d"}; b = new String[] {"a", "b", "d"}; r = AssembleUtil.retain(a, b, 0, 0, 0); assertArrayEquals(b, r); a = new String[] {"a", "c", "d"}; b = new String[] {"a", "b", "c", "d"}; r = AssembleUtil.retain(a, b, 0, 0, 0); assertArrayEquals(a, r); a = new String[] {"a", "b", "c", "d"}; b = new String[] {"a", "c", "d"}; r = AssembleUtil.retain(a, b, 0, 0, 0); assertArrayEquals(b, r); a = new String[] {"a", "d"}; b = new String[] {"a", "b", "c", "d"}; r = AssembleUtil.retain(a, b, 0, 0, 0); assertArrayEquals(a, r); a = new String[] {"a", "b", "c", "d"}; b = new String[] {"a", "d"}; r = AssembleUtil.retain(a, b, 0, 0, 0); assertArrayEquals(b, r); a = new String[] {"a", "c", "d"}; b = new String[] {"a", "b", "d"}; r = AssembleUtil.retain(a, b, 0, 0, 0); assertArrayEquals(new String[] {"a", "d"}, r); a = new String[] {"a", "b", "d"}; b = new String[] {"a", "c", "d"}; r = AssembleUtil.retain(a, b, 0, 0, 0); assertArrayEquals(new String[] {"a", "d"}, r); a = new String[] {"c", "d"}; b = new String[] {"a", "b", "d"}; r = AssembleUtil.retain(a, b, 0, 0, 0); assertArrayEquals(new String[] {"d"}, r); a = new String[] {"a", "b"}; b = new String[] {"a", "c"}; r = AssembleUtil.retain(a, b, 0, 0, 0); assertArrayEquals(new String[] {"a"}, r); a = new String[] {"a", "b"}; b = new String[] {"c", "d"}; r = AssembleUtil.retain(a, b, 0, 0, 0); assertNull(r); }