diff --git a/maven-projects/info/src/main/java/org/apache/graphar/info/VertexInfo.java b/maven-projects/info/src/main/java/org/apache/graphar/info/VertexInfo.java index b4a14c5ee..e0e15ef87 100644 --- a/maven-projects/info/src/main/java/org/apache/graphar/info/VertexInfo.java +++ b/maven-projects/info/src/main/java/org/apache/graphar/info/VertexInfo.java @@ -21,6 +21,7 @@ import java.io.Writer; import java.net.URI; +import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Optional; @@ -37,6 +38,102 @@ public class VertexInfo { private final URI baseUri; private final VersionInfo version; + public static VertexInfoBuilder builder() { + return new VertexInfoBuilder(); + } + + public static class VertexInfoBuilder { + private String type; + private long chunkSize; + private PropertyGroups propertyGroups; + private URI baseUri; + private VersionInfo version; + private List propertyGroupsAsListTemp; + + private VertexInfoBuilder() {} + + public VertexInfoBuilder type(String type) { + this.type = type; + return this; + } + + public VertexInfoBuilder chunkSize(long chunkSize) { + this.chunkSize = chunkSize; + return this; + } + + public VertexInfoBuilder baseUri(URI baseUri) { + this.baseUri = baseUri; + + return this; + } + + public VertexInfoBuilder baseUri(String baseUri) { + this.baseUri = URI.create(baseUri); + + return this; + } + + public VertexInfoBuilder version(VersionInfo version) { + this.version = version; + + return this; + } + + public VertexInfoBuilder version(String version) { + this.version = VersionParser.getVersion(version); + + return this; + } + + public VertexInfoBuilder addPropertyGroup(PropertyGroup propertyGroup) { + if (propertyGroupsAsListTemp == null) propertyGroupsAsListTemp = new ArrayList<>(); + propertyGroupsAsListTemp.add(propertyGroup); + return this; + } + + public VertexInfoBuilder addPropertyGroups(List propertyGroups) { + if (propertyGroupsAsListTemp == null) propertyGroupsAsListTemp = new ArrayList<>(); + propertyGroupsAsListTemp.addAll(propertyGroups); + return this; + } + + public VertexInfoBuilder propertyGroups(PropertyGroups propertyGroups) { + this.propertyGroups = propertyGroups; + return this; + } + + public VertexInfo build() { + if (propertyGroups == null && propertyGroupsAsListTemp != null) { + propertyGroups = new PropertyGroups(propertyGroupsAsListTemp); + } else if (propertyGroupsAsListTemp != null) { + propertyGroups = + propertyGroupsAsListTemp.stream() + .map(propertyGroups::addPropertyGroupAsNew) + .filter(Optional::isPresent) + .map(Optional::get) + .reduce((first, second) -> second) + .orElse(new PropertyGroups(new ArrayList<>())); + } + + if (chunkSize < 0) { + throw new IllegalArgumentException("Chunk size cannot be negative: " + chunkSize); + } + if (baseUri == null) { + throw new IllegalArgumentException("Base URI cannot be null"); + } + return new VertexInfo(this); + } + } + + private VertexInfo(VertexInfoBuilder builder) { + this.type = builder.type; + this.chunkSize = builder.chunkSize; + this.propertyGroups = builder.propertyGroups; + this.baseUri = builder.baseUri; + this.version = builder.version; + } + public VertexInfo( String type, long chunkSize, diff --git a/maven-projects/info/src/test/java/org/apache/graphar/info/VertexInfoTest.java b/maven-projects/info/src/test/java/org/apache/graphar/info/VertexInfoTest.java index 4cf486a9c..addd4687f 100644 --- a/maven-projects/info/src/test/java/org/apache/graphar/info/VertexInfoTest.java +++ b/maven-projects/info/src/test/java/org/apache/graphar/info/VertexInfoTest.java @@ -20,7 +20,9 @@ package org.apache.graphar.info; import java.net.URI; +import java.net.URISyntaxException; import java.util.Arrays; +import java.util.List; import org.apache.graphar.info.type.DataType; import org.apache.graphar.info.type.FileType; import org.junit.Assert; @@ -28,6 +30,57 @@ public class VertexInfoTest { + private VertexInfo.VertexInfoBuilder b = + VertexInfo.builder() + .baseUri("test") + .type("test") + .chunkSize(24) + .version("gar/v1") + .propertyGroups(new PropertyGroups(List.of(TestUtil.pg3))); + + @Test + public void testVertexInfoBasicBuilder() { + VertexInfo v = b.build(); + } + + @Test + public void testVertexInfoBuilderDoubleDeclaration() throws URISyntaxException { + VertexInfo v = b.baseUri(new URI("world")).build(); + + Assert.assertEquals(new URI("world"), v.getBaseUri()); + } + + @Test(expected = RuntimeException.class) + public void URInullTest() { + VertexInfo v = + VertexInfo.builder() + .type("test") + .chunkSize(24) + .version("gar/v1") + .propertyGroups(new PropertyGroups(List.of(TestUtil.pg3))) + .build(); + } + + @Test + public void propertyGroupAppendTest() { + VertexInfo v = b.addPropertyGroup(TestUtil.pg2).build(); + + Assert.assertEquals(2, v.getPropertyGroups().size()); + } + + @Test + public void propertyGroupAddOnlyTest() { + VertexInfo v = b.propertyGroups(null).addPropertyGroup(TestUtil.pg2).build(); + + Assert.assertEquals(1, v.getPropertyGroups().size()); + } + + @Test(expected = RuntimeException.class) + public void invalidChunkSizeTest() { + b.chunkSize(-1); + b.build(); + } + @Test public void testBuildWithPrefix() { try {