/
InjectorWithSideEffects.java
99 lines (84 loc) · 3.09 KB
/
InjectorWithSideEffects.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
/*
* Copyright 2019 Confluent Inc.
*
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package io.confluent.ksql.statement;
import io.confluent.ksql.parser.tree.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* An {@link Injector} that might produce side effects during
* {@link #injectWithSideEffects(ConfiguredStatement) the injection}
* and revert those side effects if necessary.
*/
public interface InjectorWithSideEffects extends Injector {
<T extends Statement> ConfiguredStatementWithSideEffects<T> injectWithSideEffects(
ConfiguredStatement<T> statement
);
<T extends Statement> void revertSideEffects(ConfiguredStatementWithSideEffects<T> statement);
/**
* Container for {@link Statement} and side effects produced during injection.
* @param <T> type of statement
*/
class ConfiguredStatementWithSideEffects<T extends Statement> {
private final ConfiguredStatement<T> statement;
private final List<Object> sideEffects;
public ConfiguredStatementWithSideEffects(
final ConfiguredStatement<T> statement,
final List<Object> sideEffects
) {
this.statement = statement;
this.sideEffects = new ArrayList<>(sideEffects);
}
public ConfiguredStatement<T> getStatement() {
return statement;
}
public List<Object> getSideEffects() {
return Collections.unmodifiableList(sideEffects);
}
public static <T extends Statement> ConfiguredStatementWithSideEffects<T> withNoEffects(
final ConfiguredStatement<T> statement
) {
return new ConfiguredStatementWithSideEffects<>(statement, Collections.emptyList());
}
}
/**
* Wrap an injector into {@link InjectorWithSideEffects}.
*/
static InjectorWithSideEffects wrap(Injector injector) {
if (injector instanceof InjectorWithSideEffects) {
return (InjectorWithSideEffects) injector;
} else {
return new InjectorWithSideEffects() {
@Override
public <T extends Statement> ConfiguredStatementWithSideEffects<T> injectWithSideEffects(
final ConfiguredStatement<T> statement
) {
return ConfiguredStatementWithSideEffects.withNoEffects(inject(statement));
}
@Override
public <T extends Statement> ConfiguredStatement<T> inject(
final ConfiguredStatement<T> statement
) {
return injector.inject(statement);
}
@Override
public <T extends Statement> void revertSideEffects(
final ConfiguredStatementWithSideEffects<T> statement
) {
}
};
}
}
}