Skip to content

Commit 33298a3

Browse files
author
nicolaiparlog
committed
Improved comments on 'SerializableOptional'.
1 parent 4cfce1a commit 33298a3

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

src/main/java/org/codefx/libfx/serialization/SerializableOptional.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,8 @@
1919
* <p>
2020
* There are three ways to use this class to serialize instances which have an optional field.
2121
* <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.
3424
* <p>
3525
* The class then needs to implement custom (de)serialization methods {@code writeObject} and {@code readObject}. They
3626
* must transform the {@code optionalField} to a {@code SerializableOptional} when writing the object and after reading
@@ -54,7 +44,15 @@
5444
* }
5545
* </pre>
5646
* <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
5856
* include it in the (de)serialization process so it does not need to be customized.
5957
* <p>
6058
* 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
8785
private static final long serialVersionUID = -652697447004597911L;
8886

8987
/**
90-
* The wrapped {@link Optional}.
88+
* The wrapped {@link Optional}. Note that this field is transient so it will not be (de)serializd automatically.
9189
*/
9290
private final Optional<T> optional;
9391

@@ -130,23 +128,23 @@ public static <T extends Serializable> SerializableOptional<T> empty() {
130128
}
131129

132130
/**
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}.
134132
*
135133
* @param <T>
136134
* the type of the wrapped value
137135
* @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
139137
* @return a {@link SerializableOptional} which wraps the an optional for the specified value
140138
* @throws NullPointerException
141-
* if value is null
139+
* if {@code value} is null
142140
* @see Optional#of(Object)
143141
*/
144142
public static <T extends Serializable> SerializableOptional<T> of(T value) throws NullPointerException {
145143
return new SerializableOptional<>(Optional.of(value));
146144
}
147145

148146
/**
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}.
150148
*
151149
* @param <T>
152150
* the type of the wrapped value

0 commit comments

Comments
 (0)