|
| 1 | +package org.codefx.libfx.serialization; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileOutputStream; |
| 6 | +import java.io.NotSerializableException; |
| 7 | +import java.io.ObjectInputStream; |
| 8 | +import java.io.ObjectOutputStream; |
| 9 | +import java.io.Serializable; |
| 10 | +import java.util.Optional; |
| 11 | +import java.util.Random; |
| 12 | + |
| 13 | +/** |
| 14 | + * Demonstrates how to use the {@link SerializableOptional}. |
| 15 | + */ |
| 16 | +@SuppressWarnings("static-method") |
| 17 | +public class SerializableOptionalDemo { |
| 18 | + |
| 19 | + /** |
| 20 | + * Runs this demo. |
| 21 | + * |
| 22 | + * @param args |
| 23 | + * command line arguments (will not be used) |
| 24 | + * @throws Exception |
| 25 | + * if (de)serialization fails |
| 26 | + */ |
| 27 | + public static void main(String[] args) throws Exception { |
| 28 | + SerializableOptionalDemo demo = new SerializableOptionalDemo(); |
| 29 | + |
| 30 | + demo.serializeString(); |
| 31 | + demo.failSerializingOptional(); |
| 32 | + demo.serializeEmptySerializableOptional(); |
| 33 | + demo.serializeNonEmptySerializableOptional(); |
| 34 | + |
| 35 | + print(""); |
| 36 | + |
| 37 | + demo.callMethodsWithSerializableOptional(); |
| 38 | + } |
| 39 | + |
| 40 | + // DEMO |
| 41 | + |
| 42 | + // serialize "simple" objects, i.e. ones which contain no further instances, to demo serialization in general |
| 43 | + |
| 44 | + /** |
| 45 | + * To get started, serialize a string, deserialize it and print its value. |
| 46 | + * |
| 47 | + * @throws Exception |
| 48 | + * if (de)serialization fails |
| 49 | + */ |
| 50 | + private void serializeString() throws Exception { |
| 51 | + String someString = "a string"; |
| 52 | + String deserializedString = serializeAndDeserialize(someString); |
| 53 | + print("The deserialized 'String' is \"" + deserializedString + "\"."); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Try the same with an {@code Optional<String>}, which will fail as {@link Optional} is not {@link Serializable}. |
| 58 | + * |
| 59 | + * @throws Exception |
| 60 | + * if (de)serialization fails |
| 61 | + */ |
| 62 | + private void failSerializingOptional() throws Exception { |
| 63 | + try { |
| 64 | + Optional<String> someOptional = Optional.of("another string"); |
| 65 | + Optional<String> deserializedOptional = serializeAndDeserialize(someOptional); |
| 66 | + print("The deserialized 'Optional' should have the value \"" + deserializedOptional.get() + "\"."); |
| 67 | + } catch (NotSerializableException e) { |
| 68 | + print("Serialization of 'Optional' failed as expected."); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Create a {@link SerializableOptional} from an empty {@link Optional} and (de)serialize it successfully. |
| 74 | + * |
| 75 | + * @throws Exception |
| 76 | + * if (de)serialization fails |
| 77 | + */ |
| 78 | + private void serializeEmptySerializableOptional() throws Exception { |
| 79 | + Optional<String> someOptional = Optional.empty(); |
| 80 | + SerializableOptional<String> serializableOptional = SerializableOptional.fromOptional(someOptional); |
| 81 | + Optional<String> deserializedOptional = serializeAndDeserialize(serializableOptional).asOptional(); |
| 82 | + print("The deserialized empty 'SerializableOptional' has no value: " + !deserializedOptional.isPresent() + "."); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Create a {@link SerializableOptional} from a nonempty {@link Optional} and (de)serialize it successfully. |
| 87 | + * |
| 88 | + * @throws Exception |
| 89 | + * if (de)serialization fails |
| 90 | + */ |
| 91 | + private void serializeNonEmptySerializableOptional() throws Exception { |
| 92 | + Optional<String> someOptional = Optional.of("another string"); |
| 93 | + SerializableOptional<String> serializableOptional = SerializableOptional.fromOptional(someOptional); |
| 94 | + Optional<String> deserializedOptional = serializeAndDeserialize(serializableOptional).asOptional(); |
| 95 | + print("The deserialized non-empty 'SerializableOptional' has the value \"" + deserializedOptional.get() + "\"."); |
| 96 | + } |
| 97 | + |
| 98 | + // use 'SerializableOptional' in method signatures |
| 99 | + |
| 100 | + /** |
| 101 | + * Shows how to quickly wrap and unwrap an {@link Optional} for RPC method calls which rely on serialization. |
| 102 | + * <p> |
| 103 | + * Note that {@link SearchAndLog}'s methods have {@link SerializableOptional} as argument and return type. |
| 104 | + */ |
| 105 | + private void callMethodsWithSerializableOptional() { |
| 106 | + SearchAndLog searchAndLog = new SearchAndLog(); |
| 107 | + for (int id = 0; id < 7; id++) { |
| 108 | + // unwrap the returned optional using 'asOptional' |
| 109 | + Optional<String> searchResult = searchAndLog.search(id).asOptional(); |
| 110 | + // wrap the optional using 'fromOptional'; if used often, this could be a static import |
| 111 | + searchAndLog.log(id, SerializableOptional.fromOptional(searchResult)); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // USABILITY |
| 116 | + |
| 117 | + /** |
| 118 | + * Serializes the specified instance to disk. Then deserializes the file and returns the deserialized value. |
| 119 | + * |
| 120 | + * @param <T> |
| 121 | + * the type of the serialized instance |
| 122 | + * @param serialized |
| 123 | + * the instance to be serialized |
| 124 | + * @return the deserialized instance |
| 125 | + * @throws Exception |
| 126 | + * if (de)serialization fails |
| 127 | + */ |
| 128 | + private static <T> T serializeAndDeserialize(T serialized) throws Exception { |
| 129 | + File serializeFile = new File("_serialized"); |
| 130 | + // serialize |
| 131 | + try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(serializeFile))) { |
| 132 | + out.writeObject(serialized); |
| 133 | + } |
| 134 | + // deserialize |
| 135 | + try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(serializeFile))) { |
| 136 | + @SuppressWarnings("unchecked") |
| 137 | + T deserialized = (T) in.readObject(); |
| 138 | + return deserialized; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Prints the specified text to the console. |
| 144 | + * |
| 145 | + * @param text |
| 146 | + * the text to print |
| 147 | + */ |
| 148 | + private static void print(String text) { |
| 149 | + System.out.println(text); |
| 150 | + } |
| 151 | + |
| 152 | + // INNER CLASS FOR METHOD CALLS |
| 153 | + |
| 154 | + /** |
| 155 | + * A class with methods which have an optional return value or argument. |
| 156 | + */ |
| 157 | + @SuppressWarnings("javadoc") |
| 158 | + private static class SearchAndLog { |
| 159 | + |
| 160 | + Random random = new Random(); |
| 161 | + |
| 162 | + public SerializableOptional<String> search(@SuppressWarnings("unused") int id) { |
| 163 | + boolean searchSuccessfull = random.nextBoolean(); |
| 164 | + if (searchSuccessfull) |
| 165 | + return SerializableOptional.of("found something!"); |
| 166 | + else |
| 167 | + return SerializableOptional.empty(); |
| 168 | + } |
| 169 | + |
| 170 | + public void log(int id, SerializableOptional<String> item) { |
| 171 | + print("Search result for id " + id + ": " + item.asOptional().orElse("empty search result")); |
| 172 | + } |
| 173 | + |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments