This is an automated email from the ASF dual-hosted git repository.
xiaokang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-graphar.git
The following commit(s) were added to refs/heads/main by this push:
new a2615c10 feat(java,info): add vertex info builder (#764)
a2615c10 is described below
commit a2615c1092e750b384a596c149336542c897b868
Author: Bigu Cezar <[email protected]>
AuthorDate: Fri Oct 17 05:12:06 2025 +0300
feat(java,info): add vertex info builder (#764)
* refactor: created basic builder for `VertexInfo` class
* test: Added basic testing for new `VertexInfoBuilder` and null checking
* style: applied spotless formatting
* chore: added license
* test: Removed redundant checks and added test cases for exceptions
* style: applied spotless
* fix: added exception expected for null tests
* style: re-applied spotless
* refactor: added builder methods for propertyGroups
* test: added tests for new property group methods
* style: applied spottless
* fix: fixed merge problems
* style: re-applied spottless
* test: Changed to `Assert.assertThrows`
* style: applied spottless
* style: renamed builders in tests for clarity
* fix: Replaced try/catch block with assertThrows/assertEquals
* style: applied spottless
---------
Signed-off-by: Bigu Cezar <[email protected]>
---
.../java/org/apache/graphar/info/VertexInfo.java | 101 +++++++++++++++++++++
.../org/apache/graphar/info/VertexInfoTest.java | 62 +++++++++++++
2 files changed, 163 insertions(+)
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 b4a14c5e..3a56ffb3 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 @@ package org.apache.graphar.info;
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,106 @@ 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<PropertyGroup> 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<PropertyGroup>
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 4cf486a9..4623c738 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,66 @@ import org.junit.Test;
public class VertexInfoTest {
+ private VertexInfo.VertexInfoBuilder defaultBuilder =
+ VertexInfo.builder()
+ .baseUri("test")
+ .type("test")
+ .chunkSize(24)
+ .version("gar/v1")
+ .propertyGroups(new PropertyGroups(List.of(TestUtil.pg3)));
+
+ @Test
+ public void testVertexInfoBasicBuilder() {
+ VertexInfo v = defaultBuilder.build();
+ }
+
+ @Test
+ public void testVertexInfoBuilderDoubleDeclaration() throws
URISyntaxException {
+ VertexInfo.VertexInfoBuilder doubleDefinitionBuilder =
+ defaultBuilder.baseUri(new URI("world"));
+ VertexInfo v = doubleDefinitionBuilder.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.VertexInfoBuilder propertyGroupAppendBuilder =
+ defaultBuilder.addPropertyGroup(TestUtil.pg2);
+ VertexInfo v = propertyGroupAppendBuilder.build();
+
+ Assert.assertEquals(2, v.getPropertyGroups().size());
+ }
+
+ @Test
+ public void propertyGroupAddOnlyTest() {
+ VertexInfo.VertexInfoBuilder propertyGroupAddOnlyBuilder =
+
defaultBuilder.propertyGroups(null).addPropertyGroup(TestUtil.pg2);
+ VertexInfo v = propertyGroupAddOnlyBuilder.build();
+
+ Assert.assertEquals(1, v.getPropertyGroups().size());
+ }
+
+ @Test
+ public void invalidChunkSizeTest() {
+ VertexInfo.VertexInfoBuilder invalidChunkSizeBuilder =
defaultBuilder.chunkSize(-1);
+ IllegalArgumentException illegalArgumentException =
+ Assert.assertThrows(IllegalArgumentException.class,
invalidChunkSizeBuilder::build);
+ Assert.assertEquals(
+ "Chunk size cannot be negative: -1",
illegalArgumentException.getMessage());
+ }
+
@Test
public void testBuildWithPrefix() {
try {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]