io.ultreia.java4all.util.Predicates Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-util Show documentation
Show all versions of java-util Show documentation
Java Util api extends by Ultreia.io (not guava, nor commons)
package io.ultreia.java4all.util;
/*-
* #%L
* Java Util extends by Ultreia.io
* %%
* Copyright (C) 2018 Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
import java.util.function.Predicate;
/**
* Created by tchemit on 20/01/2018.
*
* @author Tony Chemit - [email protected]
*/
public class Predicates {
/**
* Compute a and predicate of given {@code predicates}.
*
* @param predicates predicates to concat
* @param type of object of predicates
* @return the concatenated predicate or {@code d -> true} predicate if collection is empty
*/
public static Predicate predicate(Collection> predicates) {
Iterator> iterator = Objects.requireNonNull(predicates).iterator();
if (!iterator.hasNext()) {
return d -> true;
}
Predicate result = iterator.next();
while (iterator.hasNext()) {
result = result.and(iterator.next());
}
return result;
}
public static Predicate Equals(O value) {
return new Predicates.EqualsPredicate<>(value);
}
public static Predicate NotEquals(O value) {
return Equals(value).negate();
}
public static Predicate In(Collection value) {
return new Predicates.InPredicate<>(value);
}
public static Predicate NotIn(Collection value) {
return In(value).negate();
}
public static Predicate StringContains(String value) {
return new Predicates.StringContainsPredicate(value);
}
public static Predicate NotStringContains(String value) {
return StringContains(value).negate();
}
public static Predicate StringMatches(String value) {
return new Predicates.StringMatchesPredicate(value);
}
public static Predicate NotStringMatches(String value) {
return StringMatches(value).negate();
}
public static > Predicate After(O min) {
return new Predicates.AfterPredicate<>(min, false);
}
public static > Predicate AfterOrEquals(O min) {
return new Predicates.AfterPredicate<>(min, true);
}
public static > Predicate Before(O max) {
return new Predicates.BeforePredicate<>(max, false);
}
public static > Predicate BeforeOrEquals(O max) {
return new Predicates.BeforePredicate<>(max, true);
}
public static > Predicate Between(O min, O max) {
return new Predicates.AfterPredicate<>(min, false).and(new Predicates.BeforePredicate<>(max, false));
}
public static > Predicate BetweenOrEquals(O min, O max) {
return new Predicates.AfterPredicate<>(min, true).and(new Predicates.BeforePredicate<>(max, true));
}
public static final class EqualsPredicate implements Predicate {
private final O value;
public EqualsPredicate(O value) {
this.value = Objects.requireNonNull(value);
}
@Override
public boolean test(O o) {
return Objects.equals(o, value);
}
}
public static final class InPredicate implements Predicate {
private final Collection value;
public InPredicate(Collection value) {
this.value = Objects.requireNonNull(value);
}
@Override
public boolean test(O o) {
return value.contains(o);
}
}
public static final class BeforePredicate> implements Predicate {
private final O value;
private final boolean orEquals;
public BeforePredicate(O value, boolean orEquals) {
this.value = Objects.requireNonNull(value);
this.orEquals = orEquals;
}
@Override
public boolean test(O o) {
int i = o.compareTo(value);
return i < 0 || i == 0 && orEquals;
}
}
public static final class AfterPredicate> implements Predicate {
private final O value;
private final boolean orEquals;
public AfterPredicate(O value, boolean orEquals) {
this.value = Objects.requireNonNull(value);
this.orEquals = orEquals;
}
@Override
public boolean test(O o) {
int i = o.compareTo(value);
return i > 0 || i == 0 && orEquals;
}
}
public static final class StringContainsPredicate implements Predicate {
private final String value;
public StringContainsPredicate(String value) {
this.value = Objects.requireNonNull(value);
}
@Override
public boolean test(String o) {
return o.contains(value);
}
}
public static final class StringMatchesPredicate implements Predicate {
private final String value;
public StringMatchesPredicate(String value) {
this.value = Objects.requireNonNull(value);
}
@Override
public boolean test(String o) {
return o.matches(value);
}
}
}