Skip to content

Commit 021e4de

Browse files
committed
feat: add func cast method
1 parent 6407868 commit 021e4de

15 files changed

+93
-13
lines changed

src/main/java/com/github/lokic/javaplus/func/entry/EntryConsumer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ public interface EntryConsumer<K, V> extends Consumer<Map.Entry<K, V>>, Consumer
1313
default void accept(Map.Entry<K, V> entry) {
1414
accept(entry.getKey(), entry.getValue());
1515
}
16+
17+
18+
static <K, V> EntryConsumer<K, V> cast(Consumer2<K, V> consumer2) {
19+
return consumer2::accept;
20+
}
1621
}

src/main/java/com/github/lokic/javaplus/func/entry/EntryFunction.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@ public interface EntryFunction<K, V, R> extends Function<Map.Entry<K, V>, R>, Fu
1313
default R apply(Map.Entry<K, V> entry) {
1414
return apply(entry.getKey(), entry.getValue());
1515
}
16+
17+
18+
static <K, V, R> EntryFunction<K, V, R> cast(Function2<K, V, R> function2) {
19+
return function2::apply;
20+
}
21+
1622
}
1723

src/main/java/com/github/lokic/javaplus/func/entry/package-info.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,13 @@
1111
* .stream()
1212
* .map((EntryFunction<Integer, String, String>) (index, desc) -> a + b))
1313
* }</pre>
14+
*
15+
* 或者
16+
* <pre>{@code
17+
* Map<Integer, String> map = ...
18+
* map.entrySet()
19+
* .stream()
20+
* .map(EntryFunction.cast((index, desc) -> a + b))
21+
* }</pre>
1422
*/
1523
package com.github.lokic.javaplus.func.entry;
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package com.github.lokic.javaplus.func.sneakythrows;
22

3+
import com.github.lokic.javaplus.func.throwable.ThrowConsumer1;
34
import lombok.SneakyThrows;
45

56
import java.util.function.Consumer;
67

78
@FunctionalInterface
8-
public interface SneakyThrowConsumer1<T> extends Consumer<T> {
9+
public interface SneakyThrowConsumer1<T> extends Consumer<T>, ThrowConsumer1<T> {
910

10-
void throwableAccept(T t) throws Throwable;
1111

1212
@SneakyThrows
1313
@Override
1414
default void accept(T t) {
1515
throwableAccept(t);
1616
}
17+
18+
19+
static <T> SneakyThrowConsumer1<T> cast(ThrowConsumer1<T> throwConsumer1) {
20+
return throwConsumer1::throwableAccept;
21+
}
1722
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package com.github.lokic.javaplus.func.sneakythrows;
22

3+
import com.github.lokic.javaplus.func.throwable.ThrowFunction1;
34
import lombok.SneakyThrows;
45

56
import java.util.function.Function;
67

78
@FunctionalInterface
8-
public interface SneakyThrowFunction1<T, R> extends Function<T, R> {
9-
10-
R throwableApply(T t) throws Throwable;
9+
public interface SneakyThrowFunction1<T, R> extends Function<T, R>, ThrowFunction1<T, R> {
1110

1211
@SneakyThrows
1312
@Override
1413
default R apply(T t) {
1514
return throwableApply(t);
1615
}
1716

17+
static <T, R> SneakyThrowFunction1<T, R> cast(ThrowFunction1<T, R> throwFunction1) {
18+
return throwFunction1::throwableApply;
19+
}
1820
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package com.github.lokic.javaplus.func.sneakythrows;
22

3+
import com.github.lokic.javaplus.func.throwable.ThrowFunction2;
34
import com.github.lokic.javaplus.func.tuple.TupleFunction2;
45
import lombok.SneakyThrows;
56

67
@FunctionalInterface
7-
public interface SneakyThrowFunction2<T1, T2, R> extends TupleFunction2<T1, T2, R> {
8+
public interface SneakyThrowFunction2<T1, T2, R> extends TupleFunction2<T1, T2, R>, ThrowFunction2<T1, T2, R> {
89

9-
R throwableApply(T1 t1, T2 t2) throws Throwable;
1010

1111
@SneakyThrows
1212
@Override
1313
default R apply(T1 t1, T2 t2) {
1414
return throwableApply(t1, t2);
1515
}
16+
17+
static <T1, T2, R> SneakyThrowFunction2<T1, T2, R> cast(ThrowFunction2<T1, T2, R> throwFunction2) {
18+
return throwFunction2::throwableApply;
19+
}
1620
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package com.github.lokic.javaplus.func.sneakythrows;
22

3+
import com.github.lokic.javaplus.func.throwable.ThrowFunction3;
34
import com.github.lokic.javaplus.func.tuple.TupleFunction3;
45
import lombok.SneakyThrows;
56

67
@FunctionalInterface
7-
public interface SneakyThrowFunction3<T1, T2, T3, R> extends TupleFunction3<T1, T2, T3, R> {
8+
public interface SneakyThrowFunction3<T1, T2, T3, R> extends TupleFunction3<T1, T2, T3, R>, ThrowFunction3<T1, T2, T3, R> {
89

9-
R throwableApply(T1 t1, T2 t2, T3 t3) throws Throwable;
1010

1111
@SneakyThrows
1212
@Override
1313
default R apply(T1 t1, T2 t2, T3 t3) {
1414
return throwableApply(t1, t2, t3);
1515
}
16+
17+
18+
static <T1, T2, T3, R> SneakyThrowFunction3<T1, T2, T3, R> cast(ThrowFunction3<T1, T2, T3, R> throwFunction3) {
19+
return throwFunction3::throwableApply;
20+
}
1621
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package com.github.lokic.javaplus.func.sneakythrows;
22

3+
import com.github.lokic.javaplus.func.throwable.ThrowRunnable;
34
import lombok.SneakyThrows;
45

56
@FunctionalInterface
6-
public interface SneakyThrowRunnable extends Runnable {
7+
public interface SneakyThrowRunnable extends Runnable, ThrowRunnable {
78

8-
void throwableRun() throws Throwable;
99

1010
@SneakyThrows
1111
@Override
1212
default void run() {
1313
throwableRun();
1414
}
15+
16+
static SneakyThrowRunnable cast(ThrowRunnable throwRunnable) {
17+
return throwRunnable::throwableRun;
18+
}
1519
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package com.github.lokic.javaplus.func.sneakythrows;
22

3+
import com.github.lokic.javaplus.func.throwable.ThrowSupplier;
34
import lombok.SneakyThrows;
45

56
import java.util.function.Supplier;
67

78
@FunctionalInterface
8-
public interface SneakyThrowSupplier<T> extends Supplier<T> {
9+
public interface SneakyThrowSupplier<T> extends Supplier<T>, ThrowSupplier<T> {
910

10-
T throwableGet() throws Throwable;
1111

1212
@SneakyThrows
1313
@Override
1414
default T get() {
1515
return throwableGet();
1616
}
1717

18+
static <T> SneakyThrowSupplier<T> cast(ThrowSupplier<T> throwSupplier) {
19+
return throwSupplier::throwableGet;
20+
}
1821
}

src/main/java/com/github/lokic/javaplus/func/sneakythrows/package-info.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,16 @@
1919
*
2020
* }
2121
* }</pre>
22+
*
23+
* 或者
24+
*
25+
* <pre>{@code
26+
* Optional.of(0)
27+
* .map( SneakyThrowFunction1.cast(this::throwableMethod ));
28+
*
29+
* int throwableMethod(int i) throws Exception {
30+
*
31+
* }
32+
* }</pre>
2233
*/
2334
package com.github.lokic.javaplus.func.sneakythrows;

0 commit comments

Comments
 (0)