gemini-code-assist[bot] commented on code in PR #37824:
URL: https://github.com/apache/beam/pull/37824#discussion_r2918650806


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/state/ValueState.java:
##########
@@ -37,6 +37,15 @@ public interface ValueState<T> extends 
ReadableState<@Nullable T>, State {
   @Nullable
   T read();
 
+  /**
+   * Returns the current value of the state, or {@code defaultValue} if the 
value has never been
+   * written.
+   */

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The Javadoc states that `defaultValue` is returned 'if the value has never 
been written'. However, the implementation returns `defaultValue` whenever 
`read()` returns `null`. This can occur not only when the state is 
uninitialized, but also when `null` has been explicitly written or the state 
has been cleared. To avoid confusion, the Javadoc should be updated to 
accurately reflect this behavior.
   
   ```suggestion
     /**
      * Returns the current value of the state, or {@code defaultValue} if the 
current value is {@code
      * null}. This can happen if the value has never been written or if {@code 
null} was written.
      */
   ```



##########
sdks/java/core/src/test/java/org/apache/beam/sdk/state/ValueStateTest.java:
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.state;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for {@link ValueState}. */
+@RunWith(JUnit4.class)
+public class ValueStateTest {

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The tests for the new functionality are a good start. To make them more 
comprehensive, consider adding test cases for edge scenarios. Specifically, 
testing the behavior after `state.clear()` is called, and after `null` is 
explicitly written to the state, would strengthen the test suite and clarify 
the contract of the new method.
   
   For example:
   
   ```java
   @Test
   public void testReadReturnsDefaultValueAfterClear() {
     TestValueState<Integer> state = new TestValueState<>();
     state.write(10);
     state.clear();
     assertEquals(Integer.valueOf(5), state.read(5));
   }
   
   @Test
   public void testReadReturnsDefaultValueWhenNullIsWritten() {
     TestValueState<Integer> state = new TestValueState<>();
     state.write(null);
     assertEquals(Integer.valueOf(5), state.read(5));
   }
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to