forked from EsotericSoftware/kryo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request EsotericSoftware#362 from magro/java8-support
Add java8 support and serializer for java.util.Optional
- Loading branch information
Showing
16 changed files
with
214 additions
and
22 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
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
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); | ||
} | ||
} | ||
|
||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.