Skip to content

Commit

Permalink
runtime-field: Introduce new CombinedPredicate interface
Browse files Browse the repository at this point in the history
  • Loading branch information
minborg committed Apr 26, 2017
1 parent 8a3033c commit 317c18a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 41 deletions.
Expand Up @@ -16,6 +16,7 @@
*/
package com.speedment.runtime.field.internal.predicate;

import com.speedment.runtime.field.predicate.CombinedPredicate;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
Expand All @@ -27,27 +28,20 @@
* Aggregation of a number of {@link Predicate Predicates} of the same type
* (e.g. AND or OR) that can be applied in combination.
*
* @param <ENTITY> the entity type
*
* @author Per Minborg
* @author Emil Forslund
* @since 2.2.0
* @param <ENTITY> the entity type
*
* @author Per Minborg
* @author Emil Forslund
* @since 2.2.0
*/
public abstract class AbstractCombinedPredicate<ENTITY> extends AbstractPredicate<ENTITY> {

/**
* This enum list all the different types of concrete implementation of the
* abstract CombinedBasePredicate
*/
public enum Type {
AND, OR
}
public abstract class AbstractCombinedPredicate<ENTITY> extends AbstractPredicate<ENTITY>
implements CombinedPredicate<ENTITY> {

private final List<Predicate<? super ENTITY>> predicates;
private final Type type;

private AbstractCombinedPredicate(Type type, Predicate<ENTITY> first, Predicate<? super ENTITY> second) {

this.type = requireNonNull(type);
this.predicates = new ArrayList<>();
add(requireNonNull(first));
Expand Down Expand Up @@ -91,31 +85,17 @@ protected AbstractCombinedPredicate<ENTITY> remove(Predicate<? super ENTITY> pre
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
*/
@Override
public Stream<Predicate<? super ENTITY>> stream() {
return predicates.stream();
}

/**
* Returns the number of predicates that this CombinedBasePredicate holds
*
* @return the number of predicates that this CombinedBasePredicate holds
*/
@Override
public int size() {
return predicates.size();
}

/**
* Returns the {@link Type} of this CombinedBasePredicate
*
* @return the {@link Type} of this CombinedBasePredicate
*/
@Override
public Type getType() {
return type;
}
Expand Down Expand Up @@ -178,10 +158,10 @@ public OrCombinedBasePredicate<ENTITY> or(Predicate<? super ENTITY> other) {

@Override
public String toString() {
return "{type=" +
type.name() +
", predicates=" +
predicates.toString() +
"}";
return "{type="
+ type.name()
+ ", predicates="
+ predicates.toString()
+ "}";
}
}
Expand Up @@ -16,8 +16,7 @@
*/
package com.speedment.runtime.field.internal.predicate;

import com.speedment.runtime.field.internal.predicate.AbstractCombinedPredicate.AndCombinedBasePredicate;
import com.speedment.runtime.field.internal.predicate.AbstractCombinedPredicate.OrCombinedBasePredicate;
import com.speedment.runtime.field.predicate.CombinedPredicate;
import com.speedment.runtime.field.predicate.trait.HasNegated;
import static java.util.Objects.requireNonNull;
import java.util.function.Predicate;
Expand All @@ -42,13 +41,13 @@ abstract class AbstractPredicate<T> implements HasNegated, Predicate<T> {
@Override
public Predicate<T> and(Predicate<? super T> other) {
requireNonNull(other);
return new AndCombinedBasePredicate<>(this, other);
return CombinedPredicate.and(this, other);
}

@Override
public Predicate<T> or(Predicate<? super T> other) {
requireNonNull(other);
return new OrCombinedBasePredicate<>(this, other);
return CombinedPredicate.or(this, other);
}

@Override
Expand Down
@@ -0,0 +1,62 @@
package com.speedment.runtime.field.predicate;

import com.speedment.runtime.field.internal.predicate.AbstractCombinedPredicate;
import java.util.function.Predicate;
import java.util.stream.Stream;

/**
* Aggregation of a number of {@link Predicate Predicates} of the same type
* (e.g. AND or OR) that can be applied in combination.
*
* @param <ENTITY> the entity type
*
* @author Per Minborg
*/
public interface CombinedPredicate<ENTITY> extends Predicate<ENTITY> {

/**
* This enum list all the different types of combinations
*/
public enum Type {
AND, OR
}

/**
* Creates and returns a {link Stream} of all predicates that this
* CombinedPredicate holds.
*
* @return a {link Stream} of all predicates that this CombinedPredicate
* holds
*/
Stream<Predicate<? super ENTITY>> stream();

/**
* Returns the number of predicates that this CombinedBasePredicate holds
*
* @return the number of predicates that this CombinedBasePredicate holds
*/
int size();

/**
* Returns the {@link Type} of this CombinedBasePredicate
*
* @return the {@link Type} of this CombinedBasePredicate
*/
Type getType();

@Override
public CombinedPredicate<ENTITY> and(Predicate<? super ENTITY> other);

@Override
public CombinedPredicate<ENTITY> or(Predicate<? super ENTITY> other);


static <ENTITY> CombinedPredicate<ENTITY> and(Predicate<ENTITY> first, Predicate<? super ENTITY> second) {
return new AbstractCombinedPredicate.AndCombinedBasePredicate<>(first, second);
}

static <ENTITY> CombinedPredicate<ENTITY> or(Predicate<ENTITY> first, Predicate<? super ENTITY> second) {
return new AbstractCombinedPredicate.OrCombinedBasePredicate<>(first, second);
}

}

0 comments on commit 317c18a

Please sign in to comment.