-
Notifications
You must be signed in to change notification settings - Fork 273
/
Expressions.java
118 lines (96 loc) · 3.96 KB
/
Expressions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright 2019 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 com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import javax.annotation.Nullable;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
/**
* A set of predefined utilities and factories for expressions like {@link Constant} or {@link Call}
*/
public final class Expressions {
private Expressions() {}
public static Constant constant(Object value) {
if (value == null) {
throw new NullPointerException(String.format("value argument is null. Use method %s.constantOfType(Object, Type)", Expressions.class.getSimpleName()));
}
return constantOfType(value, value.getClass());
}
public static Constant constantOfType(@Nullable Object value, Type type) {
Objects.requireNonNull(type, "type");
return Constant.ofType(value, type);
}
public static Call and(Expression first, Expression second) {
return and(ImmutableList.of(first, second));
}
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 Call or(Expression first, Expression second) {
return or(ImmutableList.of(first, second));
}
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);
}
/**
* Converts a {@link ExpressionVisitor} into a {@link ExpressionBiVisitor} (with ignored payload).
*/
static <V> ExpressionBiVisitor<V, Void> toBiVisitor(ExpressionVisitor<V> visitor) {
return new ExpressionBiVisitor<V, Void>() {
@Override
public V visit(Call call, @Nullable Void context) {
return visitor.visit(call);
}
@Override
public V visit(Constant constant, @Nullable Void context) {
return visitor.visit(constant);
}
@Override
public V visit(Path path, @Nullable Void context) {
return visitor.visit(path);
}
};
}
public static Call not(Expression call) {
return unaryCall(Operators.NOT, call);
}
public static Call unaryCall(Operator operator, Expression arg) {
Objects.requireNonNull(arg, "arg");
return call(operator, ImmutableSet.of(arg));
}
public static Call binaryCall(final Operator operator, Expression left, Expression right) {
Objects.requireNonNull(left, "left");
Objects.requireNonNull(right, "right");
return call(operator, ImmutableList.of(left, right));
}
public static Call call(final Operator operator, Expression ... operands) {
return call(operator, ImmutableList.copyOf(operands));
}
public static Call call(final Operator operator, final Iterable<? extends Expression> operands) {
return Call.of(operator, operands);
}
}