Skip to content

Commit

Permalink
[1801] [refactor] Consistent variable names in LocationHandler
Browse files Browse the repository at this point in the history
Use better local variables and parameters. No behavior change.

Bug: 1801
Change-Id: I26a4b1cd03d422c5e102de8674ce50fe0b303e7b
Signed-off-by: Felix Dorner <felix.dorner@gmail.com>
  • Loading branch information
felixdo committed Mar 21, 2018
1 parent ad5c83f commit 97fe7ee
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 70 deletions.
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2006, 2016 THALES GLOBAL SERVICES.
* Copyright (c) 2006, 2017 THALES GLOBAL SERVICES.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
*
* Contributors:
* Thales - initial API and implementation
*******************************************************************************/
Expand Down Expand Up @@ -51,9 +51,9 @@ public class DefaultLocationHandler implements ILocationHandler {
* {@inheritDoc}
*/
@Override
public EObject getCurrentLocation(CatalogElementLink object, IContext context) {
if (currentLocation.containsKey(object)) {
return currentLocation.get(object);
public EObject getCurrentLocation(CatalogElementLink link, IContext context) {
if (currentLocation.containsKey(link)) {
return currentLocation.get(link);
}
return null;
}
Expand Down Expand Up @@ -117,8 +117,8 @@ private boolean isInvalidResourceLocation(CatalogElementLink link, EObject locat
* {@inheritDoc}
*/
@Override
public void setCurrentLocation(CatalogElementLink object, EObject container, IContext context) {
currentLocation.put(object, container);
public void setCurrentLocation(CatalogElementLink link, EObject container, IContext context) {
currentLocation.put(link, container);
}

protected CatalogElementLink getLink(CatalogElement element, EObject target, IContext context) {
Expand Down Expand Up @@ -154,6 +154,7 @@ protected CatalogElementLink getLinkRelatedElement(CatalogElementLink link, ICon
return null;
}

@Override
public void cleanLocations(IContext context) {
currentLocation.clear();
locations.clear();
Expand All @@ -164,9 +165,8 @@ public void cleanLocations(IContext context) {
* {@inheritDoc}
*/
@Override
public EObject getLocation(CatalogElementLink object, CatalogElementLink origin, IContext context) {
public EObject getLocation(CatalogElementLink link, CatalogElementLink oppositeLink, IContext context) {

CatalogElementLink link = object;
EObject target = link.getTarget();

if (target == null) {
Expand All @@ -191,10 +191,10 @@ public EObject getLocation(CatalogElementLink object, CatalogElementLink origin,
return relatedElement;
}

if (((target instanceof CatalogElement)) || !ContextScopeHandlerHelper.getInstance(context).contains(IReConstants.CREATED_LINKS, object, context)) {
if (!isInvalidResourceLocation(link, object.getTarget().eContainer(), context)) {
locations.put(link, object.getTarget().eContainer());
return object.getTarget().eContainer();
if (((target instanceof CatalogElement)) || !ContextScopeHandlerHelper.getInstance(context).contains(IReConstants.CREATED_LINKS, link, context)) {
if (!isInvalidResourceLocation(link, link.getTarget().eContainer(), context)) {
locations.put(link, link.getTarget().eContainer());
return link.getTarget().eContainer();
}
}

Expand Down Expand Up @@ -255,78 +255,77 @@ protected Collection<EObject> getRelatedElements(EObject element) {
* {@inheritDoc}
*/
@Override
public EObject getDefaultLocation(CatalogElementLink source, CatalogElementLink origin, IContext context) {
CatalogElementLink linkSource = source;
if ((linkSource == null) || (linkSource.getTarget() == null)) {
public EObject getDefaultLocation(CatalogElementLink link, CatalogElementLink oppositeLink, IContext context) {
if ((link == null) || (link.getTarget() == null)) {
return null;
}

if (defaultLocations.containsKey(source)) {
return defaultLocations.get(source);
if (defaultLocations.containsKey(link)) {
return defaultLocations.get(link);
}

// Retrieve default containers
Collection<EObject> elementsContainers = retrieveDefaultContainers(source, origin, context);
Collection<EObject> elementsContainers = retrieveDefaultContainers(link, oppositeLink, context);

// Now we are looking for a feature according to related rule and if we can add element to one of the retrieve possible container, we return it
IRuleAttachment arule = getRule(linkSource.getTarget(), context);
IRuleAttachment arule = getRule(link.getTarget(), context);

for (EObject targetContainer : elementsContainers) {

// If REC and RPL are not located in the same resource, we exclude all containers not located in the targetElement's resource.
if (isInvalidResourceLocation(linkSource, targetContainer, context)) {
if (isInvalidResourceLocation(link, targetContainer, context)) {
continue;
}
if (!isValidContainement(targetContainer, linkSource.getTarget())) {
if (!isValidContainement(targetContainer, link.getTarget())) {
continue;
}

EStructuralFeature sourceFeature = linkSource.getTarget().eContainingFeature();
EStructuralFeature sourceFeature = link.getTarget().eContainingFeature();
EStructuralFeature targetFeature = sourceFeature;
if (arule != null) {
// If we have a rule, we use with priority rule information
targetFeature = getFeature(arule, linkSource.getTarget(), linkSource.getTarget(), targetContainer, context);
if (((targetFeature == null) || (targetFeature == sourceFeature)) && (linkSource.getOrigin() != null) && (linkSource.getOrigin().getTarget() != null)) {
targetFeature = getFeature(arule, linkSource.getOrigin().getTarget(), linkSource.getTarget(), targetContainer, context);
targetFeature = getFeature(arule, link.getTarget(), link.getTarget(), targetContainer, context);
if (((targetFeature == null) || (targetFeature == sourceFeature)) && (link.getOrigin() != null) && (link.getOrigin().getTarget() != null)) {
targetFeature = getFeature(arule, link.getOrigin().getTarget(), link.getTarget(), targetContainer, context);
}
}

if (targetFeature != null) {
if (AttachmentHelper.getInstance(context).isApplicable(targetContainer.eClass(), targetFeature)) {
defaultLocations.put(source, targetContainer);
defaultLocations.put(link, targetContainer);
return targetContainer;
}
}
}

try {
CatalogElementLink linkSourceOrigin = linkSource.getOrigin();
EObject other = TraceabilityHandlerHelper.getInstance(context).retrieveSourceElements(linkSource.getTarget(), context).iterator().next();
CatalogElementLink linkOrigin = link.getOrigin();
EObject other = TraceabilityHandlerHelper.getInstance(context).retrieveSourceElements(link.getTarget(), context).iterator().next();

if (arule != null) {
EObject result = arule.retrieveDefaultContainer(linkSourceOrigin == null ? other : linkSourceOrigin.getTarget(), linkSource.getTarget(), context);
EObject result = arule.retrieveDefaultContainer(linkOrigin == null ? other : linkOrigin.getTarget(), link.getTarget(), context);

if (result != null) {
if (!isValidContainement(result, linkSource.getTarget())) {
defaultLocations.put(source, null);
if (!isValidContainement(result, link.getTarget())) {
defaultLocations.put(link, null);
return null;
}
// If REC and RPL are not located in the same resource, we exclude all containers not located in the destination's resource.
if (isInvalidResourceLocation(linkSource, result, context)) {
defaultLocations.put(source, null);
if (isInvalidResourceLocation(link, result, context)) {
defaultLocations.put(link, null);
return null;
}
}

defaultLocations.put(source, result);
defaultLocations.put(link, result);
return result;
}

} catch (Exception exception) {
exception.printStackTrace();
}

defaultLocations.put(source, null);
defaultLocations.put(link, null);
return null;
}

Expand All @@ -336,6 +335,7 @@ public EObject getDefaultLocation(CatalogElementLink source, CatalogElementLink
* @param currentLocation
* @return
*/
@Override
public EStructuralFeature getFeature(EObject source, EObject target, EObject currentLocation, IContext context) {
if (source == null) {
return null;
Expand All @@ -360,15 +360,14 @@ protected EStructuralFeature getFeature(IRuleAttachment rule, EObject source, EO
}

/**
* @param target
* @param source
* @param link
* @param oppositeLink
* @param context
* @return
*/
protected Collection<EObject> retrieveDefaultContainers(CatalogElementLink target, CatalogElementLink source, IContext context) {
protected Collection<EObject> retrieveDefaultContainers(CatalogElementLink link, CatalogElementLink oppositeLink, IContext context) {

CatalogElement element = ReplicableElementHandlerHelper.getInstance(context).getInitialTarget(context);
CatalogElementLink linkSource = target;

Collection<EObject> elementsContainers = new LinkedHashSet<EObject>(); // order is important!

Expand All @@ -378,7 +377,7 @@ protected Collection<EObject> retrieveDefaultContainers(CatalogElementLink targe
// For a link stored in catalogElement used "somewhere", if the TARGET catalogElement is used in replica of "somewhere", we can
// find a container for the source which is source.target.eContainer<-replicaOfSomewhere
for (CatalogElement referencingElement : ReplicableElementHandlerHelper.getInstance(context).getLinkingReplicableElements(context,
Collections.singletonList((Object) linkSource.getSource()))) {
Collections.singletonList((Object) link.getSource()))) {
for (CatalogElement referencingElement2 : ReplicableElementHandlerHelper.getInstance(context).getLinkingReplicableElements(context,
Collections.singletonList((Object) element.getOrigin()))) {
if (referencingElement.getOrigin().equals(referencingElement2)) {
Expand All @@ -388,15 +387,14 @@ protected Collection<EObject> retrieveDefaultContainers(CatalogElementLink targe
}
}
}
CatalogElementLink linkToLocate = linkSource;
if ((recUsingSource != null) && (recUsingTarget != null)) {
for (CatalogElementLink linke : (Collection<CatalogElementLink>) (Collection) ReplicableElementHandlerHelper.getInstance(context).getElementsLinks(
for (CatalogElementLink sLink : (Collection<CatalogElementLink>) (Collection) ReplicableElementHandlerHelper.getInstance(context).getElementsLinks(
recUsingSource)) {
if (((linkToLocate.getTarget() != null) && (linkToLocate.getTarget().eContainer() != null))
&& linkToLocate.getTarget().eContainer().equals(linke.getTarget())) {
for (CatalogElementLink link : recUsingTarget.getOwnedLinks()) {
if ((link != null) && ((link.getOrigin() != null) && link.getOrigin().equals(linke))) {
elementsContainers.add(link.getTarget());
if (((link.getTarget() != null) && (link.getTarget().eContainer() != null))
&& link.getTarget().eContainer().equals(sLink.getTarget())) {
for (CatalogElementLink tLink : recUsingTarget.getOwnedLinks()) {
if ((tLink != null) && ((tLink.getOrigin() != null) && tLink.getOrigin().equals(sLink))) {
elementsContainers.add(tLink.getTarget());
}
}
}
Expand All @@ -409,22 +407,22 @@ protected Collection<EObject> retrieveDefaultContainers(CatalogElementLink targe
for (EObject ppp : elements) {

for (EObject item : getRelatedElements(ppp)) {
if (isInvalidResourceLocation(linkSource, item, context)) {
if (isInvalidResourceLocation(link, item, context)) {
continue;
}
// if the selection is a REC, we add the container of the type (thus in the REC) of the RPL element as a possible location
if (item instanceof CatalogElement) {
String value = (String) context.get(IReConstants.COMMAND__CURRENT_VALUE);
// specific case when parameters are reversed
if (IReConstants.COMMAND__UPDATE_DEFINITION_REPLICA_FROM_REPLICA.equals(value)) {
elementsContainers.add(source.getTarget().eContainer());
elementsContainers.add(oppositeLink.getTarget().eContainer());
}
// nominal case
else {
elementsContainers.add(target.getOrigin().getTarget().eContainer());
elementsContainers.add(link.getOrigin().getTarget().eContainer());
}
} else {
if (isInitialSelectionValidContainer(item, linkSource, context)) {
if (isInitialSelectionValidContainer(item, link, context)) {
elementsContainers.add(item);
}
}
Expand All @@ -439,13 +437,13 @@ protected Collection<EObject> retrieveDefaultContainers(CatalogElementLink targe
if ((linkA == null) || ((linkA.getOrigin() == null) || (linkA.getOrigin().getTarget() == null)) || (linkA.getOrigin().getTarget().eContainer() == null)) {
continue; // invalid link
}
if ((linkSource == null) || ((linkSource.getOrigin() == null) || (linkSource.getOrigin().getTarget() == null))) {
if ((link == null) || ((link.getOrigin() == null) || (link.getOrigin().getTarget() == null))) {
continue; // invalid link
}
if ((linkA.equals(linkSource))) {
if ((linkA.equals(link))) {
continue; // not a brother
}
if (!linkA.getOrigin().getTarget().eContainer().equals(linkSource.getOrigin().getTarget().eContainer())) {
if (!linkA.getOrigin().getTarget().eContainer().equals(link.getOrigin().getTarget().eContainer())) {
continue; // not a brother
}

Expand All @@ -456,9 +454,9 @@ protected Collection<EObject> retrieveDefaultContainers(CatalogElementLink targe
String scope = (String) context.get(ITransitionConstants.OPTIONS_SCOPE);
IPropertyContext propertyContext = ((IPropertyHandler) OptionsHandlerHelper.getInstance(context)).getPropertyContext(context, scope);

if (linkSource.getTarget() instanceof CatalogElement) {
if (link.getTarget() instanceof CatalogElement) {
CatalogElement sourceElement = ReplicableElementHandlerHelper.getInstance(context).getSource(context);
if ((sourceElement != null) && sourceElement.equals(linkSource.getSource())) {
if ((sourceElement != null) && sourceElement.equals(link.getSource())) {
IProperty property = propertyContext.getProperties().getProperty(IReConstants.PROPERTY__LOCATION_SOURCE);
if (property != null) {
Object value = propertyContext.getCurrentValue(property);
Expand All @@ -468,7 +466,7 @@ protected Collection<EObject> retrieveDefaultContainers(CatalogElementLink targe
}
}
CatalogElement targetElement = ReplicableElementHandlerHelper.getInstance(context).getTarget(context);
if ((targetElement != null) && targetElement.equals(linkSource.getSource())) {
if ((targetElement != null) && targetElement.equals(link.getSource())) {

IProperty property = propertyContext.getProperties().getProperty(IReConstants.PROPERTY__LOCATION_TARGET);
if (property != null) {
Expand Down
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2006, 2016 THALES GLOBAL SERVICES.
* Copyright (c) 2006, 2017 THALES GLOBAL SERVICES.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
*
* Contributors:
* Thales - initial API and implementation
*******************************************************************************/
Expand All @@ -20,28 +20,28 @@
public interface ILocationHandler extends IHandler {

/**
* The location of element after choices made by user
* Get the location of the link's target element as chosen by the user
*/
public EObject getCurrentLocation(CatalogElementLink object, IContext context);
public EObject getCurrentLocation(CatalogElementLink link, IContext context);

/**
* Set the location of element after choices made by user
* Set the location of the link's target element as chosen by the user
*/
public void setCurrentLocation(CatalogElementLink object, EObject container, IContext context);
public void setCurrentLocation(CatalogElementLink link, EObject container, IContext context);

/**
* Retrieve the location for the given object.
* Such location are locations were there is no choice, for instance a port will be contained in its parent.
* Get the location of the link's target element. Such locations are locations were there is no choice,
* for instance a port will be contained in its parent.
*/
public EObject getLocation(CatalogElementLink object, CatalogElementLink origin, IContext context);
public EObject getLocation(CatalogElementLink link, CatalogElementLink oppositeLink, IContext context);

/**
* The default location which can be used to store the link
* Get the default location which can be used to store the link's target element.
*/
public EObject getDefaultLocation(CatalogElementLink object, CatalogElementLink origin, IContext context);
public EObject getDefaultLocation(CatalogElementLink link, CatalogElementLink oppositeLink, IContext context);

/**
* Remove all locations which have been computed
* Remove all locations which have been computed
*/
public void cleanLocations(IContext context);

Expand Down

0 comments on commit 97fe7ee

Please sign in to comment.