This is an automated email from the ASF dual-hosted git repository.

bossenti pushed a commit to branch sdk-add-builder-for-nested-property
in repository https://gitbox.apache.org/repos/asf/streampipes.git

commit 95765b193540f0227fef91b80f32ee330a1f2de2
Author: bossenti <[email protected]>
AuthorDate: Fri Dec 1 16:58:13 2023 +0100

    extend SDK by builder for nested properties
---
 .../sdk/builder/NestedPropertyBuilder.java         | 104 ++++++++++++++++
 .../sdk/builder/NestedPropertyBuilderTest.java     | 136 +++++++++++++++++++++
 2 files changed, 240 insertions(+)

diff --git 
a/streampipes-sdk/src/main/java/org/apache/streampipes/sdk/builder/NestedPropertyBuilder.java
 
b/streampipes-sdk/src/main/java/org/apache/streampipes/sdk/builder/NestedPropertyBuilder.java
new file mode 100644
index 000000000..f9038412e
--- /dev/null
+++ 
b/streampipes-sdk/src/main/java/org/apache/streampipes/sdk/builder/NestedPropertyBuilder.java
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.streampipes.sdk.builder;
+
+import org.apache.streampipes.model.schema.EventProperty;
+import org.apache.streampipes.model.schema.EventPropertyNested;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Builder class for creating instances of {@link EventPropertyNested}.
+ */
+public class NestedPropertyBuilder {
+
+  /**
+   * The {@link EventPropertyNested} being constructed by this builder.
+   */
+  private final EventPropertyNested eventPropertyNested;
+
+  /**
+   * List of {@link EventProperty} instances contained within the {@link 
EventPropertyNested}.
+   */
+  private final List<EventProperty> containedEventProperties;
+
+  private NestedPropertyBuilder(String propertyName) {
+    if (propertyName == null || propertyName.isEmpty()) {
+      throw new IllegalArgumentException("Property name cannot be null or 
empty");
+    }
+    this.eventPropertyNested = new EventPropertyNested(propertyName);
+    this.containedEventProperties = new ArrayList<>();
+  }
+
+  /**
+   * Static factory method to create a new instance of the builder.
+   *
+   * @param propertyName The name of the property.
+   * @return A new instance of {@code NestedPropertyBuilder}.
+   */
+  public static NestedPropertyBuilder create(String propertyName) {
+    return new NestedPropertyBuilder(propertyName);
+  }
+
+  /**
+   * Adds a single {@link EventProperty} to the list of nested properties.
+   *
+   * @param eventProperty The {@link EventProperty} to add.
+   * @return This builder instance for method chaining.
+   */
+  public NestedPropertyBuilder withEventProperty(EventProperty eventProperty) {
+    this.containedEventProperties.add(eventProperty);
+    return this;
+  }
+
+  /**
+   * Adds multiple {@link EventProperty} instances to the list of nested 
properties.
+   *
+   * @param eventProperties The array of {@link EventProperty} instances to 
add.
+   * @return This builder instance for method chaining.
+   */
+  public NestedPropertyBuilder withEventProperties(EventProperty... 
eventProperties) {
+    this.containedEventProperties.addAll(List.of(eventProperties));
+    return this;
+  }
+
+  /**
+   * Builds and returns the final {@link EventPropertyNested} instance.
+   * Note: This method creates a new instance to ensure immutability.
+   *
+   * @return The constructed {@link EventPropertyNested}.
+   */
+  public EventPropertyNested build() {
+    // Create a new instance to make the class immutable
+    EventPropertyNested built = new 
EventPropertyNested(this.eventPropertyNested.getRuntimeName());
+    built.setEventProperties(containedEventProperties);
+    return built;
+  }
+
+  /**
+   * Resets the builder state, allowing it to be reused for creating another 
instance.
+   *
+   * @return A new instance of {@code NestedPropertyBuilder}.
+   */
+  public NestedPropertyBuilder clear() {
+    // Reset the builder state
+    return new NestedPropertyBuilder(eventPropertyNested.getRuntimeName());
+  }
+}
diff --git 
a/streampipes-sdk/src/test/java/org/apache/streampipes/sdk/builder/NestedPropertyBuilderTest.java
 
b/streampipes-sdk/src/test/java/org/apache/streampipes/sdk/builder/NestedPropertyBuilderTest.java
new file mode 100644
index 000000000..b78afc6d7
--- /dev/null
+++ 
b/streampipes-sdk/src/test/java/org/apache/streampipes/sdk/builder/NestedPropertyBuilderTest.java
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.streampipes.sdk.builder;
+
+import org.apache.streampipes.model.schema.EventPropertyNested;
+import org.apache.streampipes.sdk.utils.Datatypes;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+
+public class NestedPropertyBuilderTest {
+
+  @Test
+  public void buildNestedProperty() {
+    EventPropertyNested result = NestedPropertyBuilder.create("TestProperty")
+                                                      
.withEventProperty(PrimitivePropertyBuilder.create(
+                                                                               
                      Datatypes.String,
+                                                                               
                      "SubProperty1"
+                                                                               
                  )
+                                                                               
                  .build())
+                                                      
.withEventProperty(PrimitivePropertyBuilder.create(
+                                                                               
                      Datatypes.String,
+                                                                               
                      "SubProperty2"
+                                                                               
                  )
+                                                                               
                  .build())
+                                                      .build();
+
+    assertNotNull(result);
+    assertEquals("TestProperty", result.getRuntimeName());
+    assertEquals(
+        2,
+        result.getEventProperties()
+              .size()
+    );
+  }
+
+  @Test
+  public void buildNestedPropertyWithoutProperties() {
+    EventPropertyNested result = NestedPropertyBuilder.create("TestProperty")
+                                                      .build();
+
+    assertNotNull(result);
+    assertEquals("TestProperty", result.getRuntimeName());
+    assertTrue(result.getEventProperties()
+                     .isEmpty());
+  }
+
+  @Test
+  public void buildNestedPropertyWithArray() {
+    EventPropertyNested result = NestedPropertyBuilder.create("TestProperty")
+                                                      .withEventProperties(
+                                                          
PrimitivePropertyBuilder.create(
+                                                                               
       Datatypes.String,
+                                                                               
       "SubProperty1"
+                                                                               
   )
+                                                                               
   .build(),
+                                                          
PrimitivePropertyBuilder.create(
+                                                                               
       Datatypes.String,
+                                                                               
       "SubProperty2"
+                                                                               
   )
+                                                                               
   .build()
+                                                      )
+                                                      .build();
+
+    assertNotNull(result);
+    assertEquals("TestProperty", result.getRuntimeName());
+    assertEquals(
+        2,
+        result.getEventProperties()
+              .size()
+    );
+  }
+
+  @Test
+  public void clearBuilder() {
+    EventPropertyNested result1 = NestedPropertyBuilder.create("TestProperty")
+                                                       
.withEventProperty(PrimitivePropertyBuilder.create(
+                                                                               
                       Datatypes.String,
+                                                                               
                       "SubProperty1"
+                                                                               
                   )
+                                                                               
                   .build())
+                                                       .clear()
+                                                       .build();
+
+    assertNotNull(result1);
+    assertEquals("TestProperty", result1.getRuntimeName());
+    assertTrue(result1.getEventProperties()
+                      .isEmpty());
+
+    EventPropertyNested result2 = 
NestedPropertyBuilder.create("NewTestProperty")
+                                                       
.withEventProperty(PrimitivePropertyBuilder.create(
+                                                                               
                       Datatypes.String,
+                                                                               
                       "SubProperty3"
+                                                                               
                   )
+                                                                               
                   .build())
+                                                       .build();
+
+    assertNotNull(result2);
+    assertEquals("NewTestProperty", result2.getRuntimeName());
+    assertEquals(
+        1,
+        result2.getEventProperties()
+               .size()
+    );
+  }
+
+  @Test
+  public void createBuilderWithNullName() {
+    assertThrows(IllegalArgumentException.class, () -> 
NestedPropertyBuilder.create(null));
+  }
+
+  @Test
+  public void createBuilderWithEmptyName() {
+    assertThrows(IllegalArgumentException.class, () -> 
NestedPropertyBuilder.create(""));
+  }
+}

Reply via email to