Ri1a opened a new pull request, #37824:
URL: https://github.com/apache/beam/pull/37824
Fixes #18174
This change adds a convenience overload `ValueState.read(T defaultValue)` to
simplify handling of uninitialized state. Currently, `ValueState.read()`
returns `null` if the value has never been written. As a result, users
frequently need to write boilerplate code such as:
```java
Integer count = state.read();
if (count == null) {
count = 0;
}
```
or:
```java
Integer count = MoreObjects.firstNonNull(state.read(), 0);
```
This PR introduces a default method:
```java
default T read(T defaultValue)
```
which returns the stored value if present, or `defaultValue` if the state
has not been written yet.
### Tests
Added `ValueStateTest` to verify:
* `read(defaultValue)` returns the default when the state is empty
* `read(defaultValue)` returns the stored value when present
* `readLater()` continues to return the same state instance
### Compatibility
This change is fully backward compatible:
* existing implementations of `ValueState` do not need modification
* the new method is implemented as a Java 8 default interface method
---
GitHub Actions Tests Status (on master branch)
------------------------------------------------------------------------------------------------
[](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more
information about GitHub Actions CI or the [workflows
README](https://github.com/apache/beam/blob/master/.github/workflows/README.md)
to see a list of phrases to trigger workflows.
--
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]