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

bossenti pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/streampipes.git


The following commit(s) were added to refs/heads/dev by this push:
     new 5e27cc3ac extend SDK by builder for nested properties (#2262)
5e27cc3ac is described below

commit 5e27cc3ac68aed48ae1a0c5233e0efe9db9832a1
Author: Tim <[email protected]>
AuthorDate: Mon Dec 4 09:10:47 2023 +0100

    extend SDK by builder for nested properties (#2262)
    
    * extend SDK by builder for nested properties
    
    * refactor: remove `clear()` method
---
 .../sdk/builder/NestedPropertyBuilder.java         |  94 +++++++++++++++++++
 .../sdk/builder/NestedPropertyBuilderTest.java     | 103 +++++++++++++++++++++
 2 files changed, 197 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..7ea97be90
--- /dev/null
+++ 
b/streampipes-sdk/src/main/java/org/apache/streampipes/sdk/builder/NestedPropertyBuilder.java
@@ -0,0 +1,94 @@
+/*
+ * 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;
+  }
+}
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..848acbe39
--- /dev/null
+++ 
b/streampipes-sdk/src/test/java/org/apache/streampipes/sdk/builder/NestedPropertyBuilderTest.java
@@ -0,0 +1,103 @@
+/*
+ * 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 createBuilderWithNullName() {
+    assertThrows(IllegalArgumentException.class, () -> 
NestedPropertyBuilder.create(null));
+  }
+
+  @Test
+  public void createBuilderWithEmptyName() {
+    assertThrows(IllegalArgumentException.class, () -> 
NestedPropertyBuilder.create(""));
+  }
+}

Reply via email to