Skip to content

Commit

Permalink
Add notIn and optimized CollectionsUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
minborg committed Dec 18, 2015
1 parent 3ac31d6 commit 73ae7d2
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 31 deletions.
28 changes: 28 additions & 0 deletions src/main/java/com/speedment/field/trait/ComparableFieldTrait.java
Expand Up @@ -192,4 +192,32 @@ default ComparableSpeedmentPredicate<ENTITY, V> between(V start, V end) {
*/
ComparableSpeedmentPredicate<ENTITY, V> in(Set<V> values);

/**
* Returns a {@link java.util.function.Predicate} that will evaluate to
* {@code true}, if and only if this Field is <em>not in</em> the set of
* given values.
* <p>
* N.B. if no values are given, then the returned Predicate will always
* evaluate to {@code true}
*
* @param values the set of values to match towards this Field
* @return a Predicate that will evaluate to {@code true}, if and only if
* this Field is <em>not in</em> the set of given values
*/
@SuppressWarnings("unchecked")
ComparableSpeedmentPredicate<ENTITY, V> notIn(V... values);

/**
* Returns a {@link java.util.function.Predicate} that will evaluate to
* {@code true}, if and only if this Field is <em>not in</em> the given Set.
* <p>
* N.B. if the Set is empty, then the returned Predicate will always
* evaluate to {@code true}
*
* @param values the set of values to match towards this Field
* @return a Predicate that will evaluate to {@code true}, if and only if
* this Field is <em>not in</em> the given Set
*/
ComparableSpeedmentPredicate<ENTITY, V> notIn(Set<V> values);

}
Expand Up @@ -48,9 +48,9 @@ public class ComparableFieldImpl<ENTITY, V extends Comparable<? super V>> implem
private final ComparableFieldTrait<ENTITY, V> comparableField;

public ComparableFieldImpl(
String columnName,
Getter<ENTITY, V> getter,
Setter<ENTITY, V> setter
String columnName,
Getter<ENTITY, V> getter,
Setter<ENTITY, V> setter
) {
field = new FieldTraitImpl(requireNonNull(columnName));
referenceField = new ReferenceFieldTraitImpl<>(field, requireNonNull(getter), requireNonNull(setter));
Expand Down Expand Up @@ -154,4 +154,16 @@ public ComparableSpeedmentPredicate<ENTITY, V> in(Set<V> values) {
return comparableField.in(values);
}

@SafeVarargs
@SuppressWarnings("varargs") // delegator is safe
@Override
public final ComparableSpeedmentPredicate<ENTITY, V> notIn(V... values) {
return comparableField.notIn(values);
}

@Override
public ComparableSpeedmentPredicate<ENTITY, V> notIn(Set<V> values) {
return comparableField.notIn(values);
}

}
Expand Up @@ -31,7 +31,6 @@
import java.util.Comparator;
import static java.util.Objects.requireNonNull;
import java.util.Set;
import java.util.function.Predicate;
import com.speedment.field.trait.ComparableFieldTrait;
import com.speedment.field.trait.FieldTrait;
import com.speedment.field.trait.ReferenceFieldTrait;
Expand Down Expand Up @@ -160,7 +159,19 @@ public final ComparableSpeedmentPredicate<ENTITY, V> in(V... values) {
public ComparableSpeedmentPredicate<ENTITY, V> in(Set<V> values) {
return comparableField.in(values);
}

@SafeVarargs
@SuppressWarnings("varargs") // delegator is safe
@Override
public final ComparableSpeedmentPredicate<ENTITY, V> notIn(V... values) {
return comparableField.notIn(values);
}

@Override
public ComparableSpeedmentPredicate<ENTITY, V> notIn(Set<V> values) {
return comparableField.notIn(values);
}

@Override
public Finder<ENTITY, FK> finder() {
return referenceForeignKeyField.finder();
Expand Down
Expand Up @@ -51,9 +51,9 @@ public class StringFieldImpl<ENTITY> implements StringField<ENTITY> {
private final StringFieldTrait<ENTITY> stringField;

public StringFieldImpl(
String columnName,
Getter<ENTITY, String> getter,
Setter<ENTITY, String> setter
String columnName,
Getter<ENTITY, String> getter,
Setter<ENTITY, String> setter
) {
field = new FieldTraitImpl(requireNonNull(columnName));
referenceField = new ReferenceFieldTraitImpl<>(field, requireNonNull(getter), requireNonNull(setter));
Expand Down Expand Up @@ -156,6 +156,18 @@ public ComparableSpeedmentPredicate<ENTITY, String> in(Set<String> values) {
return comparableField.in(values);
}

@SafeVarargs
@SuppressWarnings("varargs") // delegator is safe
@Override
public final ComparableSpeedmentPredicate<ENTITY, String> notIn(String... values) {
return comparableField.notIn(values);
}

@Override
public ComparableSpeedmentPredicate<ENTITY, String> notIn(Set<String> values) {
return comparableField.notIn(values);
}

@Override
public StringSpeedmentPredicate<ENTITY> equalIgnoreCase(String value) {
return stringField.equalIgnoreCase(value);
Expand Down
Expand Up @@ -56,10 +56,10 @@ public class StringForeignKeyFieldImpl<ENTITY, FK> implements StringForeignKeyFi
private final ReferenceForeignKeyFieldTrait<ENTITY, FK> referenceForeignKeyField;

public StringForeignKeyFieldImpl(
String columnName,
Getter<ENTITY, String> getter,
Setter<ENTITY, String> setter,
Finder<ENTITY, FK> finder
String columnName,
Getter<ENTITY, String> getter,
Setter<ENTITY, String> setter,
Finder<ENTITY, FK> finder
) {
field = new FieldTraitImpl(requireNonNull(columnName));
referenceField = new ReferenceFieldTraitImpl<>(field, requireNonNull(getter), requireNonNull(setter));
Expand Down Expand Up @@ -163,6 +163,18 @@ public ComparableSpeedmentPredicate<ENTITY, String> in(Set<String> values) {
return comparableField.in(values);
}

@SafeVarargs
@SuppressWarnings("varargs") // delegator is safe
@Override
public final ComparableSpeedmentPredicate<ENTITY, String> notIn(String... values) {
return comparableField.in(values);
}

@Override
public ComparableSpeedmentPredicate<ENTITY, String> notIn(Set<String> values) {
return comparableField.in(values);
}

@Override
public StringSpeedmentPredicate<ENTITY> equalIgnoreCase(String value) {
return stringField.equalIgnoreCase(value);
Expand Down
Expand Up @@ -17,15 +17,14 @@
package com.speedment.internal.core.field.predicate.impl.comparable;

import static com.speedment.field.predicate.PredicateType.IN;
import com.speedment.field.methods.Getter;
import com.speedment.field.predicate.ComparableSpeedmentPredicate;
import com.speedment.field.predicate.SpeedmentPredicate;
import com.speedment.field.trait.FieldTrait;
import com.speedment.field.trait.ReferenceFieldTrait;
import com.speedment.internal.core.field.predicate.impl.SpeedmentPredicateImpl;
import java.util.Set;
import com.speedment.internal.core.field.predicate.iface.type.BinarySetOperation;

/**
*
* @author pemi
Expand Down
@@ -0,0 +1,56 @@
/**
*
* Copyright (c) 2006-2015, 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.internal.core.field.predicate.impl.comparable;

import static com.speedment.field.predicate.PredicateType.IN;
import com.speedment.field.predicate.ComparableSpeedmentPredicate;
import static com.speedment.field.predicate.PredicateType.NOT_IN;
import com.speedment.field.predicate.SpeedmentPredicate;
import com.speedment.field.trait.FieldTrait;
import com.speedment.field.trait.ReferenceFieldTrait;
import com.speedment.internal.core.field.predicate.impl.SpeedmentPredicateImpl;
import java.util.Set;
import com.speedment.internal.core.field.predicate.iface.type.BinarySetOperation;

/**
*
* @author pemi
* @param <ENTITY> the entity type
* @param <V> value type
*/
public class NotInPredicate<ENTITY, V extends Comparable<? super V>>
extends SpeedmentPredicateImpl<ENTITY, V>
implements SpeedmentPredicate<ENTITY, V>, BinarySetOperation<V>, ComparableSpeedmentPredicate<ENTITY, V> {

private final Set<V> operand0;

public NotInPredicate(FieldTrait field, ReferenceFieldTrait<ENTITY, V> referenceField, Set<V> values) {
super(NOT_IN, field, referenceField);
this.operand0 = values;
}

@Override
public Set<V> getFirstOperand() {
return operand0;
}

@Override
public boolean testField(V fieldValue) {
return !operand0.contains(fieldValue);
}

}
Expand Up @@ -39,6 +39,8 @@
import com.speedment.internal.core.field.predicate.impl.comparable.AlwaysTrueComparablePredicate;
import com.speedment.internal.core.field.predicate.impl.comparable.IsNotNullComparablePredicate;
import com.speedment.internal.core.field.predicate.impl.comparable.IsNullComparablePredicate;
import com.speedment.internal.core.field.predicate.impl.comparable.NotInPredicate;
import static com.speedment.internal.util.CollectionsUtil.getAnyFrom;

/**
* @param <ENTITY> the entity type
Expand Down Expand Up @@ -205,11 +207,35 @@ public ComparableSpeedmentPredicate<ENTITY, V> in(Set<V> values) {
return newAlwaysFalsePredicate();
}
if (values.size() == 1) {
return new EqualPredicate<>(field, referenceFieldTrait, values.stream().findAny().get());
return new EqualPredicate<>(field, referenceFieldTrait, getAnyFrom(values));
}
return new InPredicate<>(field, referenceFieldTrait, values);
}

@SafeVarargs
@SuppressWarnings("varargs") // Creating a stream from an array is safe
@Override
public final ComparableSpeedmentPredicate<ENTITY, V> notIn(V... values) {
if (values.length == 0) {
return newAlwaysTruePredicate();
}
if (values.length == 1) {
return new NotEqualPredicate<>(field, referenceFieldTrait, values[0]);
}
return notIn(Stream.of(values).collect(toSet()));
}

@Override
public ComparableSpeedmentPredicate<ENTITY, V> notIn(Set<V> values) {
if (values.isEmpty()) {
return newAlwaysTruePredicate();
}
if (values.size() == 1) {
return new NotEqualPredicate<>(field, referenceFieldTrait, getAnyFrom(values));
}
return new NotInPredicate<>(field, referenceFieldTrait, values);
}

private ComparableSpeedmentPredicate<ENTITY, V> newAlwaysFalsePredicate() {
return new AlwaysFalseComparablePredicate<>(field, referenceFieldTrait);
}
Expand Down
Expand Up @@ -33,7 +33,6 @@
import static java.util.Objects.requireNonNull;
import java.util.Set;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

/**
*
Expand Down Expand Up @@ -120,11 +119,11 @@ protected SqlPredicateFragment render(SpeedmentPredicate<?,?> model) {
case LESS_OR_EQUAL:
return lessOrEqual(cn, model);

case BETWEEN:
case BETWEEN:
return between(cn, model);
case NOT_BETWEEN:
return notBetween(cn, model);
case IN:
case IN:
return in(cn, model);
case NOT_IN:
return notIn(cn, model);
Expand All @@ -139,7 +138,7 @@ protected SqlPredicateFragment render(SpeedmentPredicate<?,?> model) {
case NOT_STARTS_WITH:
return notStartsWith(cn, model);

case ENDS_WITH:
case ENDS_WITH:
return endsWith(cn, model);
case NOT_ENDS_WITH:
return notEndsWith(cn, model);
Expand Down Expand Up @@ -237,7 +236,7 @@ protected SqlPredicateFragment notIn(String cn, SpeedmentPredicate<?,?> model) {

protected SqlPredicateFragment inHelper(String cn, SpeedmentPredicate<?,?> model, boolean negated) {
final Set<?> set = getFirstOperandAsRawSet(model);
return of("(" + cn + " IN (" + set.stream().map(o -> "?").collect(joining(",")) + "))", negated).addAll(set.stream().collect(toList()));
return of("(" + cn + " IN (" + set.stream().map($ -> "?").collect(joining(",")) + "))", negated).addAll(set);
}

protected SqlPredicateFragment equalIgnoreCase(String cn, SpeedmentPredicate<?,?> model) {
Expand Down
Expand Up @@ -32,7 +32,7 @@ public MySqlSpeedmentPredicateView(String openingFieldQuote, String closingField

@Override
protected SqlPredicateFragment equalIgnoreCaseHelper(String cn, SpeedmentPredicate<?,?> model, boolean negated) {
return of("(UPPER(" + cn + ") = UPPER(?))", negated).add(getFirstOperandAsRaw(model));
return of("(LOWER(" + cn + ") = LOWER(?))", negated).add(getFirstOperandAsRaw(model));
}

@Override
Expand Down
Expand Up @@ -17,7 +17,8 @@
package com.speedment.internal.core.manager.sql;

import com.speedment.field.predicate.SpeedmentPredicate;

import static com.speedment.internal.core.field.predicate.PredicateUtil.getFirstOperandAsRaw;
import static com.speedment.internal.core.manager.sql.AbstractSpeedmentPredicateView.of;

/**
* Created by fdirlikl on 11/18/2015.
Expand All @@ -28,24 +29,27 @@ public PostgresSpeedmentPredicateView(String openingFieldQuote, String closingFi
super(openingFieldQuote, closingFieldQuote);
}

// Info from:
// http://stackoverflow.com/questions/23320945/postgresql-select-if-string-contains

@Override
protected SqlPredicateFragment equalIgnoreCaseHelper(String cn, SpeedmentPredicate<?,?> model, boolean negated) {
throw new UnsupportedOperationException("To be implemented");
protected SqlPredicateFragment equalIgnoreCaseHelper(String cn, SpeedmentPredicate<?, ?> model, boolean negated) {
return of("(LOWER(" + cn + ") = LOWER(?))", negated).add(getFirstOperandAsRaw(model));
}

@Override
protected SqlPredicateFragment startsWithHelper(String cn, SpeedmentPredicate<?,?> model, boolean negated) {
throw new UnsupportedOperationException("To be implemented");
protected SqlPredicateFragment startsWithHelper(String cn, SpeedmentPredicate<?, ?> model, boolean negated) {
return of("(" + cn + " LIKE ? || '%')", negated).add(getFirstOperandAsRaw(model));
}

@Override
protected SqlPredicateFragment endsWithHelper(String cn, SpeedmentPredicate<?,?> model, boolean negated) {
throw new UnsupportedOperationException("To be implemented");
protected SqlPredicateFragment endsWithHelper(String cn, SpeedmentPredicate<?, ?> model, boolean negated) {
return of("(" + cn + " LIKE '%' || ?)", negated).add(getFirstOperandAsRaw(model));
}

@Override
protected SqlPredicateFragment containsHelper(String cn, SpeedmentPredicate<?,?> model, boolean negated) {
throw new UnsupportedOperationException("To be implemented");
protected SqlPredicateFragment containsHelper(String cn, SpeedmentPredicate<?, ?> model, boolean negated) {
return of("(" + cn + " LIKE '%' || ? || '%')", negated).add(getFirstOperandAsRaw(model));
}

}
Expand Up @@ -50,8 +50,8 @@ public SqlPredicateFragment add(Object o) {
return this;
}

public SqlPredicateFragment addAll(Collection<Object> objects) {
this.objects.addAll(objects);
public SqlPredicateFragment addAll(Collection<?> extra) {
objects.addAll(extra);
return this;
}

Expand Down

0 comments on commit 73ae7d2

Please sign in to comment.