@Test void testCreateCollectionFromPotentialCollection() { Collection<String> collectionFromElement = CollectionUtils.asCollection("item"); Collection<String> collectionFromNull = CollectionUtils.asCollection(null); Collection<String> collectionFromArray = CollectionUtils.asCollection(new String[]{"item1", "item2"}); Collection<String> collectionFromCollection = CollectionUtils.asCollection(asList("item1", "item2")); Collection<String> collectionFromSpliterator = CollectionUtils.asCollection(Spliterators.spliterator(new String[] {"item1", "item2"}, Spliterator.ORDERED)); Collection<String> collectionFromIterable = CollectionUtils.asCollection((Iterable) () -> asList("item1", "item2").iterator()); assertEquals(1, collectionFromElement.size()); assertEquals(0, collectionFromNull.size()); assertEquals(2, collectionFromArray.size()); assertEquals(2, collectionFromCollection.size()); assertEquals(2, collectionFromSpliterator.size()); assertEquals(2, collectionFromIterable.size()); }
@SuppressWarnings("unchecked") public static <R> Collection<R> asCollection(Object potentialCollection) { if (potentialCollection == null) { return Collections.emptyList(); } if (potentialCollection instanceof Collection) { return ((Collection<R>) potentialCollection); } if (potentialCollection.getClass().isArray()) { return Arrays.asList((R[]) potentialCollection); } if (potentialCollection instanceof Stream) { return ((Stream<R>) potentialCollection).collect(toList()); } if (potentialCollection instanceof Spliterator) { return StreamSupport.stream((Spliterator<R>) potentialCollection, false).collect(toList()); } if (potentialCollection instanceof Iterable) { return StreamSupport.stream(((Iterable<R>) potentialCollection).spliterator(), false).collect(toList()); } return Collections.singletonList((R) potentialCollection); }
CollectionUtils { @SuppressWarnings("unchecked") public static <R> Collection<R> asCollection(Object potentialCollection) { if (potentialCollection == null) { return Collections.emptyList(); } if (potentialCollection instanceof Collection) { return ((Collection<R>) potentialCollection); } if (potentialCollection.getClass().isArray()) { return Arrays.asList((R[]) potentialCollection); } if (potentialCollection instanceof Stream) { return ((Stream<R>) potentialCollection).collect(toList()); } if (potentialCollection instanceof Spliterator) { return StreamSupport.stream((Spliterator<R>) potentialCollection, false).collect(toList()); } if (potentialCollection instanceof Iterable) { return StreamSupport.stream(((Iterable<R>) potentialCollection).spliterator(), false).collect(toList()); } return Collections.singletonList((R) potentialCollection); } }
CollectionUtils { @SuppressWarnings("unchecked") public static <R> Collection<R> asCollection(Object potentialCollection) { if (potentialCollection == null) { return Collections.emptyList(); } if (potentialCollection instanceof Collection) { return ((Collection<R>) potentialCollection); } if (potentialCollection.getClass().isArray()) { return Arrays.asList((R[]) potentialCollection); } if (potentialCollection instanceof Stream) { return ((Stream<R>) potentialCollection).collect(toList()); } if (potentialCollection instanceof Spliterator) { return StreamSupport.stream((Spliterator<R>) potentialCollection, false).collect(toList()); } if (potentialCollection instanceof Iterable) { return StreamSupport.stream(((Iterable<R>) potentialCollection).spliterator(), false).collect(toList()); } return Collections.singletonList((R) potentialCollection); } private  CollectionUtils(); }
CollectionUtils { @SuppressWarnings("unchecked") public static <R> Collection<R> asCollection(Object potentialCollection) { if (potentialCollection == null) { return Collections.emptyList(); } if (potentialCollection instanceof Collection) { return ((Collection<R>) potentialCollection); } if (potentialCollection.getClass().isArray()) { return Arrays.asList((R[]) potentialCollection); } if (potentialCollection instanceof Stream) { return ((Stream<R>) potentialCollection).collect(toList()); } if (potentialCollection instanceof Spliterator) { return StreamSupport.stream((Spliterator<R>) potentialCollection, false).collect(toList()); } if (potentialCollection instanceof Iterable) { return StreamSupport.stream(((Iterable<R>) potentialCollection).spliterator(), false).collect(toList()); } return Collections.singletonList((R) potentialCollection); } private  CollectionUtils(); static T merge(T collection1, T collection2, Supplier<T> factoryMethod); @SuppressWarnings("unchecked") static Collection<R> asCollection(Object potentialCollection); static C intersect(Collection<? extends T> collection1, Collection<? extends T> collection2, Supplier<C> collectionBuilder); }
CollectionUtils { @SuppressWarnings("unchecked") public static <R> Collection<R> asCollection(Object potentialCollection) { if (potentialCollection == null) { return Collections.emptyList(); } if (potentialCollection instanceof Collection) { return ((Collection<R>) potentialCollection); } if (potentialCollection.getClass().isArray()) { return Arrays.asList((R[]) potentialCollection); } if (potentialCollection instanceof Stream) { return ((Stream<R>) potentialCollection).collect(toList()); } if (potentialCollection instanceof Spliterator) { return StreamSupport.stream((Spliterator<R>) potentialCollection, false).collect(toList()); } if (potentialCollection instanceof Iterable) { return StreamSupport.stream(((Iterable<R>) potentialCollection).spliterator(), false).collect(toList()); } return Collections.singletonList((R) potentialCollection); } private  CollectionUtils(); static T merge(T collection1, T collection2, Supplier<T> factoryMethod); @SuppressWarnings("unchecked") static Collection<R> asCollection(Object potentialCollection); static C intersect(Collection<? extends T> collection1, Collection<? extends T> collection2, Supplier<C> collectionBuilder);  }