Skip to content

Commit

Permalink
runtime-field: Remove isNegated from all predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyknic committed Jun 7, 2017
1 parent 009c020 commit a80af53
Show file tree
Hide file tree
Showing 112 changed files with 2,881 additions and 770 deletions.
Expand Up @@ -37,7 +37,6 @@
* @see ComparableField * @see ComparableField
* @see HasStringOperators * @see HasStringOperators
*/ */

public interface StringField<ENTITY, D> extends public interface StringField<ENTITY, D> extends
ComparableField<ENTITY, D, String>, ComparableField<ENTITY, D, String>,
HasStringOperators<ENTITY, D> { HasStringOperators<ENTITY, D> {
Expand Down
Expand Up @@ -40,7 +40,6 @@
* @see StringField * @see StringField
* @see HasFinder * @see HasFinder
*/ */

public interface StringForeignKeyField<ENTITY, D, FK_ENTITY> public interface StringForeignKeyField<ENTITY, D, FK_ENTITY>
extends StringField<ENTITY, D>, extends StringField<ENTITY, D>,
HasNullableFinder<ENTITY, FK_ENTITY> { HasNullableFinder<ENTITY, FK_ENTITY> {
Expand Down
Expand Up @@ -174,7 +174,7 @@ public Predicate<ENTITY> notIn(Collection<String> values) {


@Override @Override
public Predicate<ENTITY> equalIgnoreCase(String value) { public Predicate<ENTITY> equalIgnoreCase(String value) {
return new StringEqualIgnoreCasePredicate<>(this, value); return new StringEqualIgnoreCasePredicate<>(this, value.toLowerCase());
} }


@Override @Override
Expand All @@ -199,16 +199,16 @@ public Predicate<ENTITY> isEmpty() {


@Override @Override
public Predicate<ENTITY> startsWithIgnoreCase(String value) { public Predicate<ENTITY> startsWithIgnoreCase(String value) {
return new StringStartsWithIgnoreCasePredicate<>(this, value); return new StringStartsWithIgnoreCasePredicate<>(this, value.toLowerCase());
} }


@Override @Override
public Predicate<ENTITY> endsWithIgnoreCase(String value) { public Predicate<ENTITY> endsWithIgnoreCase(String value) {
return new StringEndsWithIgnoreCasePredicate<>(this, value); return new StringEndsWithIgnoreCasePredicate<>(this, value.toLowerCase());
} }


@Override @Override
public Predicate<ENTITY> containsIgnoreCase(String value) { public Predicate<ENTITY> containsIgnoreCase(String value) {
return new StringContainsIgnoreCasePredicate<>(this, value); return new StringContainsIgnoreCasePredicate<>(this, value.toLowerCase());
} }
} }
Expand Up @@ -217,7 +217,7 @@ public Predicate<ENTITY> notIn(Collection<String> values) {


@Override @Override
public Predicate<ENTITY> equalIgnoreCase(String value) { public Predicate<ENTITY> equalIgnoreCase(String value) {
return new StringEqualIgnoreCasePredicate<>(this, value); return new StringEqualIgnoreCasePredicate<>(this, value.toLowerCase());
} }


@Override @Override
Expand All @@ -242,16 +242,16 @@ public Predicate<ENTITY> isEmpty() {


@Override @Override
public Predicate<ENTITY> startsWithIgnoreCase(String value) { public Predicate<ENTITY> startsWithIgnoreCase(String value) {
return new StringStartsWithIgnoreCasePredicate<>(this, value); return new StringStartsWithIgnoreCasePredicate<>(this, value.toLowerCase());
} }


@Override @Override
public Predicate<ENTITY> endsWithIgnoreCase(String value) { public Predicate<ENTITY> endsWithIgnoreCase(String value) {
return new StringEndsWithIgnoreCasePredicate<>(this, value); return new StringEndsWithIgnoreCasePredicate<>(this, value.toLowerCase());
} }


@Override @Override
public Predicate<ENTITY> containsIgnoreCase(String value) { public Predicate<ENTITY> containsIgnoreCase(String value) {
return new StringContainsIgnoreCasePredicate<>(this, value); return new StringContainsIgnoreCasePredicate<>(this, value.toLowerCase());
} }
} }
Expand Up @@ -22,41 +22,45 @@
import com.speedment.runtime.field.internal.util.Cast; import com.speedment.runtime.field.internal.util.Cast;
import com.speedment.runtime.field.predicate.FieldPredicate; import com.speedment.runtime.field.predicate.FieldPredicate;
import com.speedment.runtime.field.predicate.PredicateType; import com.speedment.runtime.field.predicate.PredicateType;
import static java.util.Objects.requireNonNull;
import java.util.function.Predicate; import java.util.function.Predicate;


import static java.util.Objects.requireNonNull;

/** /**
* A predicate that contains meta-data about the {@link Field} that was used to * A predicate that contains meta-data about the {@link Field} that was used to
* construct it. * construct it.
* *
* @param <ENTITY> the entity type that is being tested * @param <ENTITY> the entity type that is being tested
* @param <V> the wrapper type * @param <FIELD> the field in the entity that is operated on
* @param <FIELD> the field in the entity that is operated on
* *
* @author Emil Forslund * @author Emil Forslund
* @since 3.0.0 * @since 3.0.0
*/ */
public abstract class AbstractFieldPredicate<ENTITY, V, FIELD extends Field<ENTITY>> public abstract class AbstractFieldPredicate<ENTITY, FIELD extends Field<ENTITY>>
extends AbstractPredicate<ENTITY> implements FieldPredicate<ENTITY> { extends AbstractPredicate<ENTITY>
implements FieldPredicate<ENTITY> {


private final PredicateType predicateType; private final PredicateType predicateType;
private final FIELD field; private final FIELD field;
private final Predicate<ENTITY> tester; private final Predicate<ENTITY> tester;


protected AbstractFieldPredicate( protected AbstractFieldPredicate(
final PredicateType predicateType, final PredicateType predicateType,
final FIELD field, final FIELD field,
final Predicate<ENTITY> tester, final Predicate<ENTITY> tester) {
final boolean negated
) {
super(negated);
this.predicateType = requireNonNull(predicateType); this.predicateType = requireNonNull(predicateType);
this.field = requireNonNull(field); this.field = requireNonNull(field);
this.tester = requireNonNull(tester); this.tester = requireNonNull(tester);
}

protected final Predicate<ENTITY> getTester() {
return tester;
} }


@Override @Override
protected boolean testWithoutNegation(ENTITY instance) { public boolean test(ENTITY instance) {
return tester.test(instance); return tester.test(instance);
} }


Expand All @@ -65,11 +69,6 @@ public final PredicateType getPredicateType() {
return predicateType; return predicateType;
} }


@Override
public final PredicateType getEffectivePredicateType() {
return isNegated() ? predicateType.negate() : predicateType;
}

@Override @Override
public final FIELD getField() { public final FIELD getField() {
return field; return field;
Expand All @@ -84,6 +83,7 @@ public FieldPredicate<ENTITY> negate() {
public String toString() { public String toString() {
final ColumnIdentifier<ENTITY> cId = field.identifier(); final ColumnIdentifier<ENTITY> cId = field.identifier();
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();

sb.append(getClass().getSimpleName()) sb.append(getClass().getSimpleName())
.append(" {") .append(" {")
.append("field: ") .append("field: ")
Expand All @@ -93,15 +93,13 @@ public String toString() {
.append(cId.getColumnName()) .append(cId.getColumnName())
.append(", type: '").append(predicateType).append("'"); .append(", type: '").append(predicateType).append("'");


Cast.cast(this, Tuple.class) Cast.cast(this, Tuple.class).ifPresent(tuple -> {
.ifPresent(tuple -> { for (int i = 0; i < tuple.degree(); i++) {
for (int i = 0; i < tuple.length(); i++) { sb.append(", operand ").append(i)
sb.append(", operand ").append(i).append(": ").append(tuple.get(i)); .append(": ").append(tuple.get(i));
} }
}); });


sb.append(", negated: ").append(isNegated()) return sb.append("}").toString();
.append("}");
return sb.toString();
} }
} }
Expand Up @@ -17,10 +17,11 @@
package com.speedment.runtime.field.internal.predicate; package com.speedment.runtime.field.internal.predicate;


import com.speedment.runtime.field.predicate.CombinedPredicate; 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; import java.util.function.Predicate;


import static java.util.Objects.requireNonNull;

/** /**
* This class represents a Predicate that is used to build up higher orders * This class represents a Predicate that is used to build up higher orders
* of predicates. * of predicates.
Expand All @@ -30,13 +31,9 @@
* @author Per Minborg * @author Per Minborg
* @since 2.1.0 * @since 2.1.0
*/ */
abstract class AbstractPredicate<T> implements HasNegated, Predicate<T> { abstract class AbstractPredicate<T> implements Predicate<T> {

private final boolean negated;


AbstractPredicate(boolean negated) { AbstractPredicate() {}
this.negated = negated;
}


@Override @Override
public Predicate<T> and(Predicate<? super T> other) { public Predicate<T> and(Predicate<? super T> other) {
Expand All @@ -49,24 +46,4 @@ public Predicate<T> or(Predicate<? super T> other) {
requireNonNull(other); requireNonNull(other);
return CombinedPredicate.or(this, other); return CombinedPredicate.or(this, other);
} }

@Override
public boolean isNegated() {
return negated;
}

@Override
public final boolean test(T instance) {
return testWithoutNegation(instance) ^ negated;
}

/**
* Tests this predicate without applying negation. If the predicate
* has been negated, the result from this method will be negated
* afterwards.
*
* @param instance the instance to test
* @return the result of the test (without negation)
*/
protected abstract boolean testWithoutNegation(T instance);
} }
Expand Up @@ -23,16 +23,15 @@
* A special implementation of {@code Predicate} that always returns false. * A special implementation of {@code Predicate} that always returns false.
* *
* @param <ENTITY> the entity type * @param <ENTITY> the entity type
* @param <V> the value type
* @param <FIELD> the field type * @param <FIELD> the field type
* *
* @author Per Minborg * @author Per Minborg
* @since 2.2.0 * @since 2.2.0
*/ */
public final class AlwaysFalsePredicate<ENTITY, V, FIELD extends Field<ENTITY>> public final class AlwaysFalsePredicate<ENTITY, FIELD extends Field<ENTITY>>
extends AbstractFieldPredicate<ENTITY, V, FIELD> { extends AbstractFieldPredicate<ENTITY, FIELD> {


public AlwaysFalsePredicate(FIELD field) { public AlwaysFalsePredicate(FIELD field) {
super(PredicateType.ALWAYS_FALSE, field, entity -> false, false); super(PredicateType.ALWAYS_FALSE, field, entity -> false);
} }
} }
Expand Up @@ -23,16 +23,15 @@
* A special implementation of {@code Predicate} that always returns true. * A special implementation of {@code Predicate} that always returns true.
* *
* @param <ENTITY> the entity type * @param <ENTITY> the entity type
* @param <V> the value type
* @param <FIELD> the field type * @param <FIELD> the field type
* *
* @author Per Minborg * @author Per Minborg
* @since 2.2.0 * @since 2.2.0
*/ */
public final class AlwaysTruePredicate<ENTITY, V, FIELD extends Field<ENTITY>> public final class AlwaysTruePredicate<ENTITY, FIELD extends Field<ENTITY>>
extends AbstractFieldPredicate<ENTITY, V, FIELD> { extends AbstractFieldPredicate<ENTITY, FIELD> {


public AlwaysTruePredicate(FIELD field) { public AlwaysTruePredicate(FIELD field) {
super(PredicateType.ALWAYS_TRUE, field, entity -> true, false); super(PredicateType.ALWAYS_TRUE, field, entity -> true);
} }
} }
Expand Up @@ -35,18 +35,17 @@
* @since 3.0.0 * @since 3.0.0
*/ */
@GeneratedCode(value = "Speedment") @GeneratedCode(value = "Speedment")
public final class ByteBetweenPredicate<ENTITY, D> extends AbstractFieldPredicate<ENTITY, Byte, HasByteValue<ENTITY, D>> implements BetweenPredicate, Tuple2<Byte, Byte> { public final class ByteBetweenPredicate<ENTITY, D> extends AbstractFieldPredicate<ENTITY, HasByteValue<ENTITY, D>> implements BetweenPredicate, Tuple2<Byte, Byte> {


private final byte start; private final byte start;
private final byte end; private final byte end;
private final Inclusion inclusion; private final Inclusion inclusion;


ByteBetweenPredicate( public ByteBetweenPredicate(
HasByteValue<ENTITY, D> field, HasByteValue<ENTITY, D> field,
byte start, byte start,
byte end, byte end,
Inclusion inclusion, Inclusion inclusion) {
boolean negated) {
super(PredicateType.BETWEEN, field, entity -> { super(PredicateType.BETWEEN, field, entity -> {
final byte fieldValue = field.getAsByte(entity); final byte fieldValue = field.getAsByte(entity);


Expand All @@ -65,21 +64,13 @@ public final class ByteBetweenPredicate<ENTITY, D> extends AbstractFieldPredicat


default : throw new IllegalStateException("Inclusion unknown: " + inclusion); default : throw new IllegalStateException("Inclusion unknown: " + inclusion);
} }
}, negated); });


this.start = start; this.start = start;
this.end = end; this.end = end;
this.inclusion = requireNonNull(inclusion); this.inclusion = requireNonNull(inclusion);
} }


public ByteBetweenPredicate(
HasByteValue<ENTITY, D> field,
byte start,
byte end,
Inclusion inclusion) {
this(field, start, end, inclusion, false);
}

@Override @Override
public Byte get0() { public Byte get0() {
return start; return start;
Expand All @@ -96,7 +87,7 @@ public Inclusion getInclusion() {
} }


@Override @Override
public ByteBetweenPredicate<ENTITY, D> negate() { public ByteNotBetweenPredicate<ENTITY, D> negate() {
return new ByteBetweenPredicate<>(getField(), start, end, inclusion, !isNegated()); return new ByteNotBetweenPredicate<>(getField(), start, end, inclusion);
} }
} }
Expand Up @@ -30,16 +30,12 @@
* @since 3.0.0 * @since 3.0.0
*/ */
@GeneratedCode(value = "Speedment") @GeneratedCode(value = "Speedment")
public final class ByteEqualPredicate<ENTITY, D> extends AbstractFieldPredicate<ENTITY, Byte, HasByteValue<ENTITY, D>> implements Tuple1<Byte> { public final class ByteEqualPredicate<ENTITY, D> extends AbstractFieldPredicate<ENTITY, HasByteValue<ENTITY, D>> implements Tuple1<Byte> {


private final byte value; private final byte value;


public ByteEqualPredicate(HasByteValue<ENTITY, D> field, byte value) { public ByteEqualPredicate(HasByteValue<ENTITY, D> field, byte value) {
this(field, value, false); super(PredicateType.EQUAL, field, entity -> field.getAsByte(entity) == value);
}

ByteEqualPredicate(HasByteValue<ENTITY, D> field, byte value, boolean negated) {
super(PredicateType.EQUAL, field, entity -> field.getAsByte(entity) == value, negated);
this.value = value; this.value = value;
} }


Expand All @@ -49,7 +45,7 @@ public Byte get0() {
} }


@Override @Override
public ByteEqualPredicate<ENTITY, D> negate() { public ByteNotEqualPredicate<ENTITY, D> negate() {
return new ByteEqualPredicate<>(getField(), value, !isNegated()); return new ByteNotEqualPredicate<>(getField(), value);
} }
} }
Expand Up @@ -30,16 +30,12 @@
* @since 3.0.0 * @since 3.0.0
*/ */
@GeneratedCode(value = "Speedment") @GeneratedCode(value = "Speedment")
public final class ByteGreaterOrEqualPredicate<ENTITY, D> extends AbstractFieldPredicate<ENTITY, Byte, HasByteValue<ENTITY, D>> implements Tuple1<Byte> { public final class ByteGreaterOrEqualPredicate<ENTITY, D> extends AbstractFieldPredicate<ENTITY, HasByteValue<ENTITY, D>> implements Tuple1<Byte> {


private final byte value; private final byte value;


public ByteGreaterOrEqualPredicate(HasByteValue<ENTITY, D> field, byte value) { public ByteGreaterOrEqualPredicate(HasByteValue<ENTITY, D> field, byte value) {
this(field, value, false); super(PredicateType.GREATER_OR_EQUAL, field, entity -> field.getAsByte(entity) >= value);
}

ByteGreaterOrEqualPredicate(HasByteValue<ENTITY, D> field, byte value, boolean negated) {
super(PredicateType.GREATER_OR_EQUAL, field, entity -> field.getAsByte(entity) >= value, negated);
this.value = value; this.value = value;
} }


Expand All @@ -49,7 +45,7 @@ public Byte get0() {
} }


@Override @Override
public ByteGreaterOrEqualPredicate<ENTITY, D> negate() { public ByteLessThanPredicate<ENTITY, D> negate() {
return new ByteGreaterOrEqualPredicate<>(getField(), value, !isNegated()); return new ByteLessThanPredicate<>(getField(), value);
} }
} }

0 comments on commit a80af53

Please sign in to comment.