Skip to content

Commit

Permalink
Add helper method values() to Constant
Browse files Browse the repository at this point in the history
  • Loading branch information
asereda-gs committed Jun 19, 2019
1 parent 626a8d3 commit 32eb8a5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
Expand Up @@ -16,7 +16,10 @@

package org.immutables.criteria.expression;

import com.google.common.collect.ImmutableList;

import javax.annotation.Nullable;
import java.util.List;

/**
* A constant. {@code true}, {@code 1}, {@code "foo"}, {@code null} etc.
Expand All @@ -29,10 +32,33 @@ private Constant(Object value) {
this.value = value;
}

/**
* Value of current constant (can be {@code null})
*/
public Object value() {
return value;
}

/**
* Converts current value to list (if it is not already). If value
* is iterable returns that list (which is most likely ImmutableList already).
*
* @return singleton list with current value or immutable list of values depending on type
* of current value.
*/
public List<Object> values() {
if (value instanceof Iterable) {
// most likely ImmutableList already (if Iterable)
return ImmutableList.copyOf((Iterable<?>) value);
}

if (value == null) {
throw new NullPointerException("value is null");
}

return ImmutableList.of(value);
}

public static Constant of(Object value) {
return new Constant(value);
}
Expand Down
Expand Up @@ -57,9 +57,7 @@ public QueryBuilders.QueryBuilder visit(Call call) {
if (op == Operators.IN || op == Operators.NOT_IN) {
Preconditions.checkArgument(args.size() == 2, "Size should be 2 for %s but was %s", op, args.size());
final String field = Visitors.toPath(args.get(0)).toStringPath();
@SuppressWarnings("unchecked")
final Iterable<Object> values = (Iterable<Object>) Visitors.toConstant(args.get(1)).value();
Preconditions.checkNotNull(values, "not expected to be null %s", args.get(1));
final List<Object> values = Visitors.toConstant(args.get(1)).values();

QueryBuilders.QueryBuilder builder = QueryBuilders.termsQuery(field, values);

Expand Down
Expand Up @@ -68,10 +68,9 @@ public String visit(Call call) {
if (op == Operators.IN || op == Operators.NOT_IN) {
Preconditions.checkArgument(args.size() == 2, "Size should be 2 for %s but was %s", op, args.size());
final Path field = Visitors.toPath(args.get(0));
@SuppressWarnings("unchecked")
final Iterable<Object> values = (Iterable<Object>) Visitors.toConstant(args.get(1)).value();
final List<Object> values = Visitors.toConstant(args.get(1)).values();

final String valuesAsString = StreamSupport.stream(values.spliterator(), false)
final String valuesAsString = values.stream()
.map(GeodeQueryVisitor::toString).collect(Collectors.joining(", "));

final String query = String.format("%s in SET(%s)", pathFn.apply(field), valuesAsString);
Expand Down
Expand Up @@ -55,8 +55,7 @@ public Bson visit(Call call) {
if (op == Operators.IN || op == Operators.NOT_IN) {
Preconditions.checkArgument(args.size() == 2, "Size should be 2 for %s but was %s", op, args.size());
final String field = Visitors.toPath(args.get(0)).toStringPath();
@SuppressWarnings("unchecked")
final Iterable<Object> values = (Iterable<Object>) Visitors.toConstant(args.get(1)).value();
final List<Object> values = Visitors.toConstant(args.get(1)).values();
Preconditions.checkNotNull(values, "not expected to be null %s", args.get(1));

return op == Operators.IN ? Filters.in(field, values) : Filters.nin(field, values);
Expand Down

0 comments on commit 32eb8a5

Please sign in to comment.