Skip to content

Commit 31ad013

Browse files
committed
Add unchecked operations
1 parent 5332d47 commit 31ad013

File tree

9 files changed

+429
-0
lines changed

9 files changed

+429
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Handy utilities for things I wish were in Java's collections api.
1818
- Treated as additions to `Map<K, Tree<K,V>>`
1919
- Tuples
2020
- Pair
21+
- Lambdas that `throw Throwable`
2122
- Common utilities for `List`, `Map`, and `Set` types with features such as:
2223
- Creating singleton collections
2324
- Creating disjoint & union collection of two input collections
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package software.coley.collections;
2+
3+
import software.coley.collections.func.*;
4+
5+
import javax.annotation.Nonnull;
6+
import javax.annotation.Nullable;
7+
import java.util.function.*;
8+
9+
/**
10+
* Convenience calls for the error-able lambda types.
11+
*
12+
* @author Matt Coley
13+
*/
14+
public class Unchecked {
15+
/**
16+
* @param value
17+
* Value to cast.
18+
* @param <T>
19+
* Target type.
20+
*
21+
* @return Value casted.
22+
*/
23+
@SuppressWarnings("unchecked")
24+
public static <T> T cast(@Nullable Object value) {
25+
return (T) value;
26+
}
27+
28+
/**
29+
* @param runnable
30+
* Runnable.
31+
*/
32+
public static void run(@Nonnull UncheckedRunnable runnable) {
33+
runnable.run();
34+
}
35+
36+
/**
37+
* @param supplier
38+
* Supplier.
39+
* @param <T>
40+
* Supplier type.
41+
*
42+
* @return Supplied value.
43+
*/
44+
public static <T> T get(@Nonnull UncheckedSupplier<T> supplier) {
45+
return supplier.get();
46+
}
47+
48+
/**
49+
* @param supplier
50+
* Supplier.
51+
* @param fallback
52+
* Value to return if supplier fails.
53+
* @param <T>
54+
* Supplier type.
55+
*
56+
* @return Supplied value, or fallback if supplier failed.
57+
*/
58+
public static <T> T getOr(@Nullable UncheckedSupplier<T> supplier, T fallback) {
59+
if (supplier == null)
60+
return fallback;
61+
try {
62+
return supplier.get();
63+
} catch (Throwable t) {
64+
return fallback;
65+
}
66+
}
67+
68+
/**
69+
* @param consumer
70+
* Consumer.
71+
* @param value
72+
* Consumed value.
73+
* @param <T>
74+
* Consumer type.
75+
*/
76+
public static <T> void accept(@Nonnull UncheckedConsumer<T> consumer, T value) {
77+
consumer.accept(value);
78+
}
79+
80+
/**
81+
* @param consumer
82+
* Consumer.
83+
* @param t
84+
* First value.
85+
* @param u
86+
* Second value.
87+
* @param <T>
88+
* First type.
89+
* @param <U>
90+
* Second type.
91+
*/
92+
public static <T, U> void baccept(@Nonnull UncheckedBiConsumer<T, U> consumer, T t, U u) {
93+
consumer.accept(t, u);
94+
}
95+
96+
/**
97+
* @param fn
98+
* Function.
99+
* @param value
100+
* Function value.
101+
* @param <T>
102+
* Input type.
103+
* @param <R>
104+
* Output type.
105+
*/
106+
public static <T, R> R map(@Nonnull UncheckedFunction<T, R> fn, T value) {
107+
return fn.apply(value);
108+
}
109+
110+
/**
111+
* @param fn
112+
* Function.
113+
* @param t
114+
* First function value.
115+
* @param u
116+
* Second function value.
117+
* @param <T>
118+
* First input type.
119+
* @param <U>
120+
* Second input type.
121+
* @param <R>
122+
* Output type.
123+
*/
124+
public static <T, U, R> R bmap(@Nonnull UncheckedBiFunction<T, U, R> fn, T t, U u) {
125+
return fn.apply(t, u);
126+
}
127+
128+
/**
129+
* Helper method to created unchecked runnable.
130+
*
131+
* @param runnable
132+
* Unchecked runnable.
133+
*
134+
* @return Unchecked runnable.
135+
*/
136+
@Nonnull
137+
public static Runnable runnable(@Nonnull UncheckedRunnable runnable) {
138+
return runnable;
139+
}
140+
141+
/**
142+
* Helper method to created unchecked supplier.
143+
*
144+
* @param supplier
145+
* Unchecked supplier.
146+
*
147+
* @return Unchecked supplier.
148+
*/
149+
@Nonnull
150+
public static <T> Supplier<T> supply(@Nonnull UncheckedSupplier<T> supplier) {
151+
return supplier;
152+
}
153+
154+
/**
155+
* Helper method to created unchecked consumer.
156+
*
157+
* @param consumer
158+
* Unchecked consumer.
159+
*
160+
* @return Unchecked consumer.
161+
*/
162+
@Nonnull
163+
public static <T> Consumer<T> consumer(@Nonnull UncheckedConsumer<T> consumer) {
164+
return consumer;
165+
}
166+
167+
/**
168+
* Helper method to created unchecked consumer.
169+
*
170+
* @param consumer
171+
* Unchecked consumer.
172+
*
173+
* @return Unchecked consumer.
174+
*/
175+
@Nonnull
176+
public static <T, U> BiConsumer<T, U> bconsumer(@Nonnull UncheckedBiConsumer<T, U> consumer) {
177+
return consumer;
178+
}
179+
180+
/**
181+
* Helper method to created unchecked function.
182+
*
183+
* @param fn
184+
* Unchecked function.
185+
*
186+
* @return Unchecked function.
187+
*/
188+
@Nonnull
189+
public static <T, R> Function<T, R> function(@Nonnull UncheckedFunction<T, R> fn) {
190+
return fn;
191+
}
192+
193+
/**
194+
* Helper method to created unchecked function.
195+
*
196+
* @param fn
197+
* Unchecked function.
198+
*
199+
* @return Unchecked function.
200+
*/
201+
@Nonnull
202+
public static <T, U, R> BiFunction<T, U, R> bfunction(@Nonnull UncheckedBiFunction<T, U, R> fn) {
203+
return fn;
204+
}
205+
206+
/**
207+
* Propagates throwable.
208+
*
209+
* @param t
210+
* Throwable to propagate.
211+
*/
212+
@SuppressWarnings("unchecked")
213+
public static <X extends Throwable> void propagate(@Nonnull Throwable t) throws X {
214+
throw (X) t;
215+
}
216+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package software.coley.collections.func;
2+
3+
/**
4+
* Generic function for 3 arguments.
5+
*
6+
* @param <A>
7+
* First parameter type.
8+
* @param <B>
9+
* Second parameter type.
10+
* @param <C>
11+
* Third parameter type.
12+
* @param <R>
13+
* Return type.
14+
*
15+
* @author Matt Coley
16+
*/
17+
interface TriFunction<A, B, C, R> {
18+
/**
19+
* @param a
20+
* First arg.
21+
* @param b
22+
* Second arg.
23+
* @param c
24+
* Third arg.
25+
*
26+
* @return Return value.
27+
*/
28+
R apply(A a, B b, C c);
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package software.coley.collections.func;
2+
3+
import software.coley.collections.Unchecked;
4+
5+
import java.util.function.BiConsumer;
6+
7+
/**
8+
* Its {@link BiConsumer} but can throw an exception.
9+
*
10+
* @author xDark
11+
*/
12+
@FunctionalInterface
13+
public interface UncheckedBiConsumer<T, U> extends BiConsumer<T, U> {
14+
@Override
15+
default void accept(T t, U u) {
16+
try {
17+
uncheckedAccept(t, u);
18+
} catch (Throwable th) {
19+
Unchecked.propagate(th);
20+
}
21+
}
22+
23+
void uncheckedAccept(T t, U u) throws Throwable;
24+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package software.coley.collections.func;
2+
3+
import software.coley.collections.Unchecked;
4+
5+
import java.util.function.BiFunction;
6+
7+
/**
8+
* Its {@link BiFunction} but can throw an exception.
9+
*
10+
* @author xDark
11+
*/
12+
@FunctionalInterface
13+
public interface UncheckedBiFunction<T, U, R> extends BiFunction<T, U, R> {
14+
@Override
15+
default R apply(T t, U u) {
16+
try {
17+
return uncheckedApply(t, u);
18+
} catch (Throwable th) {
19+
Unchecked.propagate(th);
20+
return null;
21+
}
22+
}
23+
24+
/**
25+
* @param t
26+
* First input.
27+
* @param u
28+
* Second input.
29+
*
30+
* @return The function result.
31+
*
32+
* @throws Throwable
33+
* Whenever.
34+
*/
35+
R uncheckedApply(T t, U u) throws Throwable;
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package software.coley.collections.func;
2+
3+
import software.coley.collections.Unchecked;
4+
5+
import java.util.function.Consumer;
6+
7+
/**
8+
* Its {@link Consumer} but can throw an exception.
9+
*
10+
* @author Matt Coley
11+
*/
12+
@FunctionalInterface
13+
public interface UncheckedConsumer<T> extends Consumer<T> {
14+
@Override
15+
default void accept(T t) {
16+
try {
17+
uncheckedAccept(t);
18+
} catch (Throwable th) {
19+
Unchecked.propagate(th);
20+
}
21+
}
22+
23+
/**
24+
* @param input
25+
* Consumer input.
26+
*
27+
* @throws Throwable
28+
* Whenever.
29+
*/
30+
void uncheckedAccept(T input) throws Throwable;
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package software.coley.collections.func;
2+
3+
import software.coley.collections.Unchecked;
4+
5+
import java.util.function.Function;
6+
7+
/**
8+
* Its {@link Function} but can throw an exception.
9+
*
10+
* @author xDark
11+
*/
12+
@FunctionalInterface
13+
public interface UncheckedFunction<T, R> extends Function<T, R> {
14+
@Override
15+
default R apply(T t) {
16+
try {
17+
return uncheckedApply(t);
18+
} catch (Throwable th) {
19+
Unchecked.propagate(th);
20+
return null;
21+
}
22+
}
23+
24+
/**
25+
* @param input
26+
* Function input.
27+
*
28+
* @return The function result.
29+
*
30+
* @throws Throwable
31+
* Whenever.
32+
*/
33+
R uncheckedApply(T input) throws Throwable;
34+
}

0 commit comments

Comments
 (0)