Skip to content

Commit

Permalink
eclipse-ditto#60: Extended static factory method of FeatureDefinition…
Browse files Browse the repository at this point in the history
… to accept more than one identifier.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch-si.com>
  • Loading branch information
Juergen Fickel committed Jan 29, 2018
1 parent e3c6631 commit 6f53e41
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
package org.eclipse.ditto.model.things;

import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;

import java.util.stream.Stream;

import javax.annotation.concurrent.Immutable;
Expand All @@ -30,12 +32,17 @@ public interface FeatureDefinition extends Iterable<FeatureDefinition.Identifier
* containing that Identifier.
*
* @param identifier CharSequence-representation of the first Identifier of the returned FeatureDefinition.
* @param furtherIdentifiers optional further Identifiers of the returned FeatureDefinition.
* @return the instance.
* @throws NullPointerException if {@code identifier} is {@code null}.
* @throws FeatureDefinitionIdentifierInvalidException if {@code identifier} is invalid.
* @throws NullPointerException if any argument is {@code null}.
* @throws FeatureDefinitionIdentifierInvalidException if any argument is invalid.
*/
static FeatureDefinition fromIdentifier(final CharSequence identifier) {
return ThingsModelFactory.newFeatureDefinitionBuilder(identifier).build();
static FeatureDefinition fromIdentifier(final CharSequence identifier, final CharSequence ... furtherIdentifiers) {
final FeatureDefinitionBuilder builder = ThingsModelFactory.newFeatureDefinitionBuilder(identifier);
for (final CharSequence furtherIdentifier : checkNotNull(furtherIdentifiers, "further identifiers")) {
builder.add(furtherIdentifier);
}
return builder.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,20 +254,20 @@ default Thing removeAttribute(final CharSequence attributePath) {
Thing removeAttribute(JsonPointer attributePath);

/**
* Sets the given Definition of a Feature on a copy of this Thing.
* Sets the given definition of a Feature on a copy of this Thing.
*
* @param featureId the ID of the Feature.
* @param definition the Definition to be set.
* @return a copy of this Thing with the Feature containing the given Definition.
* @param definition the definition to be set.
* @return a copy of this Thing with the Feature containing the given definition.
* @throws NullPointerException if {@code featureId} is {@code null}.
*/
Thing setFeatureDefinition(String featureId, FeatureDefinition definition);

/**
* Removes the Definition from the Feature of this thing with the specified feature ID.
* Removes the definition from the Feature of this thing with the specified feature ID.
*
* @param featureId the identifier of the Feature to delete the Definition from.
* @return a copy of this Thing with the Feature without Definition.
* @param featureId the identifier of the Feature to delete the definition from.
* @return a copy of this Thing with the Feature without definition.
* @throws NullPointerException if {@code featureId} is {@code null}.
*/
Thing removeFeatureDefinition(String featureId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ FromScratch setPermissions(AuthorizationSubject authorizationSubject, Permission
* same ID is replaced.
*
* @param featureId the ID of the Feature to be set.
* @param featureDefinition the Definition of the Feature to be set.
* @param featureDefinition the definition of the Feature to be set.
* @param featureProperties the properties of the Feature to be set.
* @return this builder to allow method chaining.
* @throws NullPointerException if {@code featureId} is {@code null}.
Expand Down Expand Up @@ -272,20 +272,20 @@ FromScratch setFeature(String featureId, FeatureDefinition featureDefinition,
FromScratch removeFeature(String featureId);

/**
* Sets the given Definition to the Feature with the given ID on this builder. If this builder does not yet
* Sets the given definition to the Feature with the given ID on this builder. If this builder does not yet
* know a Feature with the given ID it creates one.
*
* @param featureId the ID of the Feature to be set.
* @param featureDefinition the Definition of the Feature to be set.
* @param featureDefinition the definition of the Feature to be set.
* @return this builder to allow method chaining.
* @throws NullPointerException if any argument is {@code null}.
*/
FromScratch setFeatureDefinition(String featureId, FeatureDefinition featureDefinition);

/**
* Removes the Definition from Feature with the given identifier on this builder.
* Removes the definition from Feature with the given identifier on this builder.
*
* @param featureId the ID of the Feature from which the Definition is to be deleted.
* @param featureId the ID of the Feature from which the definition is to be deleted.
* @return this builder to allow method chaining.
* @throws NullPointerException if {@code featureId} is {@code null}.
*/
Expand Down Expand Up @@ -916,7 +916,7 @@ FromCopy setFeature(Predicate<Features> existingFeaturesPredicate, String featur
* same ID is replaced.
*
* @param featureId the ID of the Feature to be set.
* @param featureDefinition the Definition of the Feature to be set.
* @param featureDefinition the definition of the Feature to be set.
* @param featureProperties the properties of the Feature to be set.
* @return this builder to allow method chaining.
* @throws NullPointerException if {@code featureId} is {@code null}.
Expand All @@ -934,19 +934,21 @@ default FromCopy setFeature(final String featureId, final FeatureDefinition feat
* @param existingFeaturesPredicate a predicate to decide whether the given features are set. The predicate
* receives the currently set features.
* @param featureId the ID of the Feature to be set.
* @param featureDefinition the Definition of the Feature to be set.
* @param featureDefinition the definition of the Feature to be set.
* @param featureProperties the properties of the Feature to be set.
* @return this builder to allow method chaining.
* @throws NullPointerException if any argument is {@code null}.
*/
FromCopy setFeature(Predicate<Features> existingFeaturesPredicate, String featureId,
FeatureDefinition featureDefinition, FeatureProperties featureProperties);
FromCopy setFeature(Predicate<Features> existingFeaturesPredicate,
String featureId,
FeatureDefinition featureDefinition,
FeatureProperties featureProperties);

/**
* Sets the given Definition to the Feature with the given ID on this builder.
* Sets the given definition to the Feature with the given ID on this builder.
*
* @param featureId the ID of the Feature to be set.
* @param featureDefinition the Definition of the Feature to be set.
* @param featureDefinition the definition of the Feature to be set.
* @return this builder to allow method chaining.
* @throws NullPointerException if any argument is {@code null}.
*/
Expand All @@ -955,22 +957,22 @@ default FromCopy setFeatureDefinition(final String featureId, final FeatureDefin
}

/**
* Sets the given Definition to the Feature with the given ID on this builder.
* Sets the given definition to the Feature with the given ID on this builder.
*
* @param existingFeaturesPredicate a predicate to decide whether the given Definition is set. The predicate
* @param existingFeaturesPredicate a predicate to decide whether the given definition is set. The predicate
* receives the currently set features.
* @param featureId the ID of the Feature to be set.
* @param featureDefinition the Definition of the Feature to be set.
* @param featureDefinition the definition of the Feature to be set.
* @return this builder to allow method chaining.
* @throws NullPointerException if any argument is {@code null}.
*/
FromCopy setFeatureDefinition(Predicate<Features> existingFeaturesPredicate, String featureId,
FeatureDefinition featureDefinition);

/**
* Removes the Definition from Feature with the given identifier on this builder.
* Removes the definition from Feature with the given identifier on this builder.
*
* @param featureId the ID of the Feature from which the Definition is to be deleted.
* @param featureId the ID of the Feature from which the definition is to be deleted.
* @return this builder to allow method chaining.
* @throws NullPointerException if {@code featureId} is {@code null}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,23 @@ public final class ImmutableFeatureFromCopyBuilderTest {

private ImmutableFeatureFromCopyBuilder underTest = null;


@Before
public void setUp() {
underTest = ImmutableFeatureFromCopyBuilder.of(TestConstants.Feature.FLUX_CAPACITOR);
}


@Test(expected = NullPointerException.class)
public void tryToCreateInstanceOfNullFeature() {
ImmutableFeatureFromCopyBuilder.of(null);
}


@Test
public void builderOfFeatureIsCorrectlyInitialised() {
final Feature feature = underTest.build();

assertThat(feature).isEqualTo(TestConstants.Feature.FLUX_CAPACITOR);
}


@Test
public void featurePropertiesNullGivesNullFeature() {
final Feature feature = underTest.properties((FeatureProperties) null).build();
Expand All @@ -61,7 +57,6 @@ public void featurePropertiesNullGivesNullFeature() {
.build());
}


@Test
public void jsonObjectNullGivesNullFeature() {
final Feature feature = underTest.properties((JsonObject) null).build();
Expand All @@ -72,19 +67,18 @@ public void jsonObjectNullGivesNullFeature() {
.build());
}


@Test
public void copyFeatureAndModifyProperties() {
final FeatureProperties featureProperties = FeatureProperties.newBuilder() //
.set(PROPERTY_PATH, PROPERTY_VALUE) //
.set("target_year_2", 2015) //
.set("target_year_3", 1885) //
final FeatureProperties featureProperties = FeatureProperties.newBuilder()
.set(PROPERTY_PATH, PROPERTY_VALUE)
.set("target_year_2", 2015)
.set("target_year_3", 1885)
.build();

final Feature expected = Feature.newBuilder() //
.properties(featureProperties) //
final Feature expected = Feature.newBuilder()
.properties(featureProperties)
.definition(TestConstants.Feature.FLUX_CAPACITOR_DEFINITION)
.withId(TestConstants.Feature.FLUX_CAPACITOR_ID) //
.withId(TestConstants.Feature.FLUX_CAPACITOR_ID)
.build();

final Feature actual = underTest.properties(props -> props.setValue(PROPERTY_PATH, PROPERTY_VALUE)).build();
Expand Down

0 comments on commit 6f53e41

Please sign in to comment.