JavaGroovyEquivalents { @SuppressWarnings("unchecked") public static <T> Collection<T> elvis(Collection<T> preferred, Collection<?> fallback) { return groovyTruth(preferred) ? preferred : (Collection<T>) fallback; }  static String join(Collection<?> collection, String separator); @SuppressWarnings("unchecked") static Collection<T> elvis(Collection<T> preferred, Collection<?> fallback); static String elvis(String preferred, String fallback); static String elvisString(Object preferred, Object fallback); @SuppressWarnings("unchecked") static T elvis(T preferred, T fallback); static T elvis(Iterable<?> preferences); @SuppressWarnings("unchecked") static T elvis(Object... preferences); static Object fix(Object o); static String asString(Object o); static String asNonnullString(Object o); static boolean groovyTruth(Collection<?> c); static boolean groovyTruth(String s); static boolean groovyTruth(Object o); static Predicate<T> groovyTruthPredicate(); static Function<Object,Boolean> groovyTruthFunction(); static Map<K,V> mapOf(K key1, V val1); @Deprecated static TimeDuration toTimeDuration(Object duration); @Deprecated static Predicate<T> toPredicate(final Closure<Boolean> c); @SuppressWarnings("unchecked") static Callable<T> toCallable(final Runnable job);  }
@Test public void testElvis() { final List<?> emptyList = ImmutableList.of(); final List<?> singletonList = ImmutableList.of("myVal"); final List<?> differentList = ImmutableList.of("differentVal"); assertEquals(elvis("", "string2"), "string2"); assertEquals(elvis("string1", "string2"), "string1"); assertEquals(elvis(null, "string2"), "string2"); assertEquals(elvis("", "string2"), "string2"); assertEquals(elvis(1, 2), Integer.valueOf(1)); assertEquals(elvis(0, 2), Integer.valueOf(2)); assertEquals(elvis(singletonList, differentList), singletonList); assertEquals(elvis(emptyList, differentList), differentList); assertEquals(elvis(gstring, "other"), gstringVal); assertEquals(elvis(emptyGstring, "other"), "other"); assertEquals(elvis(emptyGstring, gstring), gstringVal); }