|
19 | 19 | * <p>
|
20 | 20 | * There are three ways to use this class to serialize instances which have an optional field.
|
21 | 21 | * <p>
|
22 |
| - * <h2>Transform For Serialization Proxy</h2> If the class is serialized using the Serialization Proxy Pattern (see |
23 |
| - * <i>Effective Java, 2nd Edition</i> by Joshua Bloch, Item 78), the proxy can have an instance of |
24 |
| - * {@link SerializableOptional} to clearly denote the field as being optional. |
25 |
| - * <p> |
26 |
| - * In this case, the proxy needs to transform the {@code Optional} to {@code SerializableOptional} in its constructor |
27 |
| - * (using {@link SerializableOptional#fromOptional(Optional)}) and the other way in {@code readResolve()} (with |
28 |
| - * {@link SerializableOptional#asOptional()}). |
29 |
| - * <p> |
30 |
| - * A code example can be found in this class which implements the pattern. |
31 |
| - * <p> |
32 |
| - * <h2>Transform For Custom Serialized Form</h2> The original field can be declared as |
33 |
| - * {@code transient Optional<T> optionalField}, which will exclude it from serialization. |
| 22 | + * <h2>Transform On Serialization</h2> The field can be declared as {@code transient Optional<T> optionalField}, which |
| 23 | + * will exclude it from serialization. |
34 | 24 | * <p>
|
35 | 25 | * The class then needs to implement custom (de)serialization methods {@code writeObject} and {@code readObject}. They
|
36 | 26 | * must transform the {@code optionalField} to a {@code SerializableOptional} when writing the object and after reading
|
|
54 | 44 | * }
|
55 | 45 | * </pre>
|
56 | 46 | * <p>
|
57 |
| - * <h2>Transform For Access</h2> The field can be declared as {@code SerializableOptional<T> optionalField}. This will |
| 47 | + * <h2>Transform On Replace</h2> If the class is serialized using the Serialization Proxy Pattern (see <i>Effective |
| 48 | + * Java, 2nd Edition</i> by Joshua Bloch, Item 78), the proxy can have an instance of {@link SerializableOptional} to |
| 49 | + * clearly denote the field as being optional. |
| 50 | + * <p> |
| 51 | + * In this case, the proxy needs to transform the {@code Optional} to {@code SerializableOptional} in its constructor |
| 52 | + * (using {@link SerializableOptional#fromOptional(Optional)}) and the other way in {@code readResolve()} (with |
| 53 | + * {@link SerializableOptional#asOptional()}). |
| 54 | + * <p> |
| 55 | + * <h2>Transform On Access</h2> The field can be declared as {@code SerializableOptional<T> optionalField}. This will |
58 | 56 | * include it in the (de)serialization process so it does not need to be customized.
|
59 | 57 | * <p>
|
60 | 58 | * But methods interacting with the field need to get an {@code Optional} instead. This can easily be done by writing
|
@@ -87,7 +85,7 @@ public final class SerializableOptional<T extends Serializable> implements Seria
|
87 | 85 | private static final long serialVersionUID = -652697447004597911L;
|
88 | 86 |
|
89 | 87 | /**
|
90 |
| - * The wrapped {@link Optional}. |
| 88 | + * The wrapped {@link Optional}. Note that this field is transient so it will not be (de)serializd automatically. |
91 | 89 | */
|
92 | 90 | private final Optional<T> optional;
|
93 | 91 |
|
@@ -130,23 +128,23 @@ public static <T extends Serializable> SerializableOptional<T> empty() {
|
130 | 128 | }
|
131 | 129 |
|
132 | 130 | /**
|
133 |
| - * Creates a serializable optional for the specified, non-null value by wrapping it in an {@link Optional}. |
| 131 | + * Creates a serializable optional for the specified value by wrapping it in an {@link Optional}. |
134 | 132 | *
|
135 | 133 | * @param <T>
|
136 | 134 | * the type of the wrapped value
|
137 | 135 | * @param value
|
138 |
| - * the value which will be contained in the wrapped {@link Optional}; must not be null |
| 136 | + * the value which will be contained in the wrapped {@link Optional}; must be non-null |
139 | 137 | * @return a {@link SerializableOptional} which wraps the an optional for the specified value
|
140 | 138 | * @throws NullPointerException
|
141 |
| - * if value is null |
| 139 | + * if {@code value} is null |
142 | 140 | * @see Optional#of(Object)
|
143 | 141 | */
|
144 | 142 | public static <T extends Serializable> SerializableOptional<T> of(T value) throws NullPointerException {
|
145 | 143 | return new SerializableOptional<>(Optional.of(value));
|
146 | 144 | }
|
147 | 145 |
|
148 | 146 | /**
|
149 |
| - * Creates a serializable optional for the specified, possibly null value by wrapping it in an {@link Optional}. |
| 147 | + * Creates a serializable optional for the specified value by wrapping it in an {@link Optional}. |
150 | 148 | *
|
151 | 149 | * @param <T>
|
152 | 150 | * the type of the wrapped value
|
|
0 commit comments