Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaxime Porhel2019-07-16 05:25:21 -0400
committerMaxime Porhel2019-07-22 09:33:01 -0400
commitc1f510aafd0bb60a2bb16a525676130808057d5c (patch)
treead7e729271be70ecb809eca9a70781a19ef39035
parente67a04a56de436ec633c547b9fb22b26cc13dde4 (diff)
downloadorg.eclipse.sirius-c1f510aafd0bb60a2bb16a525676130808057d5c.tar.gz
org.eclipse.sirius-c1f510aafd0bb60a2bb16a525676130808057d5c.tar.xz
org.eclipse.sirius-c1f510aafd0bb60a2bb16a525676130808057d5c.zip
[cleanup] Reduce code duplication in saving policies.
Change-Id: I19eca87e713f57bd5f8fc7b684b919a1baad09ae Signed-off-by: Maxime Porhel <maxime.porhel@obeo.fr>
-rw-r--r--plugins/org.eclipse.sirius.doc/doc/Release_Notes.html5
-rw-r--r--plugins/org.eclipse.sirius.doc/doc/Release_Notes.textile2
-rw-r--r--plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/session/AbstractSavingPolicy.java31
-rw-r--r--plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/session/SavingPolicyImpl.java30
-rw-r--r--plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/IsModifiedSavingPolicy.java93
5 files changed, 68 insertions, 93 deletions
diff --git a/plugins/org.eclipse.sirius.doc/doc/Release_Notes.html b/plugins/org.eclipse.sirius.doc/doc/Release_Notes.html
index 809d380e25..d8ec8ae6ec 100644
--- a/plugins/org.eclipse.sirius.doc/doc/Release_Notes.html
+++ b/plugins/org.eclipse.sirius.doc/doc/Release_Notes.html
@@ -148,8 +148,9 @@
<code>DRepresentationDescriptor</code> in a
<code>DView</code>.
</li>
- </ul>
- <ul>
+ <li><span class="label label-success">Added</span>The method
+ <code>org.eclipse.sirius.business.api.session.AbstractSavingPolicy.hasDifferentSerialization(Resource, Map)</code> has been extracted from internal subclasses. It is able to tell whether a save operation on a resource can succeed or not, and if the resulting file will change. It should be used with care as it basically saves the resource in a temporary resource to know whether it will change the file or not.
+ </li>
<li><span class="label label-info">Modified</span>The method
<code>org.eclipse.sirius.business.api.dialect.AbstractRepresentationDialectServices.copyRepresentation(DRepresentation, String, Session, IProgressMonitor)</code> has its first parameter changed from
<code>DRepresentation</code> to
diff --git a/plugins/org.eclipse.sirius.doc/doc/Release_Notes.textile b/plugins/org.eclipse.sirius.doc/doc/Release_Notes.textile
index f0b3048715..acb82286f6 100644
--- a/plugins/org.eclipse.sirius.doc/doc/Release_Notes.textile
+++ b/plugins/org.eclipse.sirius.doc/doc/Release_Notes.textile
@@ -14,7 +14,7 @@ h4. Changes in @org.eclipse.sirius@
In case of migration the method @org.eclipse.sirius.business.api.migration.AbstractRepresentationsFileMigrationParticipant.updateChangeId(DAnalysis, DRepresentation)@ is available to update change id. New migration participants created after this Sirius version must update the change id of the DRepresentationDescriptor of the DRepresentation they change. The API to do that is described in MigrationParticpant "documentation:":developer/extensions-provide_migrate_contribution.html#MigrationParticipantsImplem .
* <span class="label label-success">Added</span>The method @org.eclipse.sirius.business.api.query.DViewQuery.getLoadedRepresentationsDescriptors()@ has been added and allows to retrieve all loaded @DRepresentationDescriptor@ in a @DView@.
-
+* <span class="label label-success">Added</span>The method @org.eclipse.sirius.business.api.session.AbstractSavingPolicy.hasDifferentSerialization(Resource, Map)@ has been extracted from internal subclasses. It is able to tell whether a save operation on a resource can succeed or not, and if the resulting file will change. It should be used with care as it basically saves the resource in a temporary resource to know whether it will change the file or not.
* <span class="label label-info">Modified</span>The method @org.eclipse.sirius.business.api.dialect.AbstractRepresentationDialectServices.copyRepresentation(DRepresentation, String, Session, IProgressMonitor)@ has its first parameter changed from @DRepresentation@ to @DRepresentationDescriptor@. Also the constructor of @org.eclipse.sirius.business.api.dialect.command.CopyRepresentationCommand@ has been modified. Its parameter @Collection<DRepresentation> representations@ has been replaced by @Collection<DRepresentationDescriptor> representationDescriptors@. Theses changes were made to be able to copy the name that is now only in @DRepresentationDescriptor@.
* <span class="label label-danger">Removed</span> In @org.eclipse.sirius.business.api.session.CustomDataConstants@, the constants @DREPRESENTATION@ and @DREPRESENTATION_DESCRIPTOR@ has been removed because they are not available anymore in custom data.
diff --git a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/session/AbstractSavingPolicy.java b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/session/AbstractSavingPolicy.java
index ad1023d1d0..38a22f5444 100644
--- a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/session/AbstractSavingPolicy.java
+++ b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/session/AbstractSavingPolicy.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2014, 2015 Obeo.
+ * Copyright (c) 2014, 2019 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
@@ -23,6 +23,7 @@ import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.sirius.business.internal.session.danalysis.ResourceSaveDiagnose;
import org.eclipse.sirius.common.tools.api.resource.ResourceSetSync;
import org.eclipse.sirius.viewpoint.Messages;
import org.eclipse.sirius.viewpoint.SiriusPlugin;
@@ -73,8 +74,7 @@ public abstract class AbstractSavingPolicy implements SavingPolicy {
}
/**
- * The default saving options to use if none are specified (i.e. if the
- * <code>options</code> argument passed to
+ * The default saving options to use if none are specified (i.e. if the <code>options</code> argument passed to
* {@link #save(Iterable, Map, IProgressMonitor)} is <code>null</code>.
*
* @return the default saving otions to use if none are specified.
@@ -87,8 +87,7 @@ public abstract class AbstractSavingPolicy implements SavingPolicy {
}
/**
- * Computes the set of resources to save. Subclasses only need to override
- * this method.
+ * Computes the set of resources to save. Subclasses only need to override this method.
*
* @param scope
* the set of resources to consider.
@@ -96,8 +95,26 @@ public abstract class AbstractSavingPolicy implements SavingPolicy {
* the saving options that were requested.
* @param monitor
* the monitor to use to report progress.
- * @return a sub-set of the Resources in scope that must actually be saved
- * on disk.
+ * @return a sub-set of the Resources in scope that must actually be saved on disk.
*/
protected abstract Collection<Resource> computeResourcesToSave(Set<Resource> scope, Map<?, ?> options, IProgressMonitor monitor);
+
+ /**
+ * Check if a resource has a different serialization.
+ *
+ * @param resource
+ * the resource to check
+ * @param options
+ * the save options.
+ * @return <code>true</code> if the resource has changes to save, <code>false</code> otherwise
+ */
+ protected boolean hasDifferentSerialization(Resource resource, Map<?, ?> options) {
+ ResourceSaveDiagnose diagnose = new ResourceSaveDiagnose(resource);
+ try {
+ return diagnose.isSaveable() && diagnose.hasDifferentSerialization(options);
+ } catch (final IOException e) {
+ SiriusPlugin.getDefault().error(Messages.SavingPolicyImpl_savingErrorMsg, e);
+ }
+ return false;
+ }
}
diff --git a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/session/SavingPolicyImpl.java b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/session/SavingPolicyImpl.java
index 4304f2469c..1f764705c8 100644
--- a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/session/SavingPolicyImpl.java
+++ b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/session/SavingPolicyImpl.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2010, 2015 THALES GLOBAL SERVICES.
+ * Copyright (c) 2010, 2019 THALES GLOBAL SERVICES.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
@@ -12,7 +12,6 @@
*******************************************************************************/
package org.eclipse.sirius.business.api.session;
-import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@@ -21,11 +20,8 @@ import java.util.Set;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
-import org.eclipse.sirius.business.internal.session.danalysis.ResourceSaveDiagnose;
import org.eclipse.sirius.common.tools.api.resource.ResourceSetSync;
import org.eclipse.sirius.common.tools.api.resource.ResourceSetSync.ResourceStatus;
-import org.eclipse.sirius.viewpoint.Messages;
-import org.eclipse.sirius.viewpoint.SiriusPlugin;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
@@ -51,9 +47,8 @@ public class SavingPolicyImpl extends AbstractSavingPolicy {
}
/**
- * Determines if a resource should be saved by actually saving it in a
- * temporary location and comaring the result to the current serialization.
- * The result is safe, but the method is costly.
+ * Determines if a resource should be saved by actually saving it in a temporary location and comparing the result
+ * to the current serialization. The result is safe, but the method is costly.
* <p>
* {@inheritDoc}
*/
@@ -74,22 +69,15 @@ public class SavingPolicyImpl extends AbstractSavingPolicy {
*
* @param resource
* the resource to check
- * @return <code>true</code> if the resource has changes to save,
- * <code>false</code> otherwise
+ * @return <code>true</code> if the resource has changes to save, <code>false</code> otherwise
*/
protected boolean hasChangesToSave(final Resource resource) {
- boolean hasChangesToSave = false;
- final ResourceSaveDiagnose diagnose = new ResourceSaveDiagnose(resource);
- try {
- Map<Object, Object> mergedOptions = new HashMap<Object, Object>(getDefaultSaveOptions());
- if (saveOptions != null) {
- mergedOptions.putAll(saveOptions);
- }
- hasChangesToSave = diagnose.isSaveable() && diagnose.hasDifferentSerialization(mergedOptions);
- } catch (final IOException e) {
- SiriusPlugin.getDefault().error(Messages.SavingPolicyImpl_savingErrorMsg, e);
+ Map<Object, Object> mergedOptions = new HashMap<Object, Object>(getDefaultSaveOptions());
+ if (saveOptions != null) {
+ mergedOptions.putAll(saveOptions);
}
- return hasChangesToSave;
+
+ return hasDifferentSerialization(resource, mergedOptions);
}
/**
diff --git a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/IsModifiedSavingPolicy.java b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/IsModifiedSavingPolicy.java
index 5c693cb9c2..cbfb418699 100644
--- a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/IsModifiedSavingPolicy.java
+++ b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/IsModifiedSavingPolicy.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2010, 2016 THALES GLOBAL SERVICES.
+ * Copyright (c) 2010, 2019 THALES GLOBAL SERVICES.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
@@ -12,7 +12,6 @@
*******************************************************************************/
package org.eclipse.sirius.business.internal.session;
-import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -31,12 +30,9 @@ import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.sirius.business.api.helper.SiriusUtil;
import org.eclipse.sirius.business.api.session.AbstractSavingPolicy;
-import org.eclipse.sirius.business.internal.session.danalysis.ResourceSaveDiagnose;
import org.eclipse.sirius.common.tools.api.resource.ResourceMigrationMarker;
import org.eclipse.sirius.common.tools.api.resource.ResourceSetSync;
import org.eclipse.sirius.common.tools.api.resource.ResourceSetSync.ResourceStatus;
-import org.eclipse.sirius.viewpoint.Messages;
-import org.eclipse.sirius.viewpoint.SiriusPlugin;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
@@ -45,21 +41,20 @@ import com.google.common.collect.Iterators;
import com.google.common.collect.Sets;
/**
- * A saving policy implementation which use the isModified state of the
- * resources and their inter-dependencies to determine if a resource should be
- * serialized again or not.
+ * A saving policy implementation which use the isModified state of the resources and their inter-dependencies to
+ * determine if a resource should be serialized again or not.
*
- * <b>This policy assume the isModified flag has been updated for each resource
- * if any change happened in the contained EObjects /!\</b>
+ * <b>This policy assume the isModified flag has been updated for each resource if any change happened in the contained
+ * EObjects /!\</b>
*
* <br>
* What it saves is:
* <ul>
* <li>all the modified resources (isModified=true)</li>
- * <li>go through all the other resources and identify those which have
- * references onto the modified ones : these should be saved too.</li>
- * <li>retrieve the underlying file status for each resource and add the
- * CONFLICTING or DELETED ones in the list of resources to save.</li>
+ * <li>go through all the other resources and identify those which have references onto the modified ones : these should
+ * be saved too.</li>
+ * <li>retrieve the underlying file status for each resource and add the CONFLICTING or DELETED ones in the list of
+ * resources to save.</li>
* </ul>
*
*
@@ -69,23 +64,20 @@ import com.google.common.collect.Sets;
public class IsModifiedSavingPolicy extends AbstractSavingPolicy {
/**
- * Check if a resource has been logically modified, meaning some of its
- * objects have changes in some way.
+ * Check if a resource has been logically modified, meaning some of its objects have changes in some way.
*
* @param resource
* the resource to check
- * @return <code>true</code> if the resource has changes to save,
- * <code>false</code> otherwise
+ * @return <code>true</code> if the resource has changes to save, <code>false</code> otherwise
*/
private Predicate<Resource> isModified = new Predicate<Resource>() {
@Override
public boolean apply(Resource resource) {
/*
- * We assume the resource always is "tracking modification" but not
- * using the Resource-specific implementation. We rely on the fact
- * that the Sirius runtime will set the isModified flag itself when
- * a change is done on the resource.
+ * We assume the resource always is "tracking modification" but not using the Resource-specific
+ * implementation. We rely on the fact that the Sirius runtime will set the isModified flag itself when a
+ * change is done on the resource.
*/
return resource.isModified() || ResourceMigrationMarker.hasMigrationMarker(resource);
@@ -93,13 +85,11 @@ public class IsModifiedSavingPolicy extends AbstractSavingPolicy {
};
/**
- * Check if the resource underlying file is in a DELETED or CONFLICTING
- * state.
+ * Check if the resource underlying file is in a DELETED or CONFLICTING state.
*
* @param resource
* the resource to check
- * @return <code>true</code> if the resource has changes to save,
- * <code>false</code> otherwise
+ * @return <code>true</code> if the resource has changes to save, <code>false</code> otherwise
*/
private Predicate<Resource> underlyingFileIsDeletedOrConflicting = new Predicate<Resource>() {
@@ -121,17 +111,13 @@ public class IsModifiedSavingPolicy extends AbstractSavingPolicy {
}
/**
- * Computes the set of resources to save. This is a safe approximation of
- * the exact sub-set of resource in the scope whose serialization has
- * changed. Saving all the returned resources will produce the same result
- * as saving all the resources in the scope, but in the general case will
- * save much less resources (and thus be faster).
+ * Computes the set of resources to save. This is a safe approximation of the exact sub-set of resource in the scope
+ * whose serialization has changed. Saving all the returned resources will produce the same result as saving all the
+ * resources in the scope, but in the general case will save much less resources (and thus be faster).
* <p>
- * It may save more resources than strictly needed. For example if resource
- * A (not modified) contains references to elements in resource B
- * (modified), but the only references are to elements in B whose URI will
- * not change. In such a case we will save A anyway. More precise analyses
- * would be possible but cost-prohibitive.
+ * It may save more resources than strictly needed. For example if resource A (not modified) contains references to
+ * elements in resource B (modified), but the only references are to elements in B whose URI will not change. In
+ * such a case we will save A anyway. More precise analyses would be possible but cost-prohibitive.
* <p>
* {@inheritDoc}
*/
@@ -157,9 +143,8 @@ public class IsModifiedSavingPolicy extends AbstractSavingPolicy {
Set<Resource> logicallyModified = Sets.newLinkedHashSet(Iterables.filter(saveable, isModified));
/*
- * ... or it references a resource which has been modified (in which
- * case the URIs to the referenced elements in these resource *may*
- * havechanged)...
+ * ... or it references a resource which has been modified (in which case the URIs to the referenced elements in
+ * these resource *may* havechanged)...
*/
Set<Resource> dependOnLogicallyModified = new LinkedHashSet<>();
if (logicallyModified.size() > 0) {
@@ -186,8 +171,7 @@ public class IsModifiedSavingPolicy extends AbstractSavingPolicy {
Set<Resource> underlyingFileDoesNotExist = Sets.newLinkedHashSet(Iterables.filter(saveable, Predicates.not(exists)));
Set<Resource> isConflictingOrDeleted = Sets.newLinkedHashSet(Iterables.filter(saveable, underlyingFileIsDeletedOrConflicting));
/*
- * or the underlying file is out of date and must be recreated/updated
- * to match the version in memory.
+ * or the underlying file is out of date and must be recreated/updated to match the version in memory.
*/
Set<Resource> toSave = new LinkedHashSet<>();
for (Resource resource : Sets.union(logicallyModified, dependOnLogicallyModified)) {
@@ -195,28 +179,16 @@ public class IsModifiedSavingPolicy extends AbstractSavingPolicy {
toSave.add(resource);
} else {
ResourceMigrationMarker.clearMigrationMarker(resource);
-
}
}
Iterables.addAll(toSave, Sets.union(underlyingFileDoesNotExist, isConflictingOrDeleted));
/*
- * if we have something to save which has no different serialization
- * then something is fishy...
+ * if we have something to save which has no different serialization then something is fishy...
*/
return toSave;
}
- private boolean hasDifferentSerialization(Resource input, Map<?, ?> options) {
- ResourceSaveDiagnose diagnose = new ResourceSaveDiagnose(input);
- try {
- return diagnose.isSaveable() && diagnose.hasDifferentSerialization(options);
- } catch (final IOException e) {
- SiriusPlugin.getDefault().error(Messages.SavingPolicyImpl_savingErrorMsg, e);
- }
- return false;
- }
-
private static class ResourceHasReferenceTo implements Predicate<Resource> {
private final Predicate<Resource> modifiedResources;
@@ -242,17 +214,14 @@ public class IsModifiedSavingPolicy extends AbstractSavingPolicy {
public boolean apply(EObject source) {
if (!source.eIsProxy()) {
/*
- * We could process the references in an order which gives us
- * the highest chance to hit a success sooner and avoid, for
- * instance, computing derived references unless strictly
- * necessary.
+ * We could process the references in an order which gives us the highest chance to hit a success sooner
+ * and avoid, for instance, computing derived references unless strictly necessary.
*/
for (EReference ref : source.eClass().getEAllReferences()) {
/*
- * we should not go on containment references as we are
- * already in a getProperContent iteration. We are only
- * interested in references which will impact the
- * serialization (hence ignoring isTransient).
+ * we should not go on containment references as we are already in a getProperContent iteration. We
+ * are only interested in references which will impact the serialization (hence ignoring
+ * isTransient).
*/
if (!ref.isTransient() && !ref.isContainment()) {
for (EObject target : getReferencedEObjects(source, ref)) {

Back to the top