Skip to content

Commit f69aff3

Browse files
committed
Make getCharacteristic() in Property return Optional<>
1 parent 878ef48 commit f69aff3

File tree

28 files changed

+211
-192
lines changed

28 files changed

+211
-192
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ build
144144
/core/sds-aspect-meta-model-legacy*/src
145145
/core/sds-aspect-meta-model-legacy*/target
146146

147+
# Directories where classes are generated during unit tests
148+
core/sds-aspect-model-java-generator/src/test/java/io/openmanufacturing/test
149+
core/sds-aspect-model-jackson/src/main/java/io/openmanufacturing/test
150+
147151
# jqwik
148152
.jqwik-database
149153

core/sds-aspect-meta-model-java/src/main/java/io/openmanufacturing/sds/metamodel/Property.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
public interface Property extends Base, IsDescribed {
2525

2626
/**
27-
* @return the {@link Characteristic} describing this Property
27+
* @return the {@link Characteristic} describing this Property. This can be empty when the Property is abstract.
2828
*/
29-
Characteristic getCharacteristic();
29+
Optional<Characteristic> getCharacteristic();
3030

3131
/**
3232
* @return an {@link Optional} which may contain an example value for the Property. The type of the value is
@@ -72,28 +72,26 @@ default boolean isAbstract() {
7272
* Returns the Property's unconstrained Characteristic.
7373
* This is undefined when the Property is abstract
7474
*
75-
* @return The Property's Characteristic without Constraints, or null, if {@link #isAbstract()} is true
75+
* @return The Property's Characteristic without Constraints, or empty, if {@link #isAbstract()} is true
7676
*/
77-
default Characteristic getEffectiveCharacteristic() {
78-
Characteristic characteristic = getCharacteristic();
79-
if ( characteristic == null ) {
80-
return null;
81-
}
82-
while ( characteristic instanceof Trait ) {
83-
characteristic = ((Trait) characteristic).getBaseCharacteristic();
84-
}
85-
return characteristic;
77+
default Optional<Characteristic> getEffectiveCharacteristic() {
78+
return getCharacteristic().map( characteristic -> {
79+
while ( characteristic.is( Trait.class ) ) {
80+
characteristic = characteristic.as( Trait.class ).getBaseCharacteristic();
81+
}
82+
return characteristic;
83+
} );
8684
}
8785

8886
/**
8987
* @return the type for the Property.
9088
*/
9189
default Optional<Type> getDataType() {
92-
return Optional.ofNullable( getEffectiveCharacteristic() ).flatMap( Characteristic::getDataType );
90+
return getEffectiveCharacteristic().flatMap( Characteristic::getDataType );
9391
}
9492

9593
/**
96-
* @return the {@link AbstractProperty} that is extended by this Property, if present
94+
* @return the Property that is extended by this Property, if present
9795
*/
9896
default Optional<Property> getExtends() {
9997
return Optional.empty();

core/sds-aspect-meta-model-java/src/main/java/io/openmanufacturing/sds/metamodel/impl/DefaultProperty.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import io.openmanufacturing.sds.metamodel.visitor.AspectVisitor;
2424

2525
public class DefaultProperty extends BaseImpl implements Property {
26-
private final Characteristic characteristic;
26+
private final Optional<Characteristic> characteristic;
2727
private final Optional<ScalarValue> exampleValue;
2828
private final boolean optional;
2929
private final boolean notInPayload;
@@ -32,7 +32,7 @@ public class DefaultProperty extends BaseImpl implements Property {
3232
private final Optional<Property> extends_;
3333

3434
public DefaultProperty( final MetaModelBaseAttributes metaModelBaseAttributes,
35-
final Characteristic characteristic,
35+
final Optional<Characteristic> characteristic,
3636
final Optional<ScalarValue> exampleValue,
3737
final boolean optional,
3838
final boolean notInPayload,
@@ -55,7 +55,7 @@ public DefaultProperty( final MetaModelBaseAttributes metaModelBaseAttributes,
5555
* @return the characteristic.
5656
*/
5757
@Override
58-
public Characteristic getCharacteristic() {
58+
public Optional<Characteristic> getCharacteristic() {
5959
return characteristic;
6060
}
6161

core/sds-aspect-meta-model-java/src/main/java/io/openmanufacturing/sds/metamodel/loader/DefaultPropertyWrapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
public class DefaultPropertyWrapper extends DefaultProperty {
2626

27-
private Characteristic characteristic;
27+
private Optional<Characteristic> characteristic;
2828
private Optional<ScalarValue> exampleValue;
2929
private boolean optional;
3030
private boolean notInPayload;
@@ -33,7 +33,7 @@ public class DefaultPropertyWrapper extends DefaultProperty {
3333
private Optional<String> payloadName;
3434

3535
public DefaultPropertyWrapper( final MetaModelBaseAttributes metaModelBaseAttributes ) {
36-
super( metaModelBaseAttributes, new DefaultCharacteristic( metaModelBaseAttributes, Optional.empty() ),
36+
super( metaModelBaseAttributes, Optional.of( new DefaultCharacteristic( metaModelBaseAttributes, Optional.empty() ) ),
3737
Optional.empty(), false, false, Optional.empty(), false, Optional.empty() );
3838
}
3939

@@ -92,12 +92,12 @@ public void setExtends_( final Optional<Property> extends_ ) {
9292
}
9393

9494
@Override
95-
public Characteristic getCharacteristic() {
95+
public Optional<Characteristic> getCharacteristic() {
9696
return characteristic;
9797
}
9898

9999
public void setCharacteristic( final Characteristic characteristic ) {
100-
this.characteristic = characteristic;
100+
this.characteristic = Optional.of( characteristic );
101101
}
102102

103103
@SuppressWarnings( "squid:S1067" )

core/sds-aspect-meta-model-java/src/main/java/io/openmanufacturing/sds/metamodel/loader/instantiator/AspectInstantiator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package io.openmanufacturing.sds.metamodel.loader.instantiator;
1515

1616
import java.util.List;
17+
import java.util.Optional;
1718
import java.util.stream.Collectors;
1819

1920
import org.apache.jena.rdf.model.Resource;
@@ -45,6 +46,7 @@ public Aspect apply( final Resource aspect ) {
4546
.collect( Collectors.toList() );
4647
final boolean isCollectionAspect = properties.stream()
4748
.map( Property::getCharacteristic )
49+
.flatMap( Optional::stream )
4850
.filter( Collection.class::isInstance ).count() == 1;
4951
return new DefaultAspect( metaModelBaseAttributes, properties, operations, events, isCollectionAspect );
5052
}

core/sds-aspect-meta-model-java/src/main/java/io/openmanufacturing/sds/metamodel/visitor/AspectStreamTraversalVisitor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public Stream<Base> visitProperty( final Property property, final Void context )
6666
return Stream.<Base> of( property );
6767
}
6868
hasVisited.add( property );
69-
final Stream<Base> characteristicResult = property.getCharacteristic() == null ? Stream.empty() : property.getCharacteristic().accept( this, null );
69+
final Stream<Base> characteristicResult = property.getCharacteristic().stream().flatMap( characteristic ->
70+
characteristic.accept( this, null ) );
7071
final Stream<Base> extendsResult = property.getExtends().stream().flatMap( superProperty -> superProperty.accept( this, null ) );
7172
return Stream.of( Stream.<Base> of( property ), characteristicResult, extendsResult
7273
).flatMap( Function.identity() );

core/sds-aspect-meta-model-java/src/main/java/io/openmanufacturing/sds/metamodel/visitor/LanguageCollectorModelVisitor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public Set<Locale> visitAspect( final Aspect aspect, final Set<Locale> context )
6060
@Override
6161
public Set<Locale> visitProperty( final Property property, final Set<Locale> context ) {
6262
return Stream.of(
63-
visitIsDescribed( property, context ),
64-
property.getCharacteristic().accept( this, Collections.emptySet() ) )
65-
.reduce( context, Sets::union );
63+
Stream.of( visitIsDescribed( property, context ) ),
64+
property.getCharacteristic().stream().map( characteristic -> characteristic.accept( this, Collections.emptySet() ) ) )
65+
.reduce( Stream.empty(), Stream::concat ).reduce( context, Sets::union );
6666
}
6767

6868
@Override

core/sds-aspect-meta-model-java/src/test/java/io/openmanufacturing/sds/metamodel/loader/AspectMetaModelInstantiatorTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void testPropertyInstantiationExpectSuccess( final KnownVersion metaModel
7474
"This is a test property.", "http://example.com/me", "http://example.com/omp" );
7575
assertThat( property.getExampleValue() ).map( value -> value.as( ScalarValue.class ).getValue() ).contains( "Example Value" );
7676
assertThat( property.isOptional() ).isFalse();
77-
assertThat( property.getCharacteristic().getName() ).isEqualTo( "Text" );
77+
assertThat( property.getCharacteristic().get().getName() ).isEqualTo( "Text" );
7878
assertThat( property.getDataType() ).isInstanceOf( Optional.class );
7979
assertThat( property.getDataType().get() ).isInstanceOf( Scalar.class );
8080
}
@@ -96,7 +96,7 @@ public void testOptionalPropertyInstantiationExpectSuccess( final KnownVersion m
9696
assertThat( property.isOptional() ).isTrue();
9797
assertThat( property.getPayloadName() ).isEqualTo( "testProperty" );
9898
assertThat( property.isNotInPayload() ).isFalse();
99-
assertThat( property.getCharacteristic().getName() ).isEqualTo( "Text" );
99+
assertThat( property.getCharacteristic().get().getName() ).isEqualTo( "Text" );
100100
}
101101

102102
@ParameterizedTest
@@ -115,7 +115,7 @@ public void testNotInPayloadPropertyInstantiationExpectSuccess( final KnownVersi
115115
assertThat( property.getExampleValue() ).isEmpty();
116116
assertThat( property.isOptional() ).isFalse();
117117
assertThat( property.isNotInPayload() ).isTrue();
118-
assertThat( property.getCharacteristic().getName() ).isEqualTo( "Text" );
118+
assertThat( property.getCharacteristic().get().getName() ).isEqualTo( "Text" );
119119
}
120120

121121
@ParameterizedTest
@@ -135,7 +135,7 @@ public void testPropertyWithPayloadNameInstantiationExpectSuccess( final KnownVe
135135
assertThat( property.isOptional() ).isFalse();
136136
assertThat( property.getPayloadName() ).isEqualTo( "test" );
137137
assertThat( property.isNotInPayload() ).isFalse();
138-
assertThat( property.getCharacteristic().getName() ).isEqualTo( "Text" );
138+
assertThat( property.getCharacteristic().get().getName() ).isEqualTo( "Text" );
139139
}
140140

141141
@ParameterizedTest
@@ -146,7 +146,7 @@ public void testEitherCharacteristicInstantiationExpectSuccess( final KnownVersi
146146

147147
assertThat( aspect.getProperties() ).hasSize( 1 );
148148

149-
final Either either = (Either) aspect.getProperties().get( 0 ).getCharacteristic();
149+
final Either either = (Either) aspect.getProperties().get( 0 ).getCharacteristic().get();
150150

151151
assertBaseAttributes( either, expectedAspectModelUrn, "TestEither",
152152
"Test Either", "This is a test Either.", "http://example.com/omp" );
@@ -164,7 +164,7 @@ public void testSingleEntityCharacteristicInstantiationExpectSuccess( final Know
164164

165165
assertThat( aspect.getProperties() ).hasSize( 1 );
166166

167-
final SingleEntity singleEntity = (SingleEntity) aspect.getProperties().get( 0 ).getCharacteristic();
167+
final SingleEntity singleEntity = (SingleEntity) aspect.getProperties().get( 0 ).getCharacteristic().get();
168168

169169
assertBaseAttributes( singleEntity, expectedAspectModelUrn, "EntityCharacteristic",
170170
"Test Entity Characteristic", "This is a test Entity Characteristic", "http://example.com/omp" );
@@ -180,7 +180,7 @@ public void testAbstractEntityInstantiationExpectSuccess( final KnownVersion met
180180

181181
assertThat( aspect.getProperties() ).hasSize( 1 );
182182

183-
final Entity entity = (Entity) aspect.getProperties().get( 0 ).getCharacteristic().getDataType().get();
183+
final Entity entity = (Entity) aspect.getProperties().get( 0 ).getCharacteristic().get().getDataType().get();
184184
final AbstractEntity abstractEntity = (AbstractEntity) entity.getExtends().get();
185185
assertBaseAttributes( abstractEntity, expectedAspectModelUrn, "AbstractTestEntity",
186186
"Abstract Test Entity", "This is a abstract test entity" );
@@ -197,7 +197,7 @@ public void testCollectionWithAbstractEntityInstantiationExpectSuccess( final Kn
197197

198198
assertThat( aspect.getProperties() ).hasSize( 1 );
199199

200-
final AbstractEntity abstractEntity = (AbstractEntity) aspect.getProperties().get( 0 ).getCharacteristic().getDataType().get();
200+
final AbstractEntity abstractEntity = (AbstractEntity) aspect.getProperties().get( 0 ).getCharacteristic().get().getDataType().get();
201201
assertThat( abstractEntity.getExtends() ).isEmpty();
202202
assertBaseAttributes( abstractEntity, expectedAspectModelUrn, "AbstractTestEntity", "AbstractTestEntity", null );
203203
final List<ComplexType> extendingElements = abstractEntity.getExtendingElements();
@@ -212,7 +212,7 @@ public void testCodeCharacteristicInstantiationExpectSuccess( final KnownVersion
212212

213213
assertThat( aspect.getProperties() ).hasSize( 1 );
214214

215-
final Code code = (Code) aspect.getProperties().get( 0 ).getCharacteristic();
215+
final Code code = (Code) aspect.getProperties().get( 0 ).getCharacteristic().get();
216216

217217
assertBaseAttributes( code, expectedAspectModelUrn, "TestCode",
218218
"Test Code", "This is a test code.", "http://example.com/omp" );
@@ -270,8 +270,8 @@ public void testAspectWithRecursivePropertyWithOptional( final KnownVersion meta
270270
final Aspect aspect = loadAspect( TestAspect.ASPECT_WITH_RECURSIVE_PROPERTY_WITH_OPTIONAL, metaModelVersion );
271271
assertThat( aspect.getProperties().size() ).isEqualTo( 1 );
272272
final Property firstProperty = aspect.getProperties().get( 0 );
273-
final Property secondProperty = ((DefaultEntity) firstProperty.getCharacteristic().getDataType().get()).getProperties().get( 0 );
274-
final Property thirdProperty = ((DefaultEntity) secondProperty.getCharacteristic().getDataType().get()).getProperties().get( 0 );
273+
final Property secondProperty = ((DefaultEntity) firstProperty.getCharacteristic().get().getDataType().get()).getProperties().get( 0 );
274+
final Property thirdProperty = ((DefaultEntity) secondProperty.getCharacteristic().get().getDataType().get()).getProperties().get( 0 );
275275
assertThat( firstProperty ).isNotEqualTo( secondProperty );
276276
assertThat( secondProperty ).isEqualTo( thirdProperty );
277277
assertThat( firstProperty.getCharacteristic() ).isEqualTo( secondProperty.getCharacteristic() );

core/sds-aspect-meta-model-java/src/test/java/io/openmanufacturing/sds/metamodel/loader/CollectionInstantiatorTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void testCollectionCharacteristicInstantiationExpectSuccess( final KnownV
4545

4646
assertThat( aspect.getProperties() ).hasSize( 1 );
4747

48-
final Collection collection = (Collection) aspect.getProperties().get( 0 ).getCharacteristic();
48+
final Collection collection = (Collection) aspect.getProperties().get( 0 ).getCharacteristic().get();
4949

5050
assertBaseAttributes( collection, expectedAspectModelUrn, "TestCollection",
5151
"Test Collection", "This is a test collection.", "http://example.com/omp" );
@@ -66,7 +66,7 @@ public void testCollectionCharacteristicWithEntityDataTypeExpectSuccess( final K
6666

6767
assertThat( aspect.getProperties() ).hasSize( 1 );
6868

69-
final Collection collection = (Collection) aspect.getProperties().get( 0 ).getCharacteristic();
69+
final Collection collection = (Collection) aspect.getProperties().get( 0 ).getCharacteristic().get();
7070

7171
assertBaseAttributes( collection, expectedAspectModelUrn, "TestCollection",
7272
"Test Collection", "This is a test collection.", "http://example.com/omp" );
@@ -85,7 +85,7 @@ public void testListCharacteristicInstantiationExpectSuccess( final KnownVersion
8585

8686
assertThat( aspect.getProperties() ).hasSize( 1 );
8787

88-
final List list = (List) aspect.getProperties().get( 0 ).getCharacteristic();
88+
final List list = (List) aspect.getProperties().get( 0 ).getCharacteristic().get();
8989

9090
assertBaseAttributes( list, expectedAspectModelUrn, "TestList",
9191
"Test List", "This is a test list.", "http://example.com/omp" );
@@ -105,7 +105,7 @@ public void testSetCharacteristicInstantiationExpectSuccess( final KnownVersion
105105

106106
assertThat( aspect.getProperties() ).hasSize( 1 );
107107

108-
final Set set = (Set) aspect.getProperties().get( 0 ).getCharacteristic();
108+
final Set set = (Set) aspect.getProperties().get( 0 ).getCharacteristic().get();
109109

110110
assertBaseAttributes( set, expectedAspectModelUrn, "TestSet",
111111
"Test Set", "This is a test set.", "http://example.com/omp" );
@@ -125,7 +125,7 @@ public void testSortedSetCharacteristicInstantiationExpectSuccess( final KnownVe
125125

126126
assertThat( aspect.getProperties() ).hasSize( 1 );
127127

128-
final SortedSet sortedSet = (SortedSet) aspect.getProperties().get( 0 ).getCharacteristic();
128+
final SortedSet sortedSet = (SortedSet) aspect.getProperties().get( 0 ).getCharacteristic().get();
129129

130130
assertBaseAttributes( sortedSet, expectedAspectModelUrn, "TestSortedSet",
131131
"Test Sorted Set", "This is a test sorted set.", "http://example.com/omp" );
@@ -145,7 +145,7 @@ public void testTimeSeriesInstantiationExpectSuccess( final KnownVersion metaMod
145145

146146
assertThat( aspect.getProperties() ).hasSize( 1 );
147147

148-
final TimeSeries timeSeries = (TimeSeries) aspect.getProperties().get( 0 ).getCharacteristic();
148+
final TimeSeries timeSeries = (TimeSeries) aspect.getProperties().get( 0 ).getCharacteristic().get();
149149

150150
assertBaseAttributes( timeSeries, expectedAspectModelUrn, "TestTimeSeries",
151151
"Test Time Series", "This is a test time series.", "http://example.com/omp" );
@@ -163,7 +163,7 @@ public void testCollectionInstantiationWithElementCharacteristicExpectSuccess( f
163163
final Aspect aspect = loadAspect( TestAspect.ASPECT_WITH_COLLECTION_WITH_ELEMENT_CHARACTERISTIC, metaModelVersion );
164164
assertThat( aspect.getProperties() ).hasSize( 1 );
165165

166-
final Collection collection = (Collection) aspect.getProperties().get( 0 ).getCharacteristic();
166+
final Collection collection = (Collection) aspect.getProperties().get( 0 ).getCharacteristic().get();
167167
assertBaseAttributes( collection, expectedAspectModelUrn, "TestCollection",
168168
"Test Collection", "This is a test collection.", "http://example.com/omp" );
169169

@@ -185,7 +185,7 @@ public void testCollectionInstantiationWithElementConstraintExpectSuccess( final
185185
final Aspect aspect = loadAspect( TestAspect.ASPECT_WITH_COLLECTION_WITH_ELEMENT_CONSTRAINT, metaModelVersion );
186186
assertThat( aspect.getProperties() ).hasSize( 1 );
187187

188-
final Collection collection = (Collection) aspect.getProperties().get( 0 ).getCharacteristic();
188+
final Collection collection = (Collection) aspect.getProperties().get( 0 ).getCharacteristic().get();
189189
assertBaseAttributes( collection, expectedAspectModelUrn, "TestCollection",
190190
"Test Collection", "This is a test collection.", "http://example.com/omp" );
191191

core/sds-aspect-meta-model-java/src/test/java/io/openmanufacturing/sds/metamodel/loader/ConstraintInstantiatorTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void testLanguageConstraintInstantiationExpectSuccess( final KnownVersion
4141

4242
assertThat( aspect.getProperties() ).hasSize( 1 );
4343

44-
final Trait trait = (Trait) aspect.getProperties().get( 0 ).getCharacteristic();
44+
final Trait trait = (Trait) aspect.getProperties().get( 0 ).getCharacteristic().get();
4545
final LanguageConstraint languageConstraint = (LanguageConstraint) trait.getConstraints().get( 0 );
4646

4747
assertBaseAttributes( languageConstraint,
@@ -59,7 +59,7 @@ public void testEncodingConstraintInstantiationExpectSuccess( final KnownVersion
5959

6060
assertThat( aspect.getProperties() ).hasSize( 1 );
6161

62-
final Trait trait = (Trait) aspect.getProperties().get( 0 ).getCharacteristic();
62+
final Trait trait = (Trait) aspect.getProperties().get( 0 ).getCharacteristic().get();
6363
final EncodingConstraint encodingConstraint = (EncodingConstraint) trait.getConstraints().get( 0 );
6464

6565
assertBaseAttributes( encodingConstraint,
@@ -77,7 +77,7 @@ public void testLengthConstraintInstantiationExpectSuccess( final KnownVersion m
7777

7878
assertThat( aspect.getProperties() ).hasSize( 1 );
7979

80-
final Trait trait = (Trait) aspect.getProperties().get( 0 ).getCharacteristic();
80+
final Trait trait = (Trait) aspect.getProperties().get( 0 ).getCharacteristic().get();
8181
final LengthConstraint lengthConstraint = (LengthConstraint) trait.getConstraints().get( 0 );
8282

8383
assertBaseAttributes( lengthConstraint,
@@ -98,7 +98,7 @@ public void testRegularExpressionConstraintInstantiationExpectSuccess( final Kno
9898

9999
assertThat( aspect.getProperties() ).hasSize( 1 );
100100

101-
final Trait trait = (Trait) aspect.getProperties().get( 0 ).getCharacteristic();
101+
final Trait trait = (Trait) aspect.getProperties().get( 0 ).getCharacteristic().get();
102102
final RegularExpressionConstraint regularExpressionConstraint = (RegularExpressionConstraint) trait
103103
.getConstraints().get( 0 );
104104

0 commit comments

Comments
 (0)