Skip to content

Commit

Permalink
[cleanup] Typos & code quality fixes.
Browse files Browse the repository at this point in the history
Signed-off-by: Étienne Bausson <[email protected]>
  • Loading branch information
ebausson-obeo committed Nov 7, 2024
1 parent c9bbe6a commit 639c20e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void checkUseCaseTest() throws IOException {
* The file has been modified because a problem has been detected during the export phase.
* Those problem force us to use some full-length qualified name. This should be investigated.
*
* for exemple:
* for example:
include UseCaseTest::uc2;
* instead of
include uc2;
Expand Down Expand Up @@ -151,7 +151,7 @@ public void checkImportTest() throws IOException {
* The file has been modified because a problem has been detected during the export phase.
* Those problem force us to use some full-length qualified name. This should be investigated.
*
* for exemple:
* for example:
private import Pkg2::Pkg21::Pkg211::*::**;
* instead of
private import Pkg211::*::**;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@
*/
public class SysMLElementSerializer extends SysmlSwitch<String> {

private static final Predicate<Object> NOT_NULL = s -> s != null;

private final String lineSeparator;

private final String indentation;
Expand All @@ -152,7 +150,7 @@ public class SysMLElementSerializer extends SysmlSwitch<String> {
/**
* Simple constructor.
*
* @param newLine
* @param lineSeparator
* the string used to separate line
* @param indentation
* the string used to indent the file
Expand Down Expand Up @@ -680,10 +678,9 @@ public String caseFeatureChainExpression(FeatureChainExpression feature) {
.map(ParameterMembership.class::cast)
.findFirst()
.map(ParameterMembership::getOwnedMemberParameter)
.filter(NOT_NULL)
.map(Feature::getOwnedRelationship)
.map(Collection::stream)
.orElseGet(Stream::empty)
.stream()
.flatMap(Collection::stream)
.filter(FeatureValue.class::isInstance)
.map(FeatureValue.class::cast)
.findFirst()
Expand Down Expand Up @@ -768,7 +765,7 @@ public String caseActorMembership(ActorMembership actor) {
if (ownedActorParameter != null) {
VisibilityKind visibility = actor.getVisibility();
if (visibility != VisibilityKind.PUBLIC) {
builder.append(this.getVisivilityIndicator(visibility));
builder.append(this.getVisibilityIndicator(visibility));
}

this.appendDefaultUsage(builder, ownedActorParameter);
Expand All @@ -785,7 +782,7 @@ public String caseSubjectMembership(SubjectMembership subject) {
if (ownedSubjectParameter != null && !this.isImplicit(ownedSubjectParameter)) {
VisibilityKind visibility = subject.getVisibility();
if (visibility != VisibilityKind.PUBLIC) {
builder.append(this.getVisivilityIndicator(visibility));
builder.append(this.getVisibilityIndicator(visibility));
}
builder.appendWithSpaceIfNeeded("subject");

Expand All @@ -803,7 +800,7 @@ public String caseObjectiveMembership(ObjectiveMembership objective) {
if (ownedObjectiveRequirement != null && !this.isImplicit(ownedObjectiveRequirement)) {
VisibilityKind visibility = objective.getVisibility();
if (visibility != VisibilityKind.PUBLIC) {
builder.append(this.getVisivilityIndicator(visibility));
builder.append(this.getVisibilityIndicator(visibility));
}

builder.append("objective");
Expand Down Expand Up @@ -880,7 +877,7 @@ public String caseReturnParameterMembership(ReturnParameterMembership parameter)
if (ownedMemberParameter != null && !this.isImplicit(ownedMemberParameter) && ownedMemberParameter instanceof Usage usage) {
VisibilityKind visibility = parameter.getVisibility();
if (visibility != VisibilityKind.PUBLIC) {
builder.append(this.getVisivilityIndicator(visibility));
builder.append(this.getVisibilityIndicator(visibility));
}

builder.append("return");
Expand Down Expand Up @@ -990,8 +987,7 @@ private void appendSubsettings(Appender builder, List<Subsetting> subSettings, b
.filter(this::nameNotNullAndNotBlank)
.toList();
if (!subSettedDifference.isEmpty()) {
String subSettingPart = subSettedDifference.stream()
.collect(Collectors.joining(", "));
String subSettingPart = String.join(", ", subSettedDifference);
if (!subSettingPart.isBlank()) {
builder.appendSpaceIfNeeded().append(LabelConstants.SUBSETTING);
builder.appendSpaceIfNeeded().append(subSettingPart);
Expand All @@ -1007,8 +1003,7 @@ private void appendRedefinition(Appender builder, List<Redefinition> redefinitio
.filter(this::nameNotNullAndNotBlank)
.toList();
if (!redefinedFeatures.isEmpty()) {
String redefinitionPart = redefinedFeatures.stream()
.collect(Collectors.joining(", "));
String redefinitionPart = String.join(", ", redefinedFeatures);
if (!redefinitionPart.isBlank()) {
builder.appendSpaceIfNeeded().append(LabelConstants.REDEFINITION);
builder.appendSpaceIfNeeded().append(redefinitionPart);
Expand Down Expand Up @@ -1056,8 +1051,7 @@ private void appendFeatureTyping(Appender builder, EList<FeatureTyping> ownedTyp
.filter(this::nameNotNullAndNotBlank)
.toList();
if (!types.isEmpty()) {
String featureTypePart = types.stream()
.collect(Collectors.joining(", "));
String featureTypePart = String.join(", ", types);
if (!featureTypePart.isBlank()) {
builder.appendSpaceIfNeeded().append(LabelConstants.COLON);
builder.appendSpaceIfNeeded().append(featureTypePart);
Expand Down Expand Up @@ -1139,7 +1133,7 @@ private void appendValuePart(Appender builder, Usage usage) {
List<FeatureValue> ownedRelationship = usage.getOwnedRelationship().stream()
.filter(FeatureValue.class::isInstance)
.map(FeatureValue.class::cast)
.collect(Collectors.toList());
.toList();

for (FeatureValue feature : ownedRelationship) {
builder.appendSpaceIfNeeded();
Expand Down Expand Up @@ -1281,8 +1275,8 @@ private void appendSequenceExpressionListMember(Appender builder, List<Parameter
.map(FeatureValue.class::cast)
.toList();

for (int i = 0; i < featureValueList.size(); i++) {
Expression expression = featureValueList.get(i).getValue();
for (FeatureValue featureValue : featureValueList) {
Expression expression = featureValue.getValue();
this.appendSequenceExpressionList(builder, expression);
}
}
Expand Down Expand Up @@ -1341,14 +1335,14 @@ private void appendArgumentMember(Appender builder, ParameterMembership paramete

private void appendArgumentList(Appender builder, InvocationExpression expression) {
builder.append(LabelConstants.OPEN_PARENTHESIS);
Relationship relationship = expression.getOwnedRelationship().stream()
FeatureMembership featureMembership = expression.getOwnedRelationship().stream()
.filter(FeatureMembership.class::isInstance)
.map(FeatureMembership.class::cast)
.findFirst()
.orElse(null);
if (relationship instanceof ParameterMembership) {
if (featureMembership instanceof ParameterMembership) {
this.appendPositionalArgumentList(builder, expression);
} else if (relationship instanceof FeatureMembership) {
} else if (featureMembership != null) {
this.reportConsumer.accept(Status.warning("NamedArgumentList are not handled yet {0}", expression.getElementId()));
}
builder.append(LabelConstants.CLOSE_PARENTHESIS);
Expand Down Expand Up @@ -1387,7 +1381,7 @@ private void appendClassificationExpression(Appender builder, OperatorExpression
expression.getOwnedRelationship().stream()
.filter(ParameterMembership.class::isInstance)
.map(ParameterMembership.class::cast)
.filter(param -> param.getOwnedMemberParameter() instanceof Feature)
.filter(param -> param.getOwnedMemberParameter() != null)
.findFirst()
.ifPresent(membership -> this.appendArgumentMember(builder, membership));

Expand Down Expand Up @@ -1448,7 +1442,7 @@ private void appendDefinitionDeclaration(Appender builder, Definition definition
.toList();

List<Classifier> subClassificationClassifier = subClassification.stream()
.map(sub -> sub.getSuperclassifier())
.map(Subclassification::getSuperclassifier)
.filter(this::isNotNullAndNotAProxy)
.toList();
if (!subClassificationClassifier.isEmpty()) {
Expand Down Expand Up @@ -1550,7 +1544,7 @@ private void appendExtensionKeyword(Appender builder, Type type) {
.filter(MetadataUsage.class::isInstance)
.map(MetadataUsage.class::cast)
.map(MetadataUsage::getMetadataDefinition)
.filter(NOT_NULL)
.filter(Objects::nonNull)
.forEach(mDef -> this.appendPrefixMetadataMember(builder, mDef));
}
}
Expand Down Expand Up @@ -1634,7 +1628,7 @@ private void appendChildrenContent(Appender builder, Element element, List<? ext
}

private String getContent(List<? extends Relationship> children, String prefix) {
return children.stream().map(rel -> this.doSwitch(rel)).filter(NOT_NULL).collect(joining(this.lineSeparator, prefix, ""));
return children.stream().map(this::doSwitch).filter(Objects::nonNull).collect(joining(this.lineSeparator, prefix, ""));
}

@Override
Expand All @@ -1644,7 +1638,7 @@ public String caseImport(Import aImport) {

VisibilityKind visibility = aImport.getVisibility();
if (visibility != VisibilityKind.PUBLIC) {
builder.append(this.getVisivilityIndicator(visibility));
builder.append(this.getVisibilityIndicator(visibility));
}
builder.appendSpaceIfNeeded().append("import ");

Expand Down Expand Up @@ -1811,7 +1805,7 @@ public String caseOwningMembership(OwningMembership owningMembership) {

this.appendMembershipPrefix(owningMembership, builder);

String content = owningMembership.getOwnedRelatedElement().stream().map(rel -> this.doSwitch(rel)).filter(NOT_NULL).collect(joining(builder.getNewLine()));
String content = owningMembership.getOwnedRelatedElement().stream().map(this::doSwitch).filter(Objects::nonNull).collect(joining(builder.getNewLine()));
builder.appendSpaceIfNeeded().append(content);

return builder.toString();
Expand Down Expand Up @@ -1868,7 +1862,7 @@ private void appendUsage(Appender builder, Usage portUsage) {
private void appendMembershipPrefix(Membership membership, Appender builder) {
VisibilityKind visibility = membership.getVisibility();
if (visibility != VisibilityKind.PUBLIC) {
builder.append(this.getVisivilityIndicator(visibility));
builder.append(this.getVisibilityIndicator(visibility));
}
}

Expand All @@ -1884,7 +1878,7 @@ private void appendMembershipImport(Appender builder, MembershipImport membershi

Membership importedMembership = membershipImport.getImportedMembership();
if (importedMembership != null) {
String qnName = Stream.concat(Stream.ofNullable(importedMembership.getMemberElement()), importedMembership.getOwnedRelatedElement().stream()).filter(e -> e != null).findFirst()
String qnName = Stream.concat(Stream.ofNullable(importedMembership.getMemberElement()), importedMembership.getOwnedRelatedElement().stream()).filter(Objects::nonNull).findFirst()
.map(e -> this.buildImportContextRelativeQualifiedName(e, membershipImport)).orElse("");

builder.appendSpaceIfNeeded().append(qnName);
Expand All @@ -1897,7 +1891,7 @@ private String buildImportContextRelativeQualifiedName(Element element, Element
if (commonAncestor != null) {
String prefix = commonAncestor.getQualifiedName() + "::";
if (qualifiedName.startsWith(prefix)) {
return qualifiedName.substring(prefix.length(), qualifiedName.length());
return qualifiedName.substring(prefix.length());
}
}
return qualifiedName;
Expand All @@ -1915,7 +1909,7 @@ private String appendNameWithShortName(Appender builder, Element element) {
return builder.toString();
}

public String getVisivilityIndicator(VisibilityKind visibility) {
public String getVisibilityIndicator(VisibilityKind visibility) {
if (visibility == null) {
return "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
package org.eclipse.syson.sysml.export.utils;

import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toSet;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -42,7 +42,7 @@
import org.slf4j.LoggerFactory;

/**
* Object in charge of converting an Element to a resolvable qualified name depending of its context. This class tries
* Object in charge of converting an Element to a resolvable qualified name depending on its context. This class tries
* its best to find the shortest resolvable name. Be aware that this element keeps a cache of the computation of
* {@link Namespace#visibleMemberships(EList, boolean, boolean)}. It should be used on a static model, discard this
* object if the model changes.
Expand Down Expand Up @@ -88,11 +88,11 @@ public String getDeresolvedName(Element element, Element context) {
for (Namespace deresolvingNamespace : deresolvingNamespaces) {

// An element is either reachable form its containment tree or via a reference Membership#memberElement
Set<Membership> elementAncestors = EMFUtils.getAncestors(Membership.class, element, null).stream().collect(toSet());
Set<Membership> elementAncestors = new HashSet<>(EMFUtils.getAncestors(Membership.class, element, null));
EMFUtils.getInverse(element, SysmlPackage.eINSTANCE.getMembership_MemberElement()).stream().map(s -> (Membership) s.getEObject()).forEach(elementAncestors::add);
String computedQualidiedName = this.deresolve(element, deresolvingNamespace, deresolvingNamespace, elementAncestors);
if (computedQualidiedName != null && !computedQualidiedName.isBlank()) {
qualifiedNames.add(computedQualidiedName);
String computedQualifiedName = this.deresolve(element, deresolvingNamespace, deresolvingNamespace, elementAncestors);
if (computedQualifiedName != null && !computedQualifiedName.isBlank()) {
qualifiedNames.add(computedQualifiedName);
}

}
Expand Down Expand Up @@ -159,7 +159,7 @@ private String deresolve(Element element, Namespace sourceNamespace, Namespace d
// Try to compute its qualified name
qualifiedName = this.buildRelativeQualifiedName(element, deresolvingNamespace, importedContainer.get(), sourceNamespace);
} else {
// Ask to the parent namespace
// Query the parent namespace
qualifiedName = this.deresolve(element, sourceNamespace, deresolvingNamespace.getOwningNamespace(), ancestors);
}

Expand All @@ -172,13 +172,13 @@ private String deresolve(Element element, Namespace sourceNamespace, Namespace d
}

private int getPathLength(Element element, Membership ancestor) {
int lenght = 0;
int length = 0;
EObject current = element;
while (current != null && ancestor != current) {
lenght++;
length++;
current = current.eContainer();
}
return lenght;
return length;
}

private boolean isContainerOrIdentity(Namespace ancestorObjectNamespace, Namespace namespace) {
Expand Down Expand Up @@ -208,8 +208,8 @@ private EList<Membership> getVisibleMemberships(Namespace deresolvingNamespace,

private String buildRelativeQualifiedName(Element element, Namespace owningNamespace, Membership visibleMembership, Namespace sourceNamespace) {
String elementQn = this.getQualifiedName(element);
if (elementQn == null || elementQn.isEmpty()) {
LOGGER.warn("No qualified name found for " + element.getElementId());
if (elementQn.isEmpty()) {
LOGGER.warn("No qualified name found for {}", element.getElementId());
return "";
}

Expand All @@ -223,8 +223,8 @@ private String buildRelativeQualifiedName(Element element, Namespace owningNames
if (resolvedElement != null && !this.match(element, resolvedElement)) {
// Last try if the element is in the containment tree find the shortest qualified name
String qualifiedName = this.getQualifiedName(owningNamespace);
if (qualifiedName != null && !qualifiedName.isBlank() && elementQn.startsWith(qualifiedName)) {
relativeQualifiedName = owningNamespace.getName() + "::" + elementQn.substring(qualifiedName.length() + 2, elementQn.length());
if (!qualifiedName.isBlank() && elementQn.startsWith(qualifiedName)) {
relativeQualifiedName = owningNamespace.getName() + "::" + elementQn.substring(qualifiedName.length() + 2);
} else {
relativeQualifiedName = elementQn;
}
Expand All @@ -236,19 +236,19 @@ private String buildRelativeQualifiedName(Element element, Namespace owningNames

/**
* Checks if the resolved element is either the resolved element member or if it implicitly matches the naming
* feature of unamed element which name computation used the resolved element.
* feature of unnamed element which name computation used the resolved element.
*
* @param element
* the element to compute the name from
* @param resolvedElement
* the resolution tested name in the local namespace
* @return <code>true</code> if the elements matche
* @return <code>true</code> if the elements match
*/
private boolean match(Element element, Membership resolvedElement) {
return resolvedElement.getMemberElement() == element || this.matchImplicite(resolvedElement, element);
return resolvedElement.getMemberElement() == element || this.implicitMatch(resolvedElement, element);
}

private boolean matchImplicite(Membership resolvedMembership, Element context) {
private boolean implicitMatch(Membership resolvedMembership, Element context) {
Element resolvedElement = resolvedMembership.getMemberElement();

if (resolvedElement.getDeclaredName() == null && resolvedElement.getDeclaredShortName() == null && resolvedElement instanceof Feature feature) {
Expand All @@ -262,9 +262,9 @@ private String getRelativeQualifiedName(String elementQn, Element element, Membe
final String qn;
Element importedElement = m.getMemberElement();
String importedElementQualifiedName = this.getQualifiedName(importedElement);
if (importedElement != element && importedElementQualifiedName != null && !importedElementQualifiedName.isEmpty()) {
if (importedElement != element && !importedElementQualifiedName.isEmpty()) {
int partToRemove = importedElementQualifiedName.length() + 2;
qn = Appender.toPrintableName(importedElement.getName()) + "::" + elementQn.substring(partToRemove, elementQn.length());
qn = Appender.toPrintableName(importedElement.getName()) + "::" + elementQn.substring(partToRemove);
} else {
qn = Appender.toPrintableName(element.getName());
}
Expand Down

0 comments on commit 639c20e

Please sign in to comment.