xref: /aosp_15_r20/external/aws-sdk-java-v2/docs/design/UseOfOptional.md (revision 8a52c7834d808308836a99fc2a6e0ed8db339086)
1*8a52c783SCole Faust**Design:** Convention, **Status:** [Accepted](README.md)
2*8a52c783SCole Faust
3*8a52c783SCole Faust## Use of Optional
4*8a52c783SCole Faust
5*8a52c783SCole FaustThis page describes a general guideline for the use of
6*8a52c783SCole Faust`java.util.Optional` in the AWS SDK for Java 2.x.
7*8a52c783SCole Faust
8*8a52c783SCole Faust- `Optional` MUST NOT be used under any circumstances when the result will never be null.
9*8a52c783SCole Faust- For return types,
10*8a52c783SCole Faust  - `Optional` SHOULD be used when it is not obvious to a caller whether a
11*8a52c783SCole Faust  result will be null, e.g, `public <T> Optional<T> getValueForField(String fieldName, Class<T> clazz)`in [SdkResponse.java](https://github.com/aws/aws-sdk-java-v2/blob/aa161c564c580ced4a0381d3ed7d4d13120916fc/core/sdk-core/src/main/java/software/amazon/awssdk/core/SdkResponse.java#L59-L61)
12*8a52c783SCole Faust  - `Optional` MUST NOT be used for "getters" in generated service model classes such as service Builders or POJOs.
13*8a52c783SCole Faust- For member variables: `Optional` SHOULD NOT be used, e.g., `private final Optional<String> field;`
14*8a52c783SCole Faust- For method parameters: `Optional` MUST NOT be used, e.g., `private void test(Optional<String> value)`
15*8a52c783SCole Faust
16*8a52c783SCole Faust
17*8a52c783SCole FaustReferences:
18*8a52c783SCole Faust
19*8a52c783SCole Faust- http://blog.joda.org/2015/08/java-se-8-optional-pragmatic-approach.html
20