Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#299 nullable and default containers, first cut
- Loading branch information
Showing
10 changed files
with
687 additions
and
312 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
27 changes: 27 additions & 0 deletions
27
value-fixture/src/org/immutables/fixture/nullable/NonnullConstruction.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,27 @@ | ||
package org.immutables.fixture.nullable; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.List; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import org.immutables.gson.Gson; | ||
import org.immutables.value.Value; | ||
|
||
@Gson.TypeAdapters | ||
@Value.Immutable | ||
@JsonDeserialize(as = ImmutableNonnullConstruction.class) | ||
@Value.Style(allParameters = true, defaultAsDefault = true) | ||
public interface NonnullConstruction { | ||
String[] arr(); | ||
|
||
@Nullable | ||
String[] brr(); | ||
|
||
default List<String> ax() { | ||
return null;// will blowup unless set explicitly to nonnull | ||
} | ||
|
||
@Value.Derived | ||
default int a() { | ||
return 1; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
value-fixture/src/org/immutables/fixture/nullable/NullableAttributes.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,39 @@ | ||
package org.immutables.fixture.nullable; | ||
|
||
import java.util.Collections; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import java.util.Set; | ||
import org.immutables.gson.Gson; | ||
import java.util.Map; | ||
import java.util.List; | ||
import javax.annotation.Nullable; | ||
import org.immutables.value.Value; | ||
|
||
|
||
@Gson.TypeAdapters | ||
@Value.Immutable | ||
@JsonDeserialize(as = ImmutableNullableAttributes.class) | ||
@Value.Style(defaultAsDefault = true) | ||
public interface NullableAttributes { | ||
@Nullable | ||
Integer integer(); | ||
|
||
@Nullable | ||
List<String> list(); | ||
|
||
@Nullable | ||
default Set<Integer> set() { | ||
return Collections.emptySet(); | ||
} | ||
|
||
@Nullable | ||
Integer[] array(); | ||
|
||
@Nullable | ||
default Double[] defArray() { | ||
return new Double[] {Double.NaN}; | ||
} | ||
|
||
@Nullable | ||
Map<String, Object> map(); | ||
} |
19 changes: 19 additions & 0 deletions
19
value-fixture/src/org/immutables/fixture/nullable/NullableCompact.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,19 @@ | ||
package org.immutables.fixture.nullable; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
import org.immutables.gson.Gson; | ||
import org.immutables.value.Value; | ||
|
||
@Gson.TypeAdapters | ||
@Value.Immutable | ||
@JsonDeserialize(as = ImmutableNullableCompact.class) | ||
@Value.Style(allParameters = true) | ||
public abstract class NullableCompact { | ||
@Nullable | ||
public abstract Integer[] array(); | ||
|
||
@Nullable | ||
public abstract Map<String, Object> map(); | ||
} |
73 changes: 73 additions & 0 deletions
73
value-fixture/test/org/immutables/fixture/nullable/NullableAttributesTest.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,73 @@ | ||
package org.immutables.fixture.nullable; | ||
|
||
import java.util.Arrays; | ||
import org.junit.Test; | ||
import static org.immutables.check.Checkers.check; | ||
|
||
public class NullableAttributesTest { | ||
@Test | ||
public void defaultValues() { | ||
ImmutableNullableAttributes a1 = ImmutableNullableAttributes.builder() | ||
.build(); | ||
|
||
check(a1.array()).isNull(); | ||
check(a1.integer()).isNull(); | ||
check(a1.defArray()).isOf(Double.NaN); | ||
check(a1.list()).isNull(); | ||
check(a1.set()).notNull(); | ||
check(a1.set()).isEmpty(); | ||
check(a1.map()).isNull(); | ||
} | ||
|
||
@Test | ||
public void explicitNulls() { | ||
ImmutableNullableAttributes a1 = ImmutableNullableAttributes.builder() | ||
.set(null) | ||
.defArray((Double[]) null) | ||
.build(); | ||
|
||
check(a1.defArray()).isNull(); | ||
check(a1.set()).isNull(); | ||
} | ||
|
||
@Test | ||
public void nonDefaultValues() { | ||
ImmutableNullableAttributes a1 = ImmutableNullableAttributes.builder() | ||
.addSet(1) | ||
.defArray(1.0, 2.0) | ||
.addList("a") | ||
.addAllList(Arrays.asList("b", "c")) | ||
.putMap("key", new Object()) | ||
.build(); | ||
|
||
check(a1.set()).isOf(1); | ||
check(a1.defArray()).isOf(1.0, 2.0); | ||
check(a1.list()).isOf("a", "b", "c"); | ||
check(a1.map().keySet()).isOf("key"); | ||
} | ||
|
||
@Test | ||
public void compactConstruction() { | ||
ImmutableNullableCompact c1 = ImmutableNullableCompact.builder() | ||
.build(); | ||
|
||
check(ImmutableNullableCompact.of(null, null)).is(c1); | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void nonnullDefaultBlowupOnNull() { | ||
ImmutableNonnullConstruction.builder() | ||
.arr() | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void nonnullDefault() { | ||
check(ImmutableNonnullConstruction.builder() | ||
.arr() | ||
.addAx("a") | ||
.addAx("b", "c") | ||
.build().ax()) | ||
.isOf("a", "b", "c"); | ||
} | ||
} |
Oops, something went wrong.