Skip to content

Commit a599d6d

Browse files
committed
Class renames
1 parent 4dd3794 commit a599d6d

12 files changed

Lines changed: 246 additions & 246 deletions

runtime/src/main/java/dev/cel/runtime/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ INTERPRABLE_SOURCES = [
7070

7171
# keep sorted
7272
DISPATCHER_SOURCES = [
73+
"DefaultDispatcher.java",
7374
"Dispatcher.java",
74-
"LegacyDispatcher.java",
7575
]
7676

7777
java_library(
@@ -125,7 +125,7 @@ java_library(
125125

126126
java_library(
127127
name = "default_dispatcher",
128-
srcs = ["DefaultDispatcher.java"],
128+
srcs = ["CelValueDispatcher.java"],
129129
tags = [
130130
],
131131
deps = [

runtime/src/main/java/dev/cel/runtime/CelLateFunctionBindings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private CelLateFunctionBindings(ImmutableMap<String, ResolvedOverload> functions
3838
@Override
3939
public Optional<ResolvedOverload> findOverload(
4040
String functionName, List<String> overloadIds, Object[] args) throws CelEvaluationException {
41-
return LegacyDispatcher.findOverload(functionName, overloadIds, functions, args);
41+
return DefaultDispatcher.findOverload(functionName, overloadIds, functions, args);
4242
}
4343

4444
public static CelLateFunctionBindings from(CelFunctionBinding... functions) {

runtime/src/main/java/dev/cel/runtime/CelRuntimeLegacyImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public CelRuntimeLegacyImpl build() {
287287

288288
functionBindingsBuilder.putAll(customFunctionBindings);
289289

290-
LegacyDispatcher dispatcher = LegacyDispatcher.create();
290+
DefaultDispatcher dispatcher = DefaultDispatcher.create();
291291
functionBindingsBuilder
292292
.buildOrThrow()
293293
.forEach(
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package dev.cel.runtime;
2+
3+
import com.google.auto.value.AutoValue;
4+
import com.google.common.collect.ImmutableList;
5+
import com.google.common.collect.ImmutableMap;
6+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
7+
import com.google.errorprone.annotations.CheckReturnValue;
8+
import dev.cel.common.annotations.Internal;
9+
10+
import java.util.Optional;
11+
12+
@Internal
13+
@AutoValue
14+
public abstract class CelValueDispatcher {
15+
16+
abstract ImmutableMap<String, CelValueFunctionBinding> overloads();
17+
18+
public Optional<CelValueFunctionBinding> findOverload(String overloadId) {
19+
return Optional.ofNullable(overloads().get(overloadId));
20+
}
21+
22+
public static Builder newBuilder() {
23+
return new AutoValue_CelValueDispatcher.Builder();
24+
}
25+
26+
@AutoValue.Builder
27+
public abstract static class Builder {
28+
public abstract ImmutableMap.Builder<String, CelValueFunctionBinding> overloadsBuilder();
29+
30+
@CanIgnoreReturnValue
31+
public Builder addOverload(CelValueFunctionBinding functionBinding) {
32+
overloadsBuilder().put(
33+
functionBinding.overloadId(),
34+
functionBinding);
35+
return this;
36+
}
37+
38+
@CanIgnoreReturnValue
39+
public Builder addDynamicDispatchOverload(String functionName, CelValueFunctionOverload definition) {
40+
overloadsBuilder().put(
41+
functionName, CelValueFunctionBinding.from(functionName, ImmutableList.of(), definition)
42+
);
43+
44+
return this;
45+
}
46+
47+
@CheckReturnValue
48+
public abstract CelValueDispatcher build();
49+
}
50+
}
Lines changed: 177 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,177 @@
1-
package dev.cel.runtime;
2-
3-
import com.google.auto.value.AutoValue;
4-
import com.google.common.collect.ImmutableList;
5-
import com.google.common.collect.ImmutableMap;
6-
import com.google.errorprone.annotations.CanIgnoreReturnValue;
7-
import com.google.errorprone.annotations.CheckReturnValue;
8-
import dev.cel.common.annotations.Internal;
9-
10-
import java.util.Optional;
11-
12-
@Internal
13-
@AutoValue
14-
public abstract class DefaultDispatcher {
15-
16-
abstract ImmutableMap<String, CelValueFunctionBinding> overloads();
17-
18-
public Optional<CelValueFunctionBinding> findOverload(String overloadId) {
19-
return Optional.ofNullable(overloads().get(overloadId));
20-
}
21-
22-
public static Builder newBuilder() {
23-
return new AutoValue_DefaultDispatcher.Builder();
24-
}
25-
26-
@AutoValue.Builder
27-
public abstract static class Builder {
28-
public abstract ImmutableMap.Builder<String, CelValueFunctionBinding> overloadsBuilder();
29-
30-
@CanIgnoreReturnValue
31-
public Builder addOverload(CelValueFunctionBinding functionBinding) {
32-
overloadsBuilder().put(
33-
functionBinding.overloadId(),
34-
functionBinding);
35-
return this;
36-
}
37-
38-
@CanIgnoreReturnValue
39-
public Builder addDynamicDispatchOverload(String functionName, CelValueFunctionOverload definition) {
40-
overloadsBuilder().put(
41-
functionName, CelValueFunctionBinding.from(functionName, ImmutableList.of(), definition)
42-
);
43-
44-
return this;
45-
}
46-
47-
@CheckReturnValue
48-
public abstract DefaultDispatcher build();
49-
}
50-
}
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.runtime;
16+
17+
import com.google.auto.value.AutoValue;
18+
import com.google.common.base.Joiner;
19+
import com.google.common.collect.ImmutableList;
20+
import com.google.common.collect.ImmutableMap;
21+
import com.google.errorprone.annotations.Immutable;
22+
import javax.annotation.concurrent.ThreadSafe;
23+
import com.google.errorprone.annotations.concurrent.GuardedBy;
24+
import dev.cel.common.CelErrorCode;
25+
import dev.cel.common.annotations.Internal;
26+
27+
import java.util.ArrayList;
28+
import java.util.HashMap;
29+
import java.util.List;
30+
import java.util.Map;
31+
import java.util.Optional;
32+
33+
/**
34+
* Default implementation of {@link Dispatcher}.
35+
*
36+
* <p>Should be final, do not mock; mocking {@link Dispatcher} instead.
37+
*/
38+
@ThreadSafe
39+
@Internal
40+
public final class DefaultDispatcher implements Dispatcher, Registrar {
41+
public static DefaultDispatcher create() {
42+
return new DefaultDispatcher();
43+
}
44+
45+
@GuardedBy("this")
46+
private final Map<String, ResolvedOverload> overloads = new HashMap<>();
47+
48+
@Override
49+
@SuppressWarnings("unchecked")
50+
public synchronized <T> void add(
51+
String overloadId, Class<T> argType, final Registrar.UnaryFunction<T> function) {
52+
overloads.put(
53+
overloadId,
54+
ResolvedOverloadImpl.of(
55+
overloadId, new Class<?>[] {argType}, args -> function.apply((T) args[0])));
56+
}
57+
58+
@Override
59+
@SuppressWarnings("unchecked")
60+
public synchronized <T1, T2> void add(
61+
String overloadId,
62+
Class<T1> argType1,
63+
Class<T2> argType2,
64+
final Registrar.BinaryFunction<T1, T2> function) {
65+
overloads.put(
66+
overloadId,
67+
ResolvedOverloadImpl.of(
68+
overloadId,
69+
new Class<?>[] {argType1, argType2},
70+
args -> function.apply((T1) args[0], (T2) args[1])));
71+
}
72+
73+
@Override
74+
public synchronized void add(
75+
String overloadId, List<Class<?>> argTypes, Registrar.Function function) {
76+
overloads.put(
77+
overloadId,
78+
ResolvedOverloadImpl.of(overloadId, argTypes.toArray(new Class<?>[0]), function));
79+
}
80+
81+
@Override
82+
public synchronized Optional<ResolvedOverload> findOverload(
83+
String functionName, List<String> overloadIds, Object[] args) throws CelEvaluationException {
84+
return DefaultDispatcher.findOverload(functionName, overloadIds, overloads, args);
85+
}
86+
87+
/** Finds the overload that matches the given function name, overload IDs, and arguments. */
88+
public static Optional<ResolvedOverload> findOverload(
89+
String functionName,
90+
List<String> overloadIds,
91+
Map<String, ? extends ResolvedOverload> overloads,
92+
Object[] args)
93+
throws CelEvaluationException {
94+
int matchingOverloadCount = 0;
95+
ResolvedOverload match = null;
96+
List<String> candidates = null;
97+
for (String overloadId : overloadIds) {
98+
ResolvedOverload overload = overloads.get(overloadId);
99+
// If the overload is null, it means that the function was not registered; however, it is
100+
// possible that the overload refers to a late-bound function.
101+
if (overload != null && overload.canHandle(args)) {
102+
if (++matchingOverloadCount > 1) {
103+
if (candidates == null) {
104+
candidates = new ArrayList<>();
105+
candidates.add(match.getOverloadId());
106+
}
107+
candidates.add(overloadId);
108+
}
109+
match = overload;
110+
}
111+
}
112+
113+
if (matchingOverloadCount > 1) {
114+
throw CelEvaluationExceptionBuilder.newBuilder(
115+
"Ambiguous overloads for function '%s'. Matching candidates: %s",
116+
functionName, Joiner.on(", ").join(candidates))
117+
.setErrorCode(CelErrorCode.AMBIGUOUS_OVERLOAD)
118+
.build();
119+
}
120+
return Optional.ofNullable(match);
121+
}
122+
123+
@Override
124+
public synchronized Dispatcher.ImmutableCopy immutableCopy() {
125+
return new ImmutableCopy(overloads);
126+
}
127+
128+
@Immutable
129+
private static final class ImmutableCopy implements Dispatcher.ImmutableCopy {
130+
private final ImmutableMap<String, ResolvedOverload> overloads;
131+
132+
private ImmutableCopy(Map<String, ResolvedOverload> overloads) {
133+
this.overloads = ImmutableMap.copyOf(overloads);
134+
}
135+
136+
@Override
137+
public Optional<ResolvedOverload> findOverload(
138+
String functionName, List<String> overloadIds, Object[] args)
139+
throws CelEvaluationException {
140+
return DefaultDispatcher.findOverload(functionName, overloadIds, overloads, args);
141+
}
142+
143+
@Override
144+
public Dispatcher.ImmutableCopy immutableCopy() {
145+
return this;
146+
}
147+
}
148+
149+
private DefaultDispatcher() {}
150+
151+
@AutoValue
152+
@Immutable
153+
abstract static class ResolvedOverloadImpl implements ResolvedOverload {
154+
/** The overload id of the function. */
155+
@Override
156+
public abstract String getOverloadId();
157+
158+
/** The types of the function parameters. */
159+
@Override
160+
public abstract ImmutableList<Class<?>> getParameterTypes();
161+
162+
/** The function definition. */
163+
@Override
164+
public abstract FunctionOverload getDefinition();
165+
166+
static ResolvedOverload of(
167+
String overloadId, Class<?>[] parameterTypes, FunctionOverload definition) {
168+
return of(overloadId, ImmutableList.copyOf(parameterTypes), definition);
169+
}
170+
171+
static ResolvedOverload of(
172+
String overloadId, ImmutableList<Class<?>> parameterTypes, FunctionOverload definition) {
173+
return new AutoValue_DefaultDispatcher_ResolvedOverloadImpl(
174+
overloadId, parameterTypes, definition);
175+
}
176+
}
177+
}

runtime/src/main/java/dev/cel/runtime/Dispatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public interface Dispatcher extends FunctionResolver {
3535
ImmutableCopy immutableCopy();
3636

3737
/**
38-
* An {@link Immutable} copy of a {@link Dispatcher}. Currently {@link LegacyDispatcher}
38+
* An {@link Immutable} copy of a {@link Dispatcher}. Currently {@link DefaultDispatcher}
3939
* implementation implements both {@link Dispatcher} and {@link Registrar} and cannot be annotated
4040
* as {@link Immutable}.
4141
*

0 commit comments

Comments
 (0)