Skip to content

Commit 049900d

Browse files
author
nicolaiparlog
committed
Improved comments on 'SerializableOptional'.
1 parent 7d7c9b2 commit 049900d

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,28 @@
1313
* Note that it does not provide any of the methods {@code Optional} has as its only goal is to enable serialization.
1414
* But it holds a reference to the {@code Optional} which was used to create it (can be accessed with
1515
* {@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.
1717
* <p>
1818
* The class can be used as an argument or return type for serialization-based RPC technologies like RMI.
1919
* <p>
2020
* There are three ways to use this class to serialize instances which have an optional field.
2121
* <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.
2434
* <p>
2535
* 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}.
2838
* <p>
2939
* <h3>Code Example</h3>
3040
*
@@ -44,16 +54,8 @@
4454
* }
4555
* </pre>
4656
* <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.
5759
* <p>
5860
* But methods interacting with the field need to get an {@code Optional} instead. This can easily be done by writing
5961
* 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
8587
private static final long serialVersionUID = -652697447004597911L;
8688

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

@@ -128,12 +130,12 @@ public static <T extends Serializable> SerializableOptional<T> empty() {
128130
}
129131

130132
/**
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}.
132134
*
133135
* @param <T>
134-
* the type of the wrapped value; must be non-null
136+
* the type of the wrapped value
135137
* @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
137139
* @return a {@link SerializableOptional} which wraps the an optional for the specified value
138140
* @throws NullPointerException
139141
* if value is null
@@ -144,7 +146,7 @@ public static <T extends Serializable> SerializableOptional<T> of(T value) throw
144146
}
145147

146148
/**
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}.
148150
*
149151
* @param <T>
150152
* the type of the wrapped value

0 commit comments

Comments
 (0)