@Test public void shouldFindByClass() { List<Number> objects = ImmutableList.<Number>of(10L, 15, 1.4d); Optional<Long> result1 = Collections3.findFirstInstanceOf(objects, Long.class); Optional<Integer> result2 = Collections3.findFirstInstanceOf(objects, Integer.class); Optional<Float> result3 = Collections3.findFirstInstanceOf(objects, Float.class); assertEquals(result1, Optional.of(10L)); assertEquals(result2, Optional.of(15)); assertEquals(result3, Optional.absent()); }
public static <T> Optional<T> findFirstInstanceOf(Collection<? super T> coll, Class<T> cls) { for (Object o : coll) { if (o != null && cls.isAssignableFrom(o.getClass())) { return Optional.of((T) o); } } return Optional.absent(); }
Collections3 { public static <T> Optional<T> findFirstInstanceOf(Collection<? super T> coll, Class<T> cls) { for (Object o : coll) { if (o != null && cls.isAssignableFrom(o.getClass())) { return Optional.of((T) o); } } return Optional.absent(); } }
Collections3 { public static <T> Optional<T> findFirstInstanceOf(Collection<? super T> coll, Class<T> cls) { for (Object o : coll) { if (o != null && cls.isAssignableFrom(o.getClass())) { return Optional.of((T) o); } } return Optional.absent(); } private  Collections3(); }
Collections3 { public static <T> Optional<T> findFirstInstanceOf(Collection<? super T> coll, Class<T> cls) { for (Object o : coll) { if (o != null && cls.isAssignableFrom(o.getClass())) { return Optional.of((T) o); } } return Optional.absent(); } private  Collections3(); static Pair<Collection<E>, Collection<E>> partition(Collection<E> unpartitioned,
                                                                   Predicate<? super E> predicate); static Optional<T> findFirstInstanceOf(Collection<? super T> coll, Class<T> cls); static T maxNullSafe(Collection<? extends T> coll, Comparator<? super T> comp); static T maxNullSafe(Collection<? extends T> coll); }
Collections3 { public static <T> Optional<T> findFirstInstanceOf(Collection<? super T> coll, Class<T> cls) { for (Object o : coll) { if (o != null && cls.isAssignableFrom(o.getClass())) { return Optional.of((T) o); } } return Optional.absent(); } private  Collections3(); static Pair<Collection<E>, Collection<E>> partition(Collection<E> unpartitioned,
                                                                   Predicate<? super E> predicate); static Optional<T> findFirstInstanceOf(Collection<? super T> coll, Class<T> cls); static T maxNullSafe(Collection<? extends T> coll, Comparator<? super T> comp); static T maxNullSafe(Collection<? extends T> coll);  }