je-ik commented on code in PR #32074: URL: https://github.com/apache/beam/pull/32074#discussion_r1714090124
########## sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/SerializableComparator.java: ########## @@ -19,10 +19,29 @@ import java.io.Serializable; import java.util.Comparator; +import java.util.Objects; +import java.util.function.Function; /** * A {@code Comparator} that is also {@code Serializable}. * * @param <T> type of values being compared */ -public interface SerializableComparator<T> extends Comparator<T>, Serializable {} +public interface SerializableComparator<T> extends Comparator<T>, Serializable { + /** + * Analogous to {@link Comparator#comparing(Function)}, except that it takes in a {@link + * SerializableFunction} as the key extractor and returns a {@link SerializableComparator}. + * + * @param keyExtractor the function used to extract the {@link java.lang.Comparable} sort key + * @return A {@link SerializableComparator} that compares by an extracted key + * @param <T> the type of element to be compared + * @param <V> the type of the {@code Comparable} sort key + * @see Comparator#comparing(Function) + */ + static <T, V extends Comparable<? super V>> SerializableComparator<T> comparing( Review Comment: I wonder, why `? super V` and not using `V` directly? Does it have any advantages? ########## sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/SerializableComparator.java: ########## @@ -19,10 +19,29 @@ import java.io.Serializable; import java.util.Comparator; +import java.util.Objects; +import java.util.function.Function; /** * A {@code Comparator} that is also {@code Serializable}. * * @param <T> type of values being compared */ -public interface SerializableComparator<T> extends Comparator<T>, Serializable {} +public interface SerializableComparator<T> extends Comparator<T>, Serializable { + /** + * Analogous to {@link Comparator#comparing(Function)}, except that it takes in a {@link + * SerializableFunction} as the key extractor and returns a {@link SerializableComparator}. + * + * @param keyExtractor the function used to extract the {@link java.lang.Comparable} sort key + * @return A {@link SerializableComparator} that compares by an extracted key + * @param <T> the type of element to be compared + * @param <V> the type of the {@code Comparable} sort key + * @see Comparator#comparing(Function) + */ + static <T, V extends Comparable<? super V>> SerializableComparator<T> comparing( + SerializableFunction<? super T, ? extends V> keyExtractor) { Review Comment: Same here probably for `T`? ########## sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/SerializableComparatorTest.java: ########## @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.beam.sdk.transforms; + +import java.io.Serializable; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link SerializableComparator}. */ +@RunWith(JUnit4.class) +public class SerializableComparatorTest { + + /** Basic test of {@link SerializableComparator} being {@link java.io.Serializable}. */ + @Test + public void testIfImplementsSerializable() { + SerializableFunction<String, Integer> fn = Integer::parseInt; + + SerializableComparator<String> cmp = SerializableComparator.comparing(fn); + Assert.assertTrue(cmp instanceof Serializable); Review Comment: This does not imply that the instance can be serialized. It would be better to use the `SerializableUtils` to check the serialization and deserialization. ########## sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/SerializableComparatorTest.java: ########## @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.beam.sdk.transforms; + +import java.io.Serializable; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link SerializableComparator}. */ +@RunWith(JUnit4.class) +public class SerializableComparatorTest { + + /** Basic test of {@link SerializableComparator} being {@link java.io.Serializable}. */ + @Test + public void testIfImplementsSerializable() { + SerializableFunction<String, Integer> fn = Integer::parseInt; + + SerializableComparator<String> cmp = SerializableComparator.comparing(fn); + Assert.assertTrue(cmp instanceof Serializable); Review Comment: You could have left the original tests as well, those were fine. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@beam.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org