13
13
* Note that it does not provide any of the methods {@code Optional} has as its only goal is to enable serialization.
14
14
* But it holds a reference to the {@code Optional} which was used to create it (can be accessed with
15
15
* {@link #asOptional()}). This {@code Optional} instance is of course reconstructed on deserialization, so it will not
16
- * the same as the one specified for its creation.
16
+ * be the same as the one specified for its creation.
17
17
* <p>
18
18
* The class can be used as an argument or return type for serialization-based RPC technologies like RMI.
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 On Serialization</h2> The field can be declared as {@code transient Optional<T> optionalField},
23
- * which will exclude it from serialization.
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.
24
34
* <p>
25
35
* The class then needs to implement custom (de)serialization methods {@code writeObject} and {@code readObject}. They
26
- * must transform the {@code optionalField} to a {@code SerializableOptional} when writing the object and after
27
- * reading such an instance transform it back to an {@code Optional}.
36
+ * must transform the {@code optionalField} to a {@code SerializableOptional} when writing the object and after reading
37
+ * such an instance transform it back to an {@code Optional}.
28
38
* <p>
29
39
* <h3>Code Example</h3>
30
40
*
44
54
* }
45
55
* </pre>
46
56
* <p>
47
- * <h2>Transform On Replace</h2> If the class is serialized using the Serialization Proxy Pattern (see <i>Effective
48
- * Java</i> by Joshua Bloch, Item 78), the proxy can have an instance of {@link SerializableOptional} to clearly denote
49
- * 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
56
- * will include it in the (de)serialization process so it does not need to be customized.
57
+ * <h2>Transform For Access</h2> The field can be declared as {@code SerializableOptional<T> optionalField}. This will
58
+ * include it in the (de)serialization process so it does not need to be customized.
57
59
* <p>
58
60
* But methods interacting with the field need to get an {@code Optional} instead. This can easily be done by writing
59
61
* the accessor methods such that they transform the field on each access.
@@ -85,7 +87,7 @@ public final class SerializableOptional<T extends Serializable> implements Seria
85
87
private static final long serialVersionUID = -652697447004597911L ;
86
88
87
89
/**
88
- * The wrapped {@link Optional}. Note that this field is transient so it will not be (de)serializd automatically.
90
+ * The wrapped {@link Optional}.
89
91
*/
90
92
private final Optional <T > optional ;
91
93
@@ -128,12 +130,12 @@ public static <T extends Serializable> SerializableOptional<T> empty() {
128
130
}
129
131
130
132
/**
131
- * Creates a serializable optional for the specified value by wrapping it in an {@link Optional}.
133
+ * Creates a serializable optional for the specified, non-null value by wrapping it in an {@link Optional}.
132
134
*
133
135
* @param <T>
134
- * the type of the wrapped value; must be non-null
136
+ * the type of the wrapped value
135
137
* @param value
136
- * the value which will be contained in the wrapped {@link Optional}; may be null
138
+ * the value which will be contained in the wrapped {@link Optional}; must not be null
137
139
* @return a {@link SerializableOptional} which wraps the an optional for the specified value
138
140
* @throws NullPointerException
139
141
* if value is null
@@ -144,7 +146,7 @@ public static <T extends Serializable> SerializableOptional<T> of(T value) throw
144
146
}
145
147
146
148
/**
147
- * Creates a serializable optional for the specified value by wrapping it in an {@link Optional}.
149
+ * Creates a serializable optional for the specified, possibly null value by wrapping it in an {@link Optional}.
148
150
*
149
151
* @param <T>
150
152
* the type of the wrapped value
0 commit comments