
org.sugarcubes.cloner.Predicates Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sugar-cubes-cloner Show documentation
Show all versions of sugar-cubes-cloner Show documentation
Deep cloning of any objects
/*
* Copyright 2017-2023 the original author or authors.
*
* Licensed 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 org.sugarcubes.cloner;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Predicate;
/**
* Predicates factory. To be used with {@link PredicatePolicy}.
*
* @see PredicatePolicy
*
* @author Maxim Butov
*/
@SuppressWarnings("checkstyle:MultipleStringLiterals")
public class Predicates {
/**
* Negates predicate.
*
* @param object type
* @param predicate predicate
* @return negated predicate
*/
public static Predicate not(Predicate predicate) {
return predicate.negate();
}
/**
* Predicate which test an object is equal to reference value.
*
* @param object type
* @param referenceValue reference value
* @return predicate
*/
public static Predicate equal(T referenceValue) {
return Predicate.isEqual(referenceValue);
}
/**
* Predicate which test an object is same (==) as reference value.
*
* @param object type
* @param referenceValue reference value
* @return predicate
*/
public static Predicate same(T referenceValue) {
return t -> t == referenceValue;
}
/**
* Predicate which test an element is annotated with provided annotation.
*
* @param element type
* @param annotation type
* @param annotation annotation type
* @return predicate
*/
public static Predicate annotatedWith(Class annotation) {
Checks.argNotNull(annotation, "Annotation");
return element -> element.getDeclaredAnnotation(annotation) != null;
}
/**
* Predicate which test an element is annotated with any of provided annotations.
*
* @param element type
* @param annotation type
* @param annotation annotation type
* @param annotations annotation types
* @return predicate
*/
@SafeVarargs
public static Predicate annotatedWithAny(Class annotation,
Class... annotations) {
Predicate predicate = Predicates.annotatedWith(annotation);
if (annotations.length != 0) {
for (Class a : annotations) {
Checks.argNotNull(a, "Annotation");
}
return predicate.or(element ->
Arrays.stream(annotations).map(element::getDeclaredAnnotation).anyMatch(Objects::nonNull));
}
else {
return predicate;
}
}
/**
* Predicate which test a class is subclass (including self) of the type.
*
* @param type type
* @return predicate
*/
public static Predicate> subclass(Class> type) {
return type::isAssignableFrom;
}
/**
* Utility class.
*/
private Predicates() {
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy