Types { @SuppressWarnings("unchecked") public static <O, T extends O> ImmutableList<T> getInstancesOfTypeUntil(Collection<O> collection, Class<T> clazz, Predicate<O> condition) { Builder<T> resultBuilder = ImmutableList.builder(); for (O o : collection) { if (!condition.test(o)) { break; } if (clazz.isAssignableFrom(o.getClass())) { resultBuilder.add((T) o); } } return resultBuilder.build(); } private  Types(); @SuppressWarnings("unchecked") // This is actually checked static Optional<T> fromType(Object o, Class<T> clazz); @SuppressWarnings("unchecked") // This is actually checked static T fromTypeOrFail(Object o, Class<T> clazz); static Stream<T> streamInstancesOfType(Collection<?> collection, Class<T> clazz); @SuppressWarnings("unchecked") // This is actually checked static Stream<T> streamInstancesOfType(Stream<?> stream, Class<T> clazz); static ImmutableList<T> getInstancesOfType(Collection<?> collection, Class<T> clazz); static ImmutableList<T> getInstancesOfType(Stream<?> stream, Class<T> clazz); static Stream<T> streamInstancesOfTypeOrFail(Collection<?> collection, Class<T> clazz); static Stream<T> streamInstancesOfTypeOrFail(Stream<?> stream, Class<T> clazz); static ImmutableList<T> getInstancesOfTypeOrFail(Collection<?> collection, Class<T> clazz); static ImmutableList<T> getInstancesOfTypeOrFail(Stream<?> stream, Class<T> clazz); @SuppressWarnings("unchecked") // This is actually checked static ImmutableList<T> getInstancesOfTypeUntil(Collection<O> collection, Class<T> clazz, Predicate<O> condition); static ImmutableList<T> getInstancesOfTypeUntil(Stream<O> stream, Class<T> clazz, Predicate<O> condition); static Stream<T> streamInstancesOfTypeUntil(Collection<O> collection, Class<T> clazz, Predicate<O> condition); static Stream<T> streamInstancesOfTypeUntil(Stream<O> stream, Class<T> clazz, Predicate<O> condition);  }
@Test public void testGetInstancesOfTypeUntil_whenGivenAnEmptyCollection_noResultsAreReturned() throws Exception { Assertions.assertEquals(ImmutableList.of(), Types.getInstancesOfTypeUntil(Lists.newArrayList(), Object.class, n -> true)); }
@Test public void testGetInstancesOfTypeUntil_whenPredicateAlwaysFalse_noResultsAreReturned() throws Exception { Assertions.assertEquals(ImmutableList.of(), Types.getInstancesOfTypeUntil(Lists.newArrayList(1, 2, 3), Integer.class, n -> false)); }
@Test public void testGetInstancesOfTypeUntil_whenPredicateAlwaysTrue_allResultsAreReturned() throws Exception { Assertions.assertEquals(ImmutableList.of(1, 2, 3), Types.getInstancesOfTypeUntil(Lists.newArrayList(1, 2, 3), Integer.class, n -> true)); }
@Test public void testGetInstancesOfTypeUntil_whenPredicateAlwaysTrue_resultsAreFilteredByClass() throws Exception { Assertions.assertEquals(ImmutableList.of(1, 3), Types.getInstancesOfTypeUntil(Lists.newArrayList(1, 2.0, 3), Integer.class, n -> true)); }
@Test public void testGetInstancesOfTypeUntil_negativeNumberOccurs() throws Exception { Assertions.assertEquals(ImmutableList.of(1.0, 3.0), Types.getInstancesOfTypeUntil(Lists.newArrayList(1.0, 2, 3.0, -4.0, 5.0), Double.class, number -> number.intValue() >= 0)); }