1717 */
1818package org .apache .beam .sdk .transforms ;
1919
20- import static org .apache .beam .vendor . guava . v32_1_2_jre . com . google . common . base . Preconditions .checkNotNull ;
20+ import static org .apache .beam .sdk . util . Preconditions .checkArgumentNotNull ;
2121
22- import javax .annotation .CheckForNull ;
2322import org .apache .beam .sdk .coders .CannotProvideCoderException ;
2423import org .apache .beam .sdk .coders .Coder ;
2524import org .apache .beam .sdk .coders .CoderRegistry ;
5655 * @param <V> the type of the elements in the input {@code PCollection} and the values in the output
5756 * {@code PCollection}
5857 */
59- @ SuppressWarnings ({
60- "nullness" // TODO(https://github.com/apache/beam/issues/20497)
61- })
6258public class WithKeys <K , V > extends PTransform <PCollection <V >, PCollection <KV <K , V >>> {
6359 /**
6460 * Returns a {@code PTransform} that takes a {@code PCollection<V>} and returns a {@code
@@ -69,7 +65,7 @@ public class WithKeys<K, V> extends PTransform<PCollection<V>, PCollection<KV<K,
6965 * result {@link PTransform}.
7066 */
7167 public static <K , V > WithKeys <K , V > of (SerializableFunction <V , K > fn ) {
72- checkNotNull (
68+ checkArgumentNotNull (
7369 fn , "WithKeys constructed with null function. Did you mean WithKeys.of((Void) null)?" );
7470 return new WithKeys <>(fn , null );
7571 }
@@ -79,20 +75,22 @@ public static <K, V> WithKeys<K, V> of(SerializableFunction<V, K> fn) {
7975 * PCollection<KV<K, V>>}, where each of the values in the input {@code PCollection} has been
8076 * paired with the given key.
8177 */
82- @ SuppressWarnings ("unchecked" )
83- public static <K , V > WithKeys <K , V > of (final @ Nullable K key ) {
84- return new WithKeys <>(
85- value -> key ,
86- (TypeDescriptor <K >)
87- (key == null ? TypeDescriptors .voids () : TypeDescriptor .of (key .getClass ())));
78+ public static <K , V > WithKeys <K , V > of (final K key ) {
79+ TypeDescriptor <K > keyType ;
80+ if (key == null ) {
81+ keyType = (TypeDescriptor <K >) TypeDescriptors .voids ();
82+ } else {
83+ keyType = (TypeDescriptor <K >) TypeDescriptor .of (key .getClass ());
84+ }
85+ return new WithKeys <>(value -> key , keyType ); // createWithConstantKey(key, keyType);
8886 }
8987
9088 /////////////////////////////////////////////////////////////////////////////
9189
9290 private SerializableFunction <V , K > fn ;
93- @ CheckForNull private transient TypeDescriptor <K > keyType ;
91+ private transient @ Nullable TypeDescriptor <K > keyType ;
9492
95- private WithKeys (SerializableFunction <V , K > fn , TypeDescriptor <K > keyType ) {
93+ private WithKeys (SerializableFunction <V , K > fn , @ Nullable TypeDescriptor <K > keyType ) {
9694 this .fn = fn ;
9795 this .keyType = keyType ;
9896 }
@@ -121,6 +119,7 @@ public KV<K, V> apply(V element) {
121119 }
122120 }));
123121
122+ TypeDescriptor <K > keyType = this .keyType ;
124123 try {
125124 Coder <K > keyCoder ;
126125 CoderRegistry coderRegistry = in .getPipeline ().getCoderRegistry ();
0 commit comments