ListUtils { public static <T> boolean addIfNotPresent(List<? super T> list, T element) { if (!list.contains(element)) { return list.add(element); } return false; } private  ListUtils(); static int firstIndexOf(List<? extends T> list, Predicate<? super T> predicate); static boolean addIfNotPresent(List<? super T> list, T element); static boolean addIfNotPresent(List<? super T> list, int index, T element); static Collector<T, ?, ImmutableList<T>> toImmutableList(); static Collector<T, ?, List<T>> joining(Supplier<? extends T> separator); static Collector<T, ?, List<T>> joining(Supplier<? extends T> prefix,
                                                     Supplier<? extends T> separator,
                                                     Supplier<? extends T> suffix); static Replacement<T> replaceIn(List<T> list);  }
@Test public void testAddIfNotPresentNoIndex() { List<String> list = new ArrayList<>(); list.add(foo); assertAll( () -> assertFalse(ListUtils.addIfNotPresent(list, foo)), () -> assertTrue(ListUtils.addIfNotPresent(list, bar)), () -> assertFalse(ListUtils.addIfNotPresent(list, bar)), () -> assertEquals(2, list.size()) ); }
@Test public void testAddIfNotPresentToIndex() { List<String> list = new ArrayList<>(); list.add(foo); assertAll( () -> assertTrue(ListUtils.addIfNotPresent(list, 0, bar)), () -> assertFalse(ListUtils.addIfNotPresent(list, 0, bar)), () -> assertEquals(0, list.indexOf(bar)), () -> assertEquals(1, list.indexOf(foo)), () -> assertEquals(2, list.size()) ); }