-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extend SDK by builder for nested properties (#2262)
* extend SDK by builder for nested properties * refactor: remove `clear()` method
- Loading branch information
Showing
2 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
streampipes-sdk/src/main/java/org/apache/streampipes/sdk/builder/NestedPropertyBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
...pipes-sdk/src/test/java/org/apache/streampipes/sdk/builder/NestedPropertyBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("")); | ||
} | ||
} |