Skip to content
This repository was archived by the owner on Jan 8, 2018. It is now read-only.

Commit 23d85e3

Browse files
committed
more java.util.function utilities
1 parent 3e58f3f commit 23d85e3

File tree

3 files changed

+180
-7
lines changed

3 files changed

+180
-7
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2016-2017 52°North Initiative for Geospatial Open Source
3+
* Software GmbH
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.n52.janmayen.function;
18+
19+
import java.util.Objects;
20+
import java.util.function.BiConsumer;
21+
import java.util.function.Consumer;
22+
23+
import javax.annotation.Nonnull;
24+
25+
/**
26+
* Utility functions for {@link Consumer}.
27+
*
28+
* @author Christian Autermann
29+
*/
30+
public class Consumers {
31+
32+
private Consumers() {
33+
}
34+
35+
/**
36+
* Curries the second parameter of the {@code BiConsumer} and creates a {@code Consumer}.
37+
*
38+
* @param <T> the first parameter type
39+
* @param <U> the second parameter type
40+
* @param consumer the consumer
41+
* @param t the curried parameter
42+
*
43+
* @return the curried consumer
44+
*/
45+
public static <T, U> Consumer<U> curryFirst(@Nonnull BiConsumer<T, U> consumer, T t) {
46+
Objects.requireNonNull(consumer);
47+
return (u) -> consumer.accept(t, u);
48+
}
49+
50+
/**
51+
* Curries the second parameter of the {@code BiConsumer} and creates a {@code Consumer}.
52+
*
53+
* @param <T> the first parameter type
54+
* @param <U> the second parameter type
55+
* @param consumer the consumer
56+
* @param u the curried parameter
57+
*
58+
* @return the curried consumer
59+
*/
60+
public static <T, U> Consumer<T> currySecond(@Nonnull BiConsumer<T, U> consumer, U u) {
61+
Objects.requireNonNull(consumer);
62+
return (t) -> consumer.accept(t, u);
63+
}
64+
65+
/**
66+
* Reverses the parameter order of the BiConsumer
67+
*
68+
* @param <A> the first parameter type
69+
* @param <B> the second parameter type
70+
* @param consumer the consumer
71+
*
72+
* @return the consumer with switched parameters
73+
*/
74+
public static <A, B> BiConsumer<B, A> reverse(@Nonnull BiConsumer<A, B> consumer) {
75+
Objects.requireNonNull(consumer);
76+
return (a, b) -> consumer.accept(b, a);
77+
}
78+
79+
}

src/main/java/org/n52/janmayen/function/Functions.java

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ private Functions() {
5252
* @param clazz The class of which the input has to be an instance of.
5353
*
5454
* @return The predicate.
55+
*
56+
* @deprecated use {@link Predicates#instanceOf(java.lang.Class)}
5557
*/
58+
@Deprecated
5659
public static <T, U extends T> Predicate<T> instanceOf(@Nonnull Class<? extends U> clazz) {
57-
Objects.requireNonNull(clazz);
58-
return x -> clazz.isAssignableFrom(x.getClass());
60+
return Predicates.instanceOf(clazz);
5961
}
6062

6163
/**
@@ -102,7 +104,7 @@ public static <T, U extends T> Function<T, U> cast(@Nonnull Class<? extends U> c
102104
* @return An {@link Optional} containing the input
103105
*/
104106
public static <T, U extends T> Function<T, Optional<U>> castIfInstanceOf(@Nonnull Class<? extends U> clazz) {
105-
Predicate<Object> filter = instanceOf(clazz);
107+
Predicate<Object> filter = Predicates.instanceOf(clazz);
106108
Function<Object, U> mapper = cast(clazz);
107109
return t -> Optional.ofNullable(t).filter(filter).map(mapper);
108110
}
@@ -134,24 +136,67 @@ public static <T, U> Function<T, U> constant(@Nullable U u) {
134136
return t -> u;
135137
}
136138

139+
/**
140+
* Curries the parameter of the {@code Function} and creates a {@code Consumer}.
141+
*
142+
* @param <T> the parameter type
143+
* @param <R> the return type
144+
* @param function the function
145+
* @param t the curried parameter
146+
*
147+
* @return the curried function
148+
*/
137149
public static <T, R> Supplier<R> curry(@Nonnull Function<? super T, ? extends R> function, T t) {
138150
Objects.requireNonNull(function);
139151
return () -> function.apply(t);
140152
}
141153

154+
/**
155+
* Curries the first parameter of the {@code BiFunction} and creates a {@code Function}.
156+
*
157+
* @param <T1> the first parameter type
158+
* @param <T2> the second parameter type
159+
* @param <R> the return type
160+
* @param bifunction the function
161+
* @param t1 the curried parameter
162+
*
163+
* @return the curried function
164+
*/
142165
public static <T1, T2, R> Function<T2, R> curryFirst(@Nonnull BiFunction<T1, T2, R> bifunction, T1 t1) {
143166
Objects.requireNonNull(bifunction);
144167
return t2 -> bifunction.apply(t1, t2);
145168
}
146169

170+
/**
171+
* Curries the second parameter of the {@code BiFunction} and creates a {@code Function}.
172+
*
173+
* @param <T1> the first parameter type
174+
* @param <T2> the second parameter type
175+
* @param <R> the return type
176+
* @param bifunction the function
177+
* @param t2 the curried parameter
178+
*
179+
* @return the curried function
180+
*/
147181
public static <T1, T2, R> Function<T1, R> currySecond(@Nonnull BiFunction<T1, T2, R> bifunction, T2 t2) {
148182
Objects.requireNonNull(bifunction);
149183
return t1 -> bifunction.apply(t1, t2);
150184
}
151185

186+
/**
187+
* Reverses the parameter order of the BiConsumer
188+
*
189+
* @param <A> the first parameter type
190+
* @param <B> the second parameter type
191+
* @param consumer the consumer
192+
*
193+
* @return the consumer with switched parameters
194+
*
195+
* @deprecated use {@link Consumers#reverse(java.util.function.BiConsumer)}
196+
*/
197+
@Deprecated
152198
public static <A, B> BiConsumer<B, A> reverse(@Nonnull BiConsumer<A, B> consumer) {
153-
Objects.requireNonNull(consumer);
154-
return (a, b) -> consumer.accept(b, a);
199+
return Consumers.reverse(consumer);
155200
}
156201

157202
public static <T> BinaryOperator<T> mergeLeft(@Nonnull BiConsumer<T, T> merger) {
@@ -203,7 +248,7 @@ public static <K, V> Function<Map<K, V>, Stream<K>> keyStreamWhereValues(Predica
203248
*
204249
* @return A function that applies the consumer and returns it's input.
205250
*/
206-
public static <T> Function<T, T> mutate(Consumer<? super T> action) {
251+
public static <T> Function<T, T> mutate(@Nonnull Consumer<? super T> action) {
207252
Objects.requireNonNull(action);
208253
return (T t) -> {
209254
action.accept(t);
@@ -222,7 +267,8 @@ public static <T> Function<T, T> mutate(Consumer<? super T> action) {
222267
*
223268
* @return the wrapped function.
224269
*/
225-
public static <S, T, X extends Exception> Function<S, T> errorWrapper(ThrowingFunction<S, T, X> fun) {
270+
public static <S, T, X extends Exception> Function<S, T> errorWrapper(@Nonnull ThrowingFunction<S, T, X> fun) {
271+
Objects.requireNonNull(fun);
226272
return s -> {
227273
try {
228274
return fun.apply(s);

src/main/java/org/n52/janmayen/function/Predicates.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
*/
1717
package org.n52.janmayen.function;
1818

19+
import java.util.Objects;
20+
import java.util.function.BiPredicate;
1921
import java.util.function.Predicate;
2022

23+
import javax.annotation.Nonnull;
24+
2125
/**
2226
* Utility functions for {@link Predicate}.
2327
*
@@ -85,4 +89,48 @@ public static <T> Predicate<T> alwaysTrue() {
8589
public static <T> Predicate<T> constant(boolean value) {
8690
return t -> value;
8791
}
92+
93+
/**
94+
* Creates a {@link Predicate} that checks if it's input is an instance of {@code clazz}.
95+
*
96+
* @param <T> The input type.
97+
* @param <U> The type of which the input has to be an instance of.
98+
* @param clazz The class of which the input has to be an instance of.
99+
*
100+
* @return The predicate.
101+
*/
102+
public static <T, U extends T> Predicate<T> instanceOf(@Nonnull Class<? extends U> clazz) {
103+
Objects.requireNonNull(clazz);
104+
return x -> clazz.isAssignableFrom(x.getClass());
105+
}
106+
107+
/**
108+
* Curries the first parameter of the {@code BiPredicate} and creates a {@code Predicate}.
109+
*
110+
* @param <T> the first parameter type
111+
* @param <U> the second parameter type
112+
* @param predicate the predicate
113+
* @param t the curried parameter
114+
*
115+
* @return the curried predicate
116+
*/
117+
public static <T, U> Predicate<U> curryFirst(@Nonnull BiPredicate<T, U> predicate, T t) {
118+
Objects.requireNonNull(predicate);
119+
return (u) -> predicate.test(t, u);
120+
}
121+
122+
/**
123+
* Curries the second parameter of the {@code BiPredicate} and creates a {@code Predicate}.
124+
*
125+
* @param <T> the first parameter type
126+
* @param <U> the second parameter type
127+
* @param predicate the predicate
128+
* @param u the curried parameter
129+
*
130+
* @return the curried predicate
131+
*/
132+
public static <T, U> Predicate<T> currySecond(@Nonnull BiPredicate<T, U> predicate, U u) {
133+
Objects.requireNonNull(predicate);
134+
return (t) -> predicate.test(t, u);
135+
}
88136
}

0 commit comments

Comments
 (0)