Skip to content

Commit

Permalink
#299 nullable and default containers, first cut
Browse files Browse the repository at this point in the history
  • Loading branch information
elucash committed Apr 15, 2016
1 parent 101ce82 commit a508869
Show file tree
Hide file tree
Showing 10 changed files with 687 additions and 312 deletions.
3 changes: 2 additions & 1 deletion value-fixture/pom.xml
Expand Up @@ -14,7 +14,8 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down
@@ -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;
}
}
@@ -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();
}
@@ -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();
}
@@ -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");
}
}

0 comments on commit a508869

Please sign in to comment.