/***************************************************************************** * Copyright (c) 2010 Atos Origin. * * * 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: * Mathieu Velten (Atos Origin) mathieu.velten@atosorigin.com - Initial API and implementation * *****************************************************************************/ package org.eclipse.papyrus.infra.core.utils; import java.util.Collection; import java.util.HashSet; import java.util.Set; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EPackage; import org.eclipse.emf.ecore.EStructuralFeature.Setting; import org.eclipse.papyrus.commands.DestroyElementPapyrusCommand; public class PapyrusEcoreUtils { /** * Gets the usages. * * @param source * the source * * @return the usages or null if there is no usages */ public static Collection<Setting> getUsages(EObject source) { return DestroyElementPapyrusCommand.getUsages(source); } /** * <pre> * Test if the used element is referenced by other elements than the known * referencer (except its container). It ignores references from an other meta-model. * </pre> * * @param usedObject * the used object * @param knownReferencer * the known referencer * @return true if the known referencer is the only referencer. */ public static boolean isOnlyUsage(EObject usedObject, EObject knownReferencer) { boolean isUsed = false; EPackage mmPackage = usedObject.eClass().getEPackage(); // Retrieve the list of elements referencing the usedObject. Set<EObject> crossReferences = new HashSet<EObject>(); for(Setting setting : PapyrusEcoreUtils.getUsages(usedObject)) { EObject eObj = setting.getEObject(); if(eObj.eClass().getEPackage().equals(mmPackage)) { crossReferences.add(eObj); } } // Remove the container of used object. crossReferences.remove(usedObject.eContainer()); // Remove the knownReferencer from the list of references. crossReferences.remove(knownReferencer); // If no referencer remains in the list, the known element is the only // usage. if(crossReferences.isEmpty()) { isUsed = true; } return isUsed; } }