Skip to content
This repository was archived by the owner on Nov 23, 2023. It is now read-only.

Commit bd50f62

Browse files
authored
Merge pull request #111 from alexengrig/refactor-predicatex
Refactor PredicateX Close #109
2 parents 7893896 + b9b1456 commit bd50f62

39 files changed

+1148
-2039
lines changed

.idea/copyright/Alexengrig_Dev_.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/copyright/profiles_settings.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ install:
66
after_success:
77
- mvn clean test jacoco:report coveralls:report -DrepoToken=$COVERALLS_TOKEN
88
- bash <(curl -s https://codecov.io/bash)
9-
- mvn com.gavinmogan:codacy-maven-plugin:coverage -DprojectToken=$CODACY_PROJECT_TOKEN -DapiToken=$CODACY_API_TOKEN

CHANGES.md

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
# *Lambda*X changes
22

3+
## 0.6.0
4+
5+
* [[#109](../../issues/109)] Refactor PredicateX
6+
* Replace final class with interface to PredicateX
7+
* Add PredicateChainX and SafePredicateChainX
8+
* Remove classes:
9+
* ComparableOptionalPredicateB
10+
* ComparableOptionalPredicateI
11+
* ComparablePredicateB
12+
* ComparablePredicateI
13+
* ComparableResultFunction
14+
* OptionalPredicateB
15+
* OptionalPredicateI
16+
* OptionalPredicateResultB
17+
* OptionalPredicateResultI
18+
* PredicateB
19+
* PredicateI
20+
321
## 0.5.0
422

523
* [[#87](../../issues/87)] Create tryXXX methods to ChainX

MIGRATION.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# *Lambda*X migration
2+
3+
## 0.6.0
4+
5+
* [[#109](../../issues/109)] Migration PredicateX
6+
* Replace PredicateX#from with PredicateX#of
7+
* Replace PredicateX#of with PredicateX#chain
8+
* Replace PredicateX#ofNullable with PredicateX#chainSafe

pom.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,11 @@
166166
<plugin>
167167
<groupId>com.gavinmogan</groupId>
168168
<artifactId>codacy-maven-plugin</artifactId>
169-
<version>1.0.3</version>
169+
<version>1.2.0</version>
170170
<configuration>
171+
<apiToken>${env.$CODACY_API_TOKEN}</apiToken>
172+
<projectToken>${env.CODACY_PROJECT_TOKEN}</projectToken>
173+
<commit>${env.TRAVIS_COMMIT}</commit>
171174
<coverageReportFile>${project.reporting.outputDirectory}/jacoco.xml</coverageReportFile>
172175
<failOnMissingReportFile>false</failOnMissingReportFile>
173176
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2019 - 2020 Alexengrig Dev.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.alexengrig.lambdax;
18+
19+
import io.github.alexengrig.lambdax.function.PredicateX;
20+
21+
import java.util.Comparator;
22+
import java.util.Objects;
23+
import java.util.function.Function;
24+
import java.util.function.Predicate;
25+
26+
/**
27+
* A predicate container which may manipulate its value.
28+
*
29+
* @param <T> the type of predicate value
30+
* @author Grig Alex
31+
* @version 0.6.0
32+
* @since 0.6.0
33+
*/
34+
public class PredicateChainX<T, R> {
35+
protected final Function<T, R> function;
36+
37+
protected PredicateChainX(Function<T, R> function) {
38+
this.function = function;
39+
}
40+
41+
// Create
42+
43+
public static <T, R> PredicateChainX<T, R> of(Function<T, R> function) {
44+
return new PredicateChainX<>(Objects.requireNonNull(function, "The function must not be null"));
45+
}
46+
47+
// Flow
48+
49+
public <V> PredicateChainX<T, V> map(Function<? super R, ? extends V> mapper) {
50+
return of(function.andThen(mapper));
51+
}
52+
53+
public <V> SafePredicateChainX<T, V> mapToSafe(Function<? super R, ? extends V> mapper) {
54+
return SafePredicateChainX.of(function.andThen(mapper));
55+
}
56+
57+
// Predicate
58+
59+
public PredicateX<T> isNull() {
60+
return t -> null == function.apply(t);
61+
}
62+
63+
public PredicateX<T> nonNull() {
64+
return t -> null != function.apply(t);
65+
}
66+
67+
public PredicateX<T> check(Predicate<? super R> checker) {
68+
return t -> checker.test(function.apply(t));
69+
}
70+
71+
public PredicateX<T> equalTo(R other) {
72+
return t -> Objects.equals(function.apply(t), other);
73+
}
74+
75+
@SuppressWarnings("unchecked")
76+
public PredicateX<T> less(R other) {
77+
return less(other, (Comparator<R>) Comparator.naturalOrder());
78+
}
79+
80+
public PredicateX<T> less(R other, Comparator<? super R> comparator) {
81+
return t -> 0 > Objects.compare(function.apply(t), other, comparator);
82+
}
83+
84+
@SuppressWarnings("unchecked")
85+
public PredicateX<T> greater(R other) {
86+
return greater(other, (Comparator<R>) Comparator.naturalOrder());
87+
}
88+
89+
public PredicateX<T> greater(R other, Comparator<? super R> comparator) {
90+
return t -> 0 < Objects.compare(function.apply(t), other, comparator);
91+
}
92+
93+
@SuppressWarnings("unchecked")
94+
public PredicateX<T> lessOrEqual(R other) {
95+
return lessOrEqual(other, (Comparator<R>) Comparator.naturalOrder());
96+
}
97+
98+
public PredicateX<T> lessOrEqual(R other, Comparator<? super R> comparator) {
99+
return t -> 0 >= Objects.compare(function.apply(t), other, comparator);
100+
}
101+
102+
@SuppressWarnings("unchecked")
103+
public PredicateX<T> greaterOrEqual(R other) {
104+
return greaterOrEqual(other, (Comparator<R>) Comparator.naturalOrder());
105+
}
106+
107+
public PredicateX<T> greaterOrEqual(R other, Comparator<? super R> comparator) {
108+
return t -> 0 <= Objects.compare(function.apply(t), other, comparator);
109+
}
110+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
* Copyright 2019 - 2020 Alexengrig Dev.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.alexengrig.lambdax;
18+
19+
import io.github.alexengrig.lambdax.function.FunctionX;
20+
import io.github.alexengrig.lambdax.function.PredicateX;
21+
22+
import java.util.Comparator;
23+
import java.util.Objects;
24+
import java.util.function.BooleanSupplier;
25+
import java.util.function.Function;
26+
import java.util.function.Predicate;
27+
28+
/**
29+
* A safe predicate container which may manipulate its value.
30+
*
31+
* @param <T> the type of predicate value
32+
* @author Grig Alex
33+
* @version 0.6.0
34+
* @since 0.6.0
35+
*/
36+
public class SafePredicateChainX<T, R> {
37+
protected final Function<T, R> function;
38+
39+
protected SafePredicateChainX(Function<T, R> function) {
40+
this.function = function;
41+
}
42+
43+
// Create
44+
45+
public static <T, R> SafePredicateChainX<T, R> of(Function<T, R> function) {
46+
return new SafePredicateChainX<>(Objects.requireNonNull(function, "The function must not be null"));
47+
}
48+
49+
// Flow
50+
51+
public <V> SafePredicateChainX<T, V> map(Function<? super R, ? extends V> mapper) {
52+
return of(function.andThen(FunctionX.nullSafe(mapper)));
53+
}
54+
55+
// Predicate
56+
57+
public PredicateX<T> isNull() {
58+
return t -> null == function.apply(t);
59+
}
60+
61+
public PredicateX<T> nonNull() {
62+
return t -> null != function.apply(t);
63+
}
64+
65+
// Result
66+
67+
public Result check(Predicate<? super R> checker) {
68+
return new Result(checker::test);
69+
}
70+
71+
public Result equalTo(R other) {
72+
return new Result(r -> Objects.equals(r, other));
73+
}
74+
75+
@SuppressWarnings("unchecked")
76+
public Result less(R other) {
77+
return less(other, (Comparator<R>) Comparator.naturalOrder());
78+
}
79+
80+
public Result less(R other, Comparator<? super R> comparator) {
81+
return new Result(r -> 0 > Objects.compare(r, other, comparator));
82+
}
83+
84+
@SuppressWarnings("unchecked")
85+
public Result greater(R other) {
86+
return greater(other, (Comparator<R>) Comparator.naturalOrder());
87+
}
88+
89+
public Result greater(R other, Comparator<? super R> comparator) {
90+
return new Result(r -> 0 < Objects.compare(r, other, comparator));
91+
}
92+
93+
@SuppressWarnings("unchecked")
94+
public Result lessOrEqual(R other) {
95+
return lessOrEqual(other, (Comparator<R>) Comparator.naturalOrder());
96+
}
97+
98+
public Result lessOrEqual(R other, Comparator<? super R> comparator) {
99+
return new Result(r -> 0 >= Objects.compare(r, other, comparator));
100+
}
101+
102+
@SuppressWarnings("unchecked")
103+
public Result greaterOrEqual(R other) {
104+
return greaterOrEqual(other, (Comparator<R>) Comparator.naturalOrder());
105+
}
106+
107+
public Result greaterOrEqual(R other, Comparator<? super R> comparator) {
108+
return new Result(r -> 0 <= Objects.compare(r, other, comparator));
109+
}
110+
111+
public class Result {
112+
protected final Predicate<R> predicate;
113+
114+
public Result(Predicate<R> predicate) {
115+
this.predicate = predicate;
116+
}
117+
118+
public Predicate<T> orElse(Predicate<? super T> checker) {
119+
return t -> {
120+
R value = function.apply(t);
121+
if (value != null) {
122+
return predicate.test(value);
123+
}
124+
return checker.test(t);
125+
};
126+
}
127+
128+
public Predicate<T> orElse(boolean check) {
129+
return t -> {
130+
R value = function.apply(t);
131+
if (value != null) {
132+
return predicate.test(value);
133+
}
134+
return check;
135+
};
136+
}
137+
138+
public Predicate<T> orElse(BooleanSupplier producer) {
139+
return t -> {
140+
R value = function.apply(t);
141+
if (value != null) {
142+
return predicate.test(value);
143+
}
144+
return producer.getAsBoolean();
145+
};
146+
}
147+
148+
public Predicate<T> orTruth() {
149+
return t -> {
150+
R value = function.apply(t);
151+
if (value != null) {
152+
return predicate.test(value);
153+
}
154+
return true;
155+
};
156+
}
157+
158+
public Predicate<T> orLie() {
159+
return t -> {
160+
R value = function.apply(t);
161+
if (value != null) {
162+
return predicate.test(value);
163+
}
164+
return false;
165+
};
166+
}
167+
}
168+
}

0 commit comments

Comments
 (0)