kennknowles commented on a change in pull request #13635: URL: https://github.com/apache/beam/pull/13635#discussion_r551482389
########## File path: sdks/java/core/src/test/java/org/apache/beam/sdk/coders/DequeCoderTest.java ########## @@ -0,0 +1,92 @@ +/* + * 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.coders; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.Collections; +import java.util.Deque; +import java.util.List; +import org.apache.beam.sdk.testing.CoderProperties; +import org.apache.beam.sdk.transforms.windowing.GlobalWindow; +import org.apache.beam.sdk.util.CoderUtils; +import org.apache.beam.sdk.values.TypeDescriptor; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Test case for {@link DequeCoder}. */ +@RunWith(JUnit4.class) +@SuppressWarnings({ + "nullness" // TODO(https://issues.apache.org/jira/browse/BEAM-10402) +}) +public class DequeCoderTest { + + private static final Coder<Deque<Integer>> TEST_CODER = DequeCoder.of(VarIntCoder.of()); + + private static final List<Deque<Integer>> TEST_VALUES = + Arrays.asList( Review comment: Prefer `ImmutableList.of` since this is not converting an array to a list, but just building a list. ########## File path: sdks/java/core/src/main/java/org/apache/beam/sdk/coders/DequeCoder.java ########## @@ -0,0 +1,81 @@ +/* + * 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.coders; + +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.List; +import org.apache.beam.sdk.values.TypeDescriptor; +import org.apache.beam.sdk.values.TypeParameter; + +/** + * A {@link Coder} for {@link Deque}, using the format of {@link IterableLikeCoder}. + * + * @param <T> the type of the elements of the Deques being transcoded + */ +public class DequeCoder<T> extends IterableLikeCoder<T, Deque<T>> { + + public static <T> DequeCoder<T> of(Coder<T> elemCoder) { + return new DequeCoder<>(elemCoder); + } + + ///////////////////////////////////////////////////////////////////////////// + // Internal operations below here. + + @Override + protected Deque<T> decodeToIterable(List<T> decodedElements) { + return new ArrayDeque<>(decodedElements); + } + + protected DequeCoder(Coder<T> elemCoder) { + super(elemCoder, "Deque"); + } + + @Override + public boolean consistentWithEquals() { + return getElemCoder().consistentWithEquals(); + } + + @Override + public Object structuralValue(Deque<T> values) { + if (consistentWithEquals()) { + return values; + } else { + final Deque<Object> ret = new ArrayDeque<>(values.size()); + for (T value : values) { + ret.add(getElemCoder().structuralValue(value)); + } + return ret; + } + } + + /** + * Deque sizes are always known, so DequeIterable may be deterministic while the general + * IterableLikeCoder is not. + */ + @Override + public void verifyDeterministic() throws NonDeterministicException { + verifyDeterministic(this, "ListCoder.elemCoder must be deterministic", getElemCoder()); Review comment: `DequeCoder.elemCoder` or better to even not reference private variables and say "Coder for elements of `DequeCoder` must be determistic". ########## File path: sdks/java/core/src/test/java/org/apache/beam/sdk/coders/DequeCoderTest.java ########## @@ -0,0 +1,92 @@ +/* + * 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.coders; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.Collections; +import java.util.Deque; +import java.util.List; +import org.apache.beam.sdk.testing.CoderProperties; +import org.apache.beam.sdk.transforms.windowing.GlobalWindow; +import org.apache.beam.sdk.util.CoderUtils; +import org.apache.beam.sdk.values.TypeDescriptor; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Test case for {@link DequeCoder}. */ +@RunWith(JUnit4.class) +@SuppressWarnings({ + "nullness" // TODO(https://issues.apache.org/jira/browse/BEAM-10402) +}) +public class DequeCoderTest { + + private static final Coder<Deque<Integer>> TEST_CODER = DequeCoder.of(VarIntCoder.of()); + + private static final List<Deque<Integer>> TEST_VALUES = + Arrays.asList( + new ArrayDeque<>(), + new ArrayDeque<>(Collections.singleton(13)), + new ArrayDeque<>(Arrays.asList(31, -5, 83))); Review comment: It would be good to have some tests where the element type is not integer. For example for the case where the inner type is arrays or some other nested structure. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
