com.speedment.runtime.field.internal.predicate.AbstractCombinedPredicate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of runtime-deploy Show documentation
Show all versions of runtime-deploy Show documentation
A Speedment bundle that shades all dependencies into one jar. This is
useful when deploying an application on a server.
/**
*
* Copyright (c) 2006-2017, Speedment, Inc. All Rights Reserved.
*
* 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 com.speedment.runtime.field.internal.predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;
import static java.util.Objects.requireNonNull;
/**
* Aggregation of a number of {@link Predicate Predicates} of the same type
* (e.g. AND or OR) that can be applied in combination.
*
* @param the entity type
*
* @author Per Minborg
* @author Emil Forslund
* @since 2.2.0
*/
public abstract class AbstractCombinedPredicate extends AbstractPredicate {
/**
* This enum list all the different types of concrete implementation of the
* abstract CombinedBasePredicate
*/
public enum Type {
AND, OR
}
private final List> predicates;
private final Type type;
private AbstractCombinedPredicate(Type type, Predicate first, Predicate second) {
this.type = requireNonNull(type);
this.predicates = new ArrayList<>();
add(requireNonNull(first));
add(requireNonNull(second));
}
/**
* Adds the provided Predicate to this CombinedBasePredicate.
*
* @param the Type of CombinedBasePredicate (AndCombinedBasePredicate or
* OrCombinedBasePredicate)
* @param predicate to add
* @return a reference to a CombinedPredicate after the method has been
* applied
*/
protected final > R add(Predicate predicate) {
requireNonNull(predicate);
if (getClass().equals(predicate.getClass())) {
@SuppressWarnings("unchecked")
final AbstractCombinedPredicate cbp = getClass().cast(predicate);
cbp.stream().forEachOrdered(predicates::add);
} else {
predicates.add(predicate);
}
@SuppressWarnings("unchecked")
final R self = (R) this;
return self;
}
/**
* Removes the provided Predicate from this CombinedBasePredicate.
*
* @param predicate to remove
* @return a reference to a CombinedPredicate after the method has been
* applied
*/
protected AbstractCombinedPredicate remove(Predicate predicate) {
requireNonNull(predicate);
predicates.remove(predicate);
return this;
}
/**
* Creates and returns a {link Stream} of all predicates that this
* CombinedBasePredicate holds.
*
* @return a {link Stream} of all predicates that this CombinedBasePredicate
* holds
*/
public Stream> stream() {
return predicates.stream();
}
/**
* Returns the number of predicates that this CombinedBasePredicate holds
*
* @return the number of predicates that this CombinedBasePredicate holds
*/
public int size() {
return predicates.size();
}
/**
* Returns the {@link Type} of this CombinedBasePredicate
*
* @return the {@link Type} of this CombinedBasePredicate
*/
public Type getType() {
return type;
}
@Override
public abstract AndCombinedBasePredicate and(Predicate other);
@Override
public abstract OrCombinedBasePredicate or(Predicate other);
public static class AndCombinedBasePredicate extends AbstractCombinedPredicate {
public AndCombinedBasePredicate(Predicate first, Predicate second) {
super(Type.AND, first, second);
}
@Override
protected boolean testWithoutNegation(ENTITY entity) {
requireNonNull(entity);
return stream().allMatch(p -> p.test(entity));
}
@Override
public AndCombinedBasePredicate and(Predicate other) {
requireNonNull(other);
return add(other);
}
@Override
public OrCombinedBasePredicate or(Predicate other) {
requireNonNull(other);
return new OrCombinedBasePredicate<>(this, other);
}
}
public static class OrCombinedBasePredicate extends AbstractCombinedPredicate {
public OrCombinedBasePredicate(Predicate first, Predicate second) {
super(Type.OR, first, second);
}
@Override
protected boolean testWithoutNegation(ENTITY entity) {
requireNonNull(entity);
return stream().anyMatch(p -> p.test(entity));
}
@Override
public AndCombinedBasePredicate and(Predicate other) {
requireNonNull(other);
return new AndCombinedBasePredicate<>(this, other);
}
@Override
public OrCombinedBasePredicate or(Predicate other) {
requireNonNull(other);
return add(other);
}
}
@Override
public String toString() {
return "{type=" +
type.name() +
", predicates=" +
predicates.toString() +
"}";
}
}