-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add serializer for java 8's java.util.Optional
It's registered as default serializer if java.util.Optional is available in the classpath. This is preferred to checking the java version (from sys props like e.g. "java.specification.version") because some platforms might provide Optional while not supporting the java 8 spec - who knows.
- Loading branch information
Showing
4 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/com/esotericsoftware/kryo/serializers/java8/OptionalSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.esotericsoftware.kryo.serializers.java8; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.Serializer; | ||
import com.esotericsoftware.kryo.io.Input; | ||
import com.esotericsoftware.kryo.io.Output; | ||
|
||
import java.util.Optional; | ||
|
||
import static com.esotericsoftware.minlog.Log.DEBUG; | ||
import static com.esotericsoftware.minlog.Log.debug; | ||
|
||
/** | ||
* Serializer for {@link Optional}. | ||
*/ | ||
public class OptionalSerializer extends Serializer<Optional> { | ||
|
||
private static final boolean OPTIONAL_SUPPORTED; | ||
|
||
static { | ||
boolean supported = false; | ||
try { | ||
Class.forName("java.util.Optional"); | ||
supported = true; | ||
} catch (Exception e) { | ||
if (DEBUG) { | ||
debug("Class 'java.util.Optional' not found, 'OptionalSerializer' won't be registered as default serializer."); | ||
} | ||
} | ||
OPTIONAL_SUPPORTED = supported; | ||
} | ||
|
||
{ | ||
setAcceptsNull(false); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public void write(Kryo kryo, Output output, Optional object) { | ||
Object nullable = object.isPresent() ? object.get() : null; | ||
kryo.writeClassAndObject(output, nullable); | ||
} | ||
|
||
@Override | ||
public Optional read(Kryo kryo, Input input, Class<Optional> type) { | ||
return Optional.ofNullable(kryo.readClassAndObject(input)); | ||
} | ||
|
||
@Override | ||
public Optional copy(Kryo kryo, Optional original) { | ||
if(original.isPresent()) { | ||
return Optional.of(kryo.copy(original.get())); | ||
} | ||
return original; | ||
} | ||
|
||
public static void addDefaultSerializer(Kryo kryo) { | ||
if(OPTIONAL_SUPPORTED) | ||
kryo.addDefaultSerializer(Optional.class, OptionalSerializer.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
test/com/esotericsoftware/kryo/serializers/java8/OptionalSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.esotericsoftware.kryo.serializers.java8; | ||
|
||
import com.esotericsoftware.kryo.KryoTestCase; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public class OptionalSerializerTest extends KryoTestCase { | ||
|
||
{ | ||
supportsCopy = true; | ||
} | ||
|
||
@Override | ||
protected void setUp() throws Exception { | ||
super.setUp(); | ||
kryo.register(Optional.class); | ||
kryo.register(TestClass.class); | ||
} | ||
|
||
public void testNull() { | ||
roundTrip(2, 2, new TestClass(null)); | ||
} | ||
|
||
public void testEmpty() { | ||
roundTrip(3, 3, new TestClass(Optional.<String>empty())); | ||
} | ||
|
||
public void testPresent() { | ||
roundTrip(6, 6, new TestClass(Optional.of("foo"))); | ||
} | ||
|
||
static class TestClass { | ||
Optional<String> maybe; | ||
public TestClass() {} | ||
public TestClass(Optional<String> maybe) { | ||
this.maybe = maybe; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
TestClass testClass = (TestClass) o; | ||
return Objects.equals(maybe, testClass.maybe); | ||
|
||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(maybe); | ||
} | ||
} | ||
|
||
} |