This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
/
LambdaGoogleCollections.java
executable file
·109 lines (98 loc) · 3.23 KB
/
LambdaGoogleCollections.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package org.enumerable.lambda.support.googlecollect;
import org.enumerable.lambda.Fn0;
import org.enumerable.lambda.Fn1;
import org.enumerable.lambda.annotation.NewLambda;
import org.enumerable.lambda.exception.LambdaWeavingNotEnabledException;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
/**
* This is class is similar {@link org.enumerable.lambda.Lambda}, but instead of creating
* lambdas inheriting from {@link org.enumerable.lambda.Fn0} it creates lambdas implementing
* the {@link Function}, {@link Predicate} or {@link Supplier} interfaces to be
* used with {@link Collections2}, {@link Iterables}, {@link Iterators},
* {@link Lists} {@link Maps} or {@link Sets}.
*/
public class LambdaGoogleCollections {
@NewLambda
public static <F, T> Function<F, T> function(F from, T to) {
throw new LambdaWeavingNotEnabledException();
}
@NewLambda
public static <T> Predicate<T> predicate(T input, boolean to) {
throw new LambdaWeavingNotEnabledException();
}
@NewLambda
public static <T> Supplier<T> supplier(T value) {
throw new LambdaWeavingNotEnabledException();
}
/**
* Wraps the {@link Predicate} in a {@link Fn1}.
*/
@SuppressWarnings("serial")
public static <T> Fn1<T, Boolean> toFn1(final Predicate<T> predicate) {
return new Fn1<T, Boolean>() {
public Boolean call(T input) {
return predicate.apply(input);
}
};
}
/**
* Wraps the {@link Function} in a {@link Fn1}.
*/
@SuppressWarnings("serial")
public static <F, T> Fn1<F, T> toFn1(final Function<F, T> function) {
return new Fn1<F, T>() {
public T call(F input) {
return function.apply(input);
}
};
}
/**
* Wraps the {@link Supplier} in a {@link Fn0}.
*/
@SuppressWarnings("serial")
public static <T> Fn0<T> toFn0(final Supplier<T> supplier) {
return new Fn0<T>() {
public T call() {
return supplier.get();
}
};
}
/**
* Wraps the {@link Fn1} in a {@link Predicate}.
*/
public static <T> Predicate<T> toPredicate(final Fn1<T, Boolean> precicate) {
return new Predicate<T>() {
public boolean apply(T input) {
return precicate.call(input);
}
};
}
/**
* Wraps the {@link Fn1} in a {@link Function}.
*/
public static <F, T> Function<F, T> toFunction(final Fn1<F, T> function) {
return new Function<F, T>() {
public T apply(F input) {
return function.call(input);
}
};
}
/**
* Wraps the {@link Fn0} in a {@link Supplier}.
*/
public static <T> Supplier<T> toSupplier(final Fn0<T> supplier) {
return new Supplier<T>() {
public T get() {
return supplier.call();
}
};
}
}