Skip to content

Commit

Permalink
Merge pull request #31 from ypresto/fix-generics
Browse files Browse the repository at this point in the history
Fix missing generic type specifier and unchecked casts
  • Loading branch information
aNNiMON committed Mar 5, 2016
2 parents d571183 + 73f3ef3 commit e821bc5
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 24 deletions.
29 changes: 17 additions & 12 deletions src/main/java/com/annimon/stream/Collectors.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ public void accept(Set<T> t, T u) {
final Function<? super T, ? extends K> keyMapper,
final Function<? super T, ? extends V> valueMapper,
final Supplier<M> mapFactory) {
return new CollectorsImpl<T, Map<K, V>, M>(
return new CollectorsImpl<T, M, M>(

(Supplier<Map<K, V>>) mapFactory,
mapFactory,

new BiConsumer<Map<K, V>, T>() {
new BiConsumer<M, T>() {
@Override
public void accept(Map<K, V> map, T t) {
public void accept(M map, T t) {
final K key = keyMapper.apply(t);
final V value = valueMapper.apply(t);
final V oldValue = map.get(key);
Expand Down Expand Up @@ -471,26 +471,31 @@ public static <T, A, IR, OR> Collector<T, A, OR> collectingAndThen(
final Function<? super T, ? extends K> classifier,
final Supplier<M> mapFactory,
final Collector<? super T, A, D> downstream) {

final Function<A, A> doownstreamFinisher = (Function<A, A>) downstream.finisher();

@SuppressWarnings("unchecked")
final Function<A, A> downstreamFinisher = (Function<A, A>) downstream.finisher();
Function<Map<K, A>, M> finisher = null;
if (doownstreamFinisher != null) {
if (downstreamFinisher != null) {
finisher = new Function<Map<K, A>, M>() {
@Override
public M apply(Map<K, A> map) {
// Update values of a map by a finisher function
for (Map.Entry<K, A> entry : map.entrySet()) {
A value = entry.getValue();
value = doownstreamFinisher.apply(value);
value = downstreamFinisher.apply(value);
entry.setValue(value);
}
return (M) map;
@SuppressWarnings("unchecked")
M castedMap = (M) map;
return castedMap;
}
};
}


@SuppressWarnings("unchecked")
Supplier<Map<K, A>> castedMapFactory = (Supplier<Map<K, A>>) mapFactory;
return new CollectorsImpl<T, Map<K, A>, M>(
(Supplier<Map<K, A>>) mapFactory,
castedMapFactory,

new BiConsumer<Map<K, A>, T>() {
@Override
Expand Down Expand Up @@ -523,7 +528,7 @@ public Map<K, V> get() {
}

@SuppressWarnings("unchecked")
private static <A, R> Function<A, R> castIdentity() {
static <A, R> Function<A, R> castIdentity() {
return new Function<A, R>() {

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/annimon/stream/Exceptional.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public Exceptional<T> ifException(Consumer<Throwable> consumer) {
* @param consumer a consumer function
* @return an {@code Exceptional}
*/
public <E extends Throwable> Exceptional<T> ifExceptionIs(Class<E> throwableClass, Consumer<E> consumer) {
@SuppressWarnings("unchecked")
public <E extends Throwable> Exceptional<T> ifExceptionIs(Class<E> throwableClass, Consumer<? super E> consumer) {
if ( (throwable != null) &&
(throwableClass.isAssignableFrom(throwable.getClass())) ) {
consumer.accept((E) throwable);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/annimon/stream/Optional.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static <T> Optional<T> of(T value) {
* @see #of(java.lang.Object)
*/
public static <T> Optional<T> ofNullable(T value) {
return value == null ? (Optional<T>) EMPTY : of(value);
return value == null ? Optional.<T>empty() : of(value);
}

/**
Expand All @@ -46,6 +46,7 @@ public static <T> Optional<T> ofNullable(T value) {
* @param <T> the type of value
* @return an {@code Optional}
*/
@SuppressWarnings("unchecked")
public static <T> Optional<T> empty() {
return (Optional<T>) EMPTY;
}
Expand Down Expand Up @@ -100,7 +101,7 @@ public void ifPresent(Consumer<? super T> consumer) {
*/
public Optional<T> filter(Predicate<? super T> predicate) {
if (!isPresent()) return this;
return predicate.test(value) ? this : (Optional<T>) EMPTY;
return predicate.test(value) ? this : Optional.<T>empty();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/annimon/stream/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ public Stream<T> sorted(Comparator<? super T> comparator) {
* @param f the transformation function
* @return the new stream
*/
public <R extends Comparable> Stream<T> sortBy(final Function<? super T, ? extends R> f) {
public <R extends Comparable<? super R>> Stream<T> sortBy(final Function<? super T, ? extends R> f) {
return sorted(new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
Expand Down Expand Up @@ -816,7 +816,7 @@ public Optional<T> reduce(BiFunction<T, T, T> accumulator) {
result = accumulator.apply(result, value);
}
}
return foundAny ? Optional.of(result) : (Optional<T>) Optional.empty();
return foundAny ? Optional.of(result) : Optional.<T>empty();
}

/**
Expand Down Expand Up @@ -900,7 +900,7 @@ public <R, A> R collect(Collector<? super T, A, R> collector) {
}
if (collector.finisher() != null)
return collector.finisher().apply(container);
return (R) container;
return Collectors.<A, R>castIdentity().apply(container);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/annimon/stream/function/Predicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public boolean test(T value) {
* @return a composed {@code Predicate}
* @throws NullPointerException if {@code p1} is null
*/
public static <T> Predicate<? super T> negate(final Predicate<? super T> p1) {
public static <T> Predicate<T> negate(final Predicate<? super T> p1) {
return new Predicate<T>() {
@Override
public boolean test(T value) {
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/annimon/stream/ExceptionalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ public void accept(Exception value) {
data[EXCEPTION] = true;
}
})
.ifExceptionIs(FileNotFoundException.class, new Consumer<FileNotFoundException>() {
.ifExceptionIs(FileNotFoundException.class, new Consumer<Throwable>() {
@Override
public void accept(FileNotFoundException value) {
public void accept(Throwable value) {
data[FILE_NOT_FOUND] = true;
}
});

assertTrue(data[INTERRUPTED]);
assertTrue(data[EXCEPTION]);
assertFalse(data[FILE_NOT_FOUND]);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/annimon/stream/function/PredicateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ public void testPrivateConstructor() throws Exception {
TestUtils.testPrivateConstructor(Predicate.Util.class);
}

private static final Predicate lessThan100 = new Predicate<Integer>() {
private static final Predicate<Integer> lessThan100 = new Predicate<Integer>() {
@Override
public boolean test(Integer value) {
return value < 100;
}
};

private static final Predicate isEven = Functions.remainder(2);
private static final Predicate<Integer> isEven = Functions.remainder(2);

}

0 comments on commit e821bc5

Please sign in to comment.