[Java SDK] Add ValueState.read(T defaultValue) overload#37824
[Java SDK] Add ValueState.read(T defaultValue) overload#37824Ri1a wants to merge 1 commit intoapache:masterfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a convenient read(T defaultValue) overload to ValueState, which simplifies handling of uninitialized state. The implementation is sound and the change is backward compatible. My review includes a suggestion to clarify the Javadoc for the new method to more accurately describe its behavior, particularly regarding null values. I also recommend adding a couple of test cases to cover edge scenarios like clear() and writing null to the state, which will make the test suite more robust.
| /** | ||
| * Returns the current value of the state, or {@code defaultValue} if the value has never been | ||
| * written. | ||
| */ |
There was a problem hiding this comment.
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.
| /** | |
| * Returns the current value of the state, or {@code defaultValue} if the value has never been | |
| * written. | |
| */ | |
| /** | |
| * 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. | |
| */ |
|
|
||
| /** Tests for {@link ValueState}. */ | ||
| @RunWith(JUnit4.class) | ||
| public class ValueStateTest { |
There was a problem hiding this comment.
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:
@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));
}|
Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment |
Fixes #18174
This change adds a convenience overload
ValueState.read(T defaultValue)to simplify handling of uninitialized state. Currently,ValueState.read()returnsnullif the value has never been written. As a result, users frequently need to write boilerplate code such as:or:
This PR introduces a default method:
which returns the stored value if present, or
defaultValueif the state has not been written yet.Tests
Added
ValueStateTestto verify:read(defaultValue)returns the default when the state is emptyread(defaultValue)returns the stored value when presentreadLater()continues to return the same state instanceCompatibility
This change is fully backward compatible:
ValueStatedo not need modification