Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refinements and more tests for jdk optional unboxing, safer identifiers
(prevents keyword clashes for simplest cases) #240
- Loading branch information
Showing
8 changed files
with
195 additions
and
8 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
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
43 changes: 43 additions & 0 deletions
43
value-fixture/src/org/immutables/fixture/jdkonly/JdkOptionals.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,43 @@ | ||
package org.immutables.fixture.jdkonly; | ||
|
||
import java.io.Serializable; | ||
import java.util.Optional; | ||
import java.util.OptionalDouble; | ||
import java.util.OptionalInt; | ||
import java.util.OptionalLong; | ||
import org.immutables.gson.Gson; | ||
import org.immutables.serial.Serial; | ||
import org.immutables.value.Value; | ||
|
||
@Serial.Structural | ||
@Value.Immutable(builder = false, singleton = true, intern = true, prehash = true) | ||
@Gson.TypeAdapters | ||
public interface JdkOptionals { | ||
@Value.Parameter | ||
Optional<String> v2(); | ||
|
||
@Value.Parameter | ||
OptionalInt i1(); | ||
|
||
@Value.Parameter | ||
OptionalLong l1(); | ||
|
||
@Value.Parameter | ||
OptionalDouble d1(); | ||
} | ||
|
||
@Value.Immutable | ||
@Value.Style(privateNoargConstructor = true) | ||
interface JdkOptionalsSer extends Serializable { | ||
@Value.Parameter | ||
Optional<String> v2(); | ||
|
||
@Value.Parameter | ||
OptionalInt i1(); | ||
|
||
@Value.Parameter | ||
OptionalLong l1(); | ||
|
||
@Value.Parameter | ||
OptionalDouble d1(); | ||
} |
77 changes: 77 additions & 0 deletions
77
value-fixture/test/org/immutables/fixture/jdkonly/JdkOptionalTest.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,77 @@ | ||
package org.immutables.fixture.jdkonly; | ||
|
||
import java.util.OptionalLong; | ||
import java.util.OptionalDouble; | ||
import java.util.OptionalInt; | ||
import java.util.Optional; | ||
import org.junit.Test; | ||
import static org.immutables.check.Checkers.check; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.io.Serializable; | ||
|
||
// tests how unboxing of jdk optionals is implemented | ||
public class JdkOptionalTest { | ||
|
||
@Test | ||
public void equals() { | ||
ImmutableJdkOptionals o1 = ImmutableJdkOptionals.of() | ||
.withV2("v2") | ||
.withI1(1) | ||
.withD1(1.0); | ||
|
||
ImmutableJdkOptionals o2 = ImmutableJdkOptionals.of( | ||
Optional.of("v2"), | ||
OptionalInt.of(1), | ||
OptionalLong.empty(), | ||
OptionalDouble.of(1.0)); | ||
|
||
check(o1).is(o2); | ||
check(o1.hashCode()).is(o2.hashCode()); | ||
} | ||
|
||
@Test | ||
public void serializeStructural() throws Exception { | ||
ImmutableJdkOptionals o1 = ImmutableJdkOptionals.of() | ||
.withV2("v2") | ||
.withI1(1); | ||
|
||
check(deserialize(serialize(o1))).is(o1); | ||
} | ||
|
||
@Test | ||
public void serializeStructuralSingleton() throws Exception { | ||
ImmutableJdkOptionals o1 = ImmutableJdkOptionals.of(); | ||
check(deserialize(serialize(o1))).same(o1); | ||
} | ||
|
||
@Test | ||
public void serializeRegular() throws Exception { | ||
ImmutableJdkOptionalsSer o0 = ImmutableJdkOptionalsSer.builder() | ||
.d1(1.0) | ||
.l1(2L) | ||
.v2("e4") | ||
.build(); | ||
|
||
Serializable o1 = deserialize(serialize(o0)); | ||
check(o1).is(o0); | ||
check(o1.hashCode()).is(o0.hashCode()); | ||
} | ||
|
||
private Serializable deserialize(byte[] bytes) throws Exception { | ||
ByteArrayInputStream stream = new ByteArrayInputStream(bytes); | ||
ObjectInputStream objectStream = new ObjectInputStream(stream); | ||
return (Serializable) objectStream.readObject(); | ||
} | ||
|
||
private byte[] serialize(Serializable instance) throws Exception { | ||
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||
ObjectOutputStream objectStream = new ObjectOutputStream(stream); | ||
objectStream.writeObject(instance); | ||
objectStream.close(); | ||
return stream.toByteArray(); | ||
} | ||
|
||
} |
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
24 changes: 24 additions & 0 deletions
24
value-processor/src/org/immutables/value/processor/meta/Keywords.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,24 @@ | ||
package org.immutables.value.processor.meta; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
|
||
class Keywords { | ||
private static final ImmutableSet<String> keywords = ImmutableSet.of( | ||
"abstract", "assert", "boolean", | ||
"break", "byte", "case", "catch", "char", "class", "const", | ||
"continue", "default", "do", "double", "else", "extends", "false", | ||
"final", "finally", "float", "for", "goto", "if", "implements", | ||
"import", "instanceof", "int", "interface", "long", "native", | ||
"new", "null", "package", "private", "protected", "public", | ||
"return", "short", "static", "strictfp", "super", "switch", | ||
"synchronized", "this", "throw", "throws", "transient", "true", | ||
"try", "void", "volatile", "while"); | ||
|
||
static boolean is(String identifier) { | ||
return keywords.contains(identifier); | ||
} | ||
|
||
static String safeIdentifier(String identifier, String fallback) { | ||
return is(identifier) ? fallback : identifier; | ||
} | ||
} |
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