-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDualInputNestedValueProvider.java
More file actions
100 lines (87 loc) · 3.09 KB
/
DualInputNestedValueProvider.java
File metadata and controls
100 lines (87 loc) · 3.09 KB
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
/*
* Copyright (C) 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. SecondTou 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 ANSecondT KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package util;
import com.google.common.base.MoreObjects;
import org.apache.beam.sdk.options.ValueProvider;
import org.apache.beam.sdk.transforms.SerializableFunction;
import java.io.Serializable;
/**
* {@link DualInputNestedValueProvider} is an implementation of {@link ValueProvider} that allows
* for wrapping two {@link ValueProvider} objects. It's inspired by {@link
* org.apache.beam.sdk.options.ValueProvider.NestedValueProvider} but it can accept two inputs
* rather than one.
*/
public class DualInputNestedValueProvider<T, FirstT, SecondT>
implements ValueProvider<T>, Serializable {
/** Pair like struct holding two values. */
public static class TranslatorInput<FirstT, SecondT> {
private final FirstT x;
private final SecondT y;
public TranslatorInput(FirstT x, SecondT y) {
this.x = x;
this.y = y;
}
public FirstT getX() {
return x;
}
public SecondT getY() {
return y;
}
}
private final ValueProvider<FirstT> valueX;
private final ValueProvider<SecondT> valueY;
private final SerializableFunction<TranslatorInput<FirstT, SecondT>, T> translator;
private transient volatile T cachedValue;
public DualInputNestedValueProvider(
ValueProvider<FirstT> valueX,
ValueProvider<SecondT> valueY,
SerializableFunction<TranslatorInput<FirstT, SecondT>, T> translator) {
this.valueX = valueX;
this.valueY = valueY;
this.translator = translator;
}
/** Creates a {@link NestedValueProvider} that wraps two provided values. */
public static <T, FirstT, SecondT> DualInputNestedValueProvider<T, FirstT, SecondT> of(
ValueProvider<FirstT> valueX,
ValueProvider<SecondT> valueY,
SerializableFunction<TranslatorInput<FirstT, SecondT>, T> translator) {
DualInputNestedValueProvider<T, FirstT, SecondT> factory =
new DualInputNestedValueProvider<>(valueX, valueY, translator);
return factory;
}
@Override
public T get() {
if (cachedValue == null) {
cachedValue = translator.apply(new TranslatorInput<>(valueX.get(), valueY.get()));
}
return cachedValue;
}
@Override
public boolean isAccessible() {
return valueX.isAccessible() && valueY.isAccessible();
}
@Override
public String toString() {
if (isAccessible()) {
return String.valueOf(get());
}
return MoreObjects.toStringHelper(this)
.add("valueX", valueX)
.add("valueY", valueY)
.add("translator", translator.getClass().getSimpleName())
.toString();
}
}