Skip to content

Commit 05e9d84

Browse files
committed
Improve inefficient code
1 parent 241d4f4 commit 05e9d84

File tree

5 files changed

+44
-17
lines changed

5 files changed

+44
-17
lines changed

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/loader/AspectModelLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ private void setNamespaces( final Collection<AspectModelFile> files, final Colle
525525
final ModelElementFactory modelElementFactory = new ModelElementFactory( model, Map.of(), r -> null );
526526
final Resource namespaceResource = model.listStatements( null, RDF.type, SammNs.SAMM.Namespace() )
527527
.mapWith( Statement::getSubject )
528-
.toList().iterator().next();
528+
.toList().getFirst();
529529
namespaceDefinition = modelElementFactory.createBaseAttributes( namespaceResource );
530530
fileContainingNamespaceDefinition = elementFile;
531531
break;

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/versionupdate/BammUriRewriter.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,17 @@ public Model migrate( final Model sourceModel ) {
125125
}
126126

127127
public enum BammVersion {
128-
BAMM_1_0_0,
129-
BAMM_2_0_0;
128+
BAMM_1_0_0( "1.0.0" ),
129+
BAMM_2_0_0( "2.0.0" );
130+
131+
private final String versionString;
132+
133+
BammVersion( final String versionString ) {
134+
this.versionString = versionString;
135+
}
130136

131137
public String versionString() {
132-
return toString().replaceFirst( "BAMM_(\\d+)_(\\d+)_(\\d+)", "$1.$2.$3" );
138+
return versionString;
133139
}
134140
}
135141
}

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/versionupdate/SammMetaModelVersionUriRewriter.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.eclipse.esmf.samm.KnownVersion;
2020

2121
public class SammMetaModelVersionUriRewriter extends AbstractUriRewriter {
22-
2322
public SammMetaModelVersionUriRewriter( final KnownVersion sourceVersion, final KnownVersion targetVersion ) {
2423
super( sourceVersion, targetVersion );
2524
}
@@ -31,15 +30,15 @@ public int order() {
3130

3231
@Override
3332
protected Optional<String> rewriteUri( final String oldUri, final Map<String, String> oldToNewNamespaces ) {
34-
final String[] uriParts = oldUri.split( "#" );
35-
if ( uriParts.length == 1 ) {
33+
final int indexOfHash = oldUri.indexOf( '#' );
34+
if ( indexOfHash == -1 ) {
35+
return Optional.empty();
36+
}
37+
final String newNamespace = oldToNewNamespaces.get( oldUri.substring( 0, indexOfHash + 1 ) );
38+
if ( newNamespace == null ) {
3639
return Optional.empty();
3740
}
38-
return oldToNewNamespaces.keySet()
39-
.stream()
40-
.filter( key -> key.equals( uriParts[0] + "#" ) )
41-
.findAny()
42-
.map( key -> oldToNewNamespaces.get( key ) + uriParts[1] );
41+
return Optional.of( newNamespace + oldUri.substring( indexOfHash + 1 ) );
4342
}
4443

4544
@Override

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/metamodel/impl/DefaultScalarValue.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ public class DefaultScalarValue implements ScalarValue {
3131

3232
private final MetaModelBaseAttributes metaModelBaseAttributes;
3333

34+
/**
35+
* Constructor for non-described/anonymous scalar values
36+
*
37+
* @param value the value
38+
* @param type the type
39+
*/
40+
public DefaultScalarValue( final Object value, final Scalar type ) {
41+
this( null, value, type );
42+
}
43+
44+
/**
45+
* Standard constructor for scalar values that provides context information such as the value's descriptions
46+
*
47+
* @param metaModelBaseAttributes the base attributes
48+
* @param value the value
49+
* @param type the type
50+
*/
3451
public DefaultScalarValue( final MetaModelBaseAttributes metaModelBaseAttributes, final Object value, final Scalar type ) {
3552
this.metaModelBaseAttributes = metaModelBaseAttributes;
3653
this.value = value;
@@ -49,22 +66,22 @@ public Scalar getType() {
4966

5067
@Override
5168
public List<String> getSee() {
52-
return metaModelBaseAttributes.getSee();
69+
return metaModelBaseAttributes == null ? List.of() : metaModelBaseAttributes.getSee();
5370
}
5471

5572
@Override
5673
public Set<LangString> getPreferredNames() {
57-
return metaModelBaseAttributes.getPreferredNames();
74+
return metaModelBaseAttributes == null ? Set.of() : metaModelBaseAttributes.getPreferredNames();
5875
}
5976

6077
@Override
6178
public Set<LangString> getDescriptions() {
62-
return metaModelBaseAttributes.getDescriptions();
79+
return metaModelBaseAttributes == null ? Set.of() : metaModelBaseAttributes.getDescriptions();
6380
}
6481

6582
@Override
6683
public AspectModelFile getSourceFile() {
67-
return metaModelBaseAttributes.getSourceFile();
84+
return metaModelBaseAttributes == null ? null : metaModelBaseAttributes.getSourceFile();
6885
}
6986

7087
@Override

core/esmf-aspect-model-urn/src/main/java/org/eclipse/esmf/aspectmodel/urn/AspectModelUrn.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class AspectModelUrn implements Comparable<AspectModelUrn> {
7373
private final String namespaceMainPart;
7474
private final ElementType elementType;
7575
private final boolean isSammUrn;
76+
private final String namespaceIdentifier;
7677

7778
@JsonValue
7879
private final URI urn;
@@ -85,6 +86,10 @@ private AspectModelUrn( final URI urn, final String name, final String namespace
8586
this.elementType = elementType;
8687
this.version = version;
8788
this.isSammUrn = isSammUrn;
89+
final String urnString = urn.toString();
90+
namespaceIdentifier = urnString.contains( "#" )
91+
? urnString.substring( 0, urnString.indexOf( '#' ) )
92+
: urnString;
8893
}
8994

9095
/**
@@ -357,7 +362,7 @@ public String getNamespaceMainPart() {
357362
* @return the prefix part of the URN
358363
*/
359364
public String getNamespaceIdentifier() {
360-
return urn.toString().split( "#" )[0];
365+
return namespaceIdentifier;
361366
}
362367

363368
/**

0 commit comments

Comments
 (0)