Skip to content

Commit

Permalink
Remove Expressions.reduce() method and make and/or stricter about arg…
Browse files Browse the repository at this point in the history
…uments

Add tests for and / or factory methods
  • Loading branch information
asereda-gs committed May 6, 2020
1 parent d688da0 commit fb98841
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

package org.immutables.criteria.expression;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.ImmutableSet;

import javax.annotation.Nullable;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
Expand All @@ -44,38 +45,30 @@ public static Constant constantOfType(@Nullable Object value, Type type) {
return Constant.ofType(value, type);
}

public static Expression and(Expression first, Expression second) {
return and(Arrays.asList(first, second));
public static Call and(Expression first, Expression second) {
return and(ImmutableList.of(first, second));
}

public static Expression and(Iterable<? extends Expression> expressions) {
return reduce(Operators.AND, expressions);
public static Call and(Iterable<? extends Expression> expressions) {
List<Expression> args = ImmutableList.copyOf(expressions);
Preconditions.checkArgument(args.size() >= 2, "expected at least 2 arguments for %s but got %s", Operators.AND, args.size());
return call(Operators.AND, expressions);
}

public static Expression or(Expression first, Expression second) {
return or(Arrays.asList(first ,second));
public static Call or(Expression first, Expression second) {
return or(ImmutableList.of(first, second));
}

public static Expression or(Iterable<? extends Expression> expressions) {
return reduce(Operators.OR, expressions);
public static Call or(Iterable<? extends Expression> expressions) {
List<Expression> args = ImmutableList.copyOf(expressions);
Preconditions.checkArgument(args.size() >= 2, "expected at least 2 arguments for %s but got %s", Operators.OR, args.size());
return call(Operators.OR, expressions);
}

public static Expression aggregation(AggregationOperators operator, Type returnType, Expression expression) {
return ImmutableCall.of(Collections.singletonList(expression), operator, returnType);
}

private static Expression reduce(Operator operator, Iterable<? extends Expression> expressions) {
final int size = Iterables.size(expressions);

if (size == 0) {
throw new IllegalArgumentException("Empty iterator");
} else if (size == 1) {
return expressions.iterator().next();
}

return call(operator, expressions);
}

/**
* Converts a {@link ExpressionVisitor} into a {@link ExpressionBiVisitor} (with ignored payload).
*/
Expand All @@ -100,7 +93,11 @@ public V visit(Path path, @Nullable Void context) {
}

public static Call not(Expression call) {
return Expressions.call(Operators.NOT, call);
return unaryCall(Operators.NOT, call);
}

public static Call unaryCall(Operator operator, Expression arg) {
return call(operator, ImmutableSet.of(arg));
}

public static Call call(final Operator operator, Expression ... operands) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2020 Immutables Authors and Contributors
*
* 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 org.immutables.criteria.expression;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;

import static org.immutables.check.Checkers.check;
import static org.junit.jupiter.api.Assertions.assertThrows;

class ExpressionsTest {

@Test
void and() {
assertThrows(IllegalArgumentException.class, () -> Expressions.and(Collections.emptyList()));
assertThrows(IllegalArgumentException.class, () -> Expressions.and(Collections.singleton(Constant.of(true))));

Call c1 = Expressions.and(Constant.of(true), Constant.of(false));
Call c2 = Expressions.and(Arrays.asList(Constant.of(true), Constant.of(false)));

for (Call c: Arrays.asList(c1, c2)) {
check(c.operator()).is(Operators.AND);
check(c.arguments()).isOf(Constant.of(true), Constant.of(false));
}
}

@Test
void or() {
assertThrows(IllegalArgumentException.class, () -> Expressions.or(Collections.emptyList()));
assertThrows(IllegalArgumentException.class, () -> Expressions.or(Collections.singleton(Constant.of(true))));

Call c1 = Expressions.or(Constant.of(true), Constant.of(false));
Call c2 = Expressions.or(Arrays.asList(Constant.of(true), Constant.of(false)));

for (Call c: Arrays.asList(c1, c2)) {
check(c.operator()).is(Operators.OR);
check(c.arguments()).isOf(Constant.of(true), Constant.of(false));
}
}
}

0 comments on commit fb98841

Please sign in to comment.