io.microsphere.lang.function.Predicates Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.microsphere.lang.function;
import java.util.function.Predicate;
/**
* The utilities class for Java {@link Predicate}
*
* @since 1.0.0
*/
public interface Predicates {
Predicate[] EMPTY_PREDICATE_ARRAY = new Predicate[0];
static Predicate[] emptyArray() {
return (Predicate[]) EMPTY_PREDICATE_ARRAY;
}
/**
* {@link Predicate} always return true
*
* @param the type to test
* @return true
*/
static Predicate alwaysTrue() {
return e -> true;
}
/**
* {@link Predicate} always return false
*
* @param the type to test
* @return false
*/
static Predicate alwaysFalse() {
return e -> false;
}
/**
* a composed predicate that represents a short-circuiting logical AND of {@link Predicate predicates}
*
* @param predicates {@link Predicate predicates}
* @param the type to test
* @return non-null
*/
static Predicate super T> and(Predicate super T>... predicates) {
int length = predicates == null ? 0 : predicates.length;
if (length == 0) {
return alwaysTrue();
} else if (length == 1) {
return predicates[0];
} else {
Predicate andPredicate = alwaysTrue();
for (Predicate super T> p : predicates) {
andPredicate = andPredicate.and(p);
}
return andPredicate;
}
}
/**
* a composed predicate that represents a short-circuiting logical OR of {@link Predicate predicates}
*
* @param predicates {@link Predicate predicates}
* @param the detected type
* @return non-null
*/
static Predicate super T> or(Predicate super T>... predicates) {
int length = predicates == null ? 0 : predicates.length;
if (length == 0) {
return alwaysTrue();
} else if (length == 1) {
return predicates[0];
} else {
Predicate orPredicate = alwaysFalse();
for (Predicate super T> p : predicates) {
orPredicate = orPredicate.or(p);
}
return orPredicate;
}
}
}