org.eclipse.emf.emfstore.internal.common.model.util
Class ModelUtil

java.lang.Object
  extended by org.eclipse.emf.emfstore.internal.common.model.util.ModelUtil

public final class ModelUtil
extends Object

Utility class for ModelElements.

Author:
koegel, emueller, ottovonwesen, jfaltermeier

Field Summary
static long NO_CHECKSUM
          Constant that may be used in case no checksum computation has taken place.
static org.eclipse.emf.common.util.URI VIRTUAL_URI
          URI used to serialize EObject with the model util.
 
Method Summary
static boolean areEqual(IdEObjectCollection collectionA, IdEObjectCollection collectionB)
          Compares two IdEObjectCollections.
static
<T extends org.eclipse.emf.ecore.EObject>
List<T>
clone(List<T> list)
          Clone a list of EObjects.
static
<T extends org.eclipse.emf.ecore.EObject>
T
clone(T eObject)
          Clone any EObject.
static List<SettingWithReferencedElement> collectOutgoingCrossReferences(IdEObjectCollection collection, Set<org.eclipse.emf.ecore.EObject> modelElements)
          Retrieve all outgoing connections from the model elements to other elements in the collection.
static long computeChecksum(org.eclipse.emf.ecore.EObject eObject)
          Computes the checksum for a given EObject.
static long computeChecksum(IdEObjectCollection collection)
          Computes the checksum for a given IdEObjectCollection.
static IdEObjectCollection copyIdEObjectCollection(IdEObjectCollection collection, org.eclipse.emf.ecore.xmi.XMIResource res)
          Copies the given IdEObjectCollection and writes the IDs it contains into the given XMIResource.
static ModelElementId createModelElementId(String id)
          Creates a ModelElementId object from a string.
static void deleteIncomingCrossReferencesFromParent(Collection<org.eclipse.emf.ecore.EStructuralFeature.Setting> inverseReferences, org.eclipse.emf.ecore.EObject modelElement)
          Delete the given incoming cross references to the given model element from any other model element in the given project.
static void deleteOutgoingCrossReferences(IdEObjectCollection collection, org.eclipse.emf.ecore.EObject modelElement)
          Delete all outgoing cross references of the given model element to any element in the given collection.
static void deleteResourcesWithPrefix(org.eclipse.emf.ecore.resource.ResourceSet resourceSet, String prefix)
          Deletes all resources from resourceSet, which string representation of URI starts with prefix.
static String eObjectToString(org.eclipse.emf.ecore.EObject eObject)
          Copies the given EObject and converts it to a string.
static String eObjectToString(org.eclipse.emf.ecore.EObject eObject, Map<?,?> saveOptions)
          Copies the given EObject and converts it to a string.
static
<T extends org.eclipse.emf.ecore.EObject>
List<T>
flatCloneList(List<T> originalList)
          Create a flat clone of the list, the list if cloned but ot its content.
static Set<org.eclipse.emf.ecore.EObject> getAllContainedModelElements(Collection<org.eclipse.emf.ecore.EObject> modelElements, boolean includeTransientContainments, boolean ignoreSingletonDatatypes)
          Get all contained elements of a given collection of model elements.
static Set<org.eclipse.emf.ecore.EObject> getAllContainedModelElements(org.eclipse.emf.ecore.EObject modelElement, boolean includeTransientContainments)
          Get all contained elements of a given element.
static Set<org.eclipse.emf.ecore.EObject> getAllContainedModelElements(org.eclipse.emf.ecore.EObject modelElement, boolean includeTransientContainments, boolean ignoreSingletonDatatypes)
          Get all contained elements of a given element.
static Set<org.eclipse.emf.ecore.EObject> getAllContainedModelElements(org.eclipse.emf.ecore.resource.Resource resource, boolean includeTransientContainments, boolean ignoreSingletonDatatypes)
          Get all contained elements of a given resource.
static List<org.eclipse.emf.ecore.EObject> getAllContainedModelElementsAsList(org.eclipse.emf.ecore.EObject modelElement, boolean includeTransientContainments)
          Get all contained elements of a given element as a list.
static Set<org.eclipse.emf.ecore.EClass> getAllMETypes(org.eclipse.emf.ecore.EPackage ePackage)
          Recursively goes through package and returns a list of all EClasses inheriting ModelElement (abstract classes and interfaces are also include).
static Map<Object,Object> getChecksumSaveOptions()
          Delivers a map of options that is used while computing a checksum.
static org.eclipse.emf.ecore.EObject getContainerModelElement(org.eclipse.emf.ecore.EObject modelElement)
          Get the container of an EObject.
static ModelElementId getModelElementId(org.eclipse.emf.ecore.EObject modelElement)
          Searches for the project and then looks for the modelelement id.
static Set<org.eclipse.emf.ecore.EClass> getNonAbstractMETypes(org.eclipse.emf.ecore.EPackage ePackage)
          Recursively goes through model and create a list of all non-Abstract classes.
static
<T extends org.eclipse.emf.ecore.EObject>
T
getParent(Class<T> parent, org.eclipse.emf.ecore.EObject child)
          Get the EContainer that contains the given model element and whose eContainer is null.
static Project getProject(org.eclipse.emf.ecore.EObject modelElement)
          Get Project that contains a model element.
static Map<Object,Object> getResourceLoadOptions()
          Delivers a map of options for loading resources.
static IResourceLogger getResourceLogger()
          Returns the resource logger.
static Map<Object,Object> getResourceSaveOptions()
          Delivers a map of mandatory options for saving resources.
static org.eclipse.emf.ecore.EObject getSingleton(ModelElementId singletonId)
          Get the singleton instance for a given model element id.
static ModelElementId getSingletonModelElementId(org.eclipse.emf.ecore.EObject singleton)
          Get the singleton id for a singleton instance.
static boolean isAssociationClassElement(org.eclipse.emf.ecore.EClass eClazz)
          Whether a EClass is a association class.
static boolean isIgnoredDatatype(org.eclipse.emf.ecore.EObject eObject)
          Determines whether the type of an EObject is an ignored one.
static boolean isSingleton(org.eclipse.emf.ecore.EObject eObject)
          Return whether the given eObject instance is a singelton.
static
<T extends org.eclipse.emf.ecore.EObject>
T
loadEObjectFromResource(org.eclipse.emf.ecore.EClass eClass, org.eclipse.emf.common.util.URI resourceURI, boolean checkConstraints)
          Load an EObject from a resource, the resource is supposed to contain only one root object of the given EClass type.
static void loadResource(org.eclipse.emf.ecore.resource.Resource resource, IResourceLogger logger)
          Loads a given resource and logs any warning and/or errors.
static void log(String message, Throwable exception, int statusInt)
          This will add a new entry to error log view of eclipse.
static void logError(String message)
          Log a error to the platform log.
static void logException(String message, Throwable exception)
          Log an exception to the platform log.
static void logException(Throwable exception)
          Log an exception to the platform log.
static void logInfo(String message)
          Log an exception to the platform log.
static void logWarning(String message)
          Log a warning to the platform log.
static void logWarning(String message, Throwable exception)
          Log a warning to the platform log.
static void saveEObjectToResource(org.eclipse.emf.ecore.EObject eObject, org.eclipse.emf.common.util.URI resourceURI)
          Save an EObject to a resource.
static void saveEObjectToResource(List<? extends org.eclipse.emf.ecore.EObject> eObjects, org.eclipse.emf.common.util.URI resourceURI)
          Save a list of EObjects to the resource with the given URI.
static void saveEObjectToResource(List<? extends org.eclipse.emf.ecore.EObject> eObjects, org.eclipse.emf.common.util.URI resourceURI, Map<Object,Object> options)
          Save a list of EObjects to the resource with the given URI.
static void saveResource(org.eclipse.emf.ecore.resource.Resource resource, IResourceLogger logger)
          Saves a given resource and logs any warning and/or errors.
static void setXmiIdsOnResource(Project project, org.eclipse.emf.ecore.xmi.XMIResource xmiResource)
          Set all IDs contained in the project as XMI IDs for the model elements in the project.
static boolean shouldBeCollected(IdEObjectCollection collection, Set<org.eclipse.emf.ecore.EObject> allModelElements, org.eclipse.emf.ecore.EObject referencedElement)
          Checks if the referenced elements is an element in the given collection which is not a singleton, not an ignored data type and not already contained in the given set of elements.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_CHECKSUM

public static final long NO_CHECKSUM
Constant that may be used in case no checksum computation has taken place.

See Also:
Constant Field Values

VIRTUAL_URI

public static final org.eclipse.emf.common.util.URI VIRTUAL_URI
URI used to serialize EObject with the model util.

Method Detail

createModelElementId

public static ModelElementId createModelElementId(String id)
Creates a ModelElementId object from a string.

Parameters:
id - as string
Returns:
id as object

areEqual

public static boolean areEqual(IdEObjectCollection collectionA,
                               IdEObjectCollection collectionB)
Compares two IdEObjectCollections. Order of the model elements at root level of a collection does not influence the outcome of this operations

Parameters:
collectionA - the first collection
collectionB - the second collection
Returns:
true if the two collections are equal, false otherwise

eObjectToString

public static String eObjectToString(org.eclipse.emf.ecore.EObject eObject)
                              throws SerializationException
Copies the given EObject and converts it to a string.

Parameters:
eObject - the eObject to be serialized
Returns:
the string representation of the eObject
Throws:
SerializationException - if a serialization problem occurs

eObjectToString

public static String eObjectToString(org.eclipse.emf.ecore.EObject eObject,
                                     Map<?,?> saveOptions)
                              throws SerializationException
Copies the given EObject and converts it to a string.

Parameters:
eObject - the eObject to be serialized
saveOptions - the EMF save options to be used during serialization
Returns:
the string representation of the eObject
Throws:
SerializationException - if a serialization problem occurs

computeChecksum

public static long computeChecksum(org.eclipse.emf.ecore.EObject eObject)
                            throws SerializationException
Computes the checksum for a given EObject.

Parameters:
eObject - the EObject for which to compute a checksum
Returns:
the computed checksum
Throws:
SerializationException - in case any errors occur during computation of the checksum

computeChecksum

public static long computeChecksum(IdEObjectCollection collection)
                            throws SerializationException
Computes the checksum for a given IdEObjectCollection. The checksum for a collection is independent of the order of the collection's elements at the root level.

Parameters:
collection - the collection for which to compute a checksum
Returns:
the computed checksum
Throws:
SerializationException - in case any errors occur during computation of the checksum

getResourceLogger

public static IResourceLogger getResourceLogger()
Returns the resource logger.

Returns:
the resource logger

copyIdEObjectCollection

public static IdEObjectCollection copyIdEObjectCollection(IdEObjectCollection collection,
                                                          org.eclipse.emf.ecore.xmi.XMIResource res)
Copies the given IdEObjectCollection and writes the IDs it contains into the given XMIResource.

Parameters:
collection - the collection to be copied
res - the resource into which the collection's IDs should be written into
Returns:
the copied collection

isIgnoredDatatype

public static boolean isIgnoredDatatype(org.eclipse.emf.ecore.EObject eObject)
Determines whether the type of an EObject is an ignored one.

Parameters:
eObject - the EObject which is to be checked
Returns:
true, if the EObject will be ignored, false otherwise

getResourceLoadOptions

public static Map<Object,Object> getResourceLoadOptions()
Delivers a map of options for loading resources. Especially XMLResource.OPTION_DEFER_IDREF_RESOLUTION which speeds up loading due to our id based resources.

Returns:
map of options for XMIResource or XMLResource.

getResourceSaveOptions

public static Map<Object,Object> getResourceSaveOptions()
Delivers a map of mandatory options for saving resources.

Returns:
map of options for XMIResource or XMLResource.

getChecksumSaveOptions

public static Map<Object,Object> getChecksumSaveOptions()
Delivers a map of options that is used while computing a checksum.

Returns:
map of options for XMIResource or XMLResource.

saveResource

public static void saveResource(org.eclipse.emf.ecore.resource.Resource resource,
                                IResourceLogger logger)
                         throws IOException
Saves a given resource and logs any warning and/or errors.

Parameters:
resource - the resource to be saved
logger - a logger instance which will be used to log warnings and errors on resources
Throws:
IOException - in case an exception occurs during save

loadResource

public static void loadResource(org.eclipse.emf.ecore.resource.Resource resource,
                                IResourceLogger logger)
                         throws IOException
Loads a given resource and logs any warning and/or errors.

Parameters:
resource - the resource to be loaded
logger - a logger instance which will be used to log warnings and errors on resources
Throws:
IOException - in case an exception occurs during load

getNonAbstractMETypes

public static Set<org.eclipse.emf.ecore.EClass> getNonAbstractMETypes(org.eclipse.emf.ecore.EPackage ePackage)
Recursively goes through model and create a list of all non-Abstract classes.

Parameters:
ePackage - the package to start with.
Returns:
list of all non-Abstract model element classes in starting package and its sub-packages

getAllMETypes

public static Set<org.eclipse.emf.ecore.EClass> getAllMETypes(org.eclipse.emf.ecore.EPackage ePackage)
Recursively goes through package and returns a list of all EClasses inheriting ModelElement (abstract classes and interfaces are also include).

Parameters:
ePackage - starting package
Returns:
a list of all EClasses inheriting ModelElement (inclusive abstract classes and interfaces) in starting package and all its sub-packages.

log

public static void log(String message,
                       Throwable exception,
                       int statusInt)
This will add a new entry to error log view of eclipse.

Parameters:
message - message
exception - exception
statusInt - severity. Use one of constants in org.eclipse.core.runtime.Status class.

logException

public static void logException(String message,
                                Throwable exception)
Log an exception to the platform log. This will create a popup in the ui.

Parameters:
message - the message
exception - the exception

logException

public static void logException(Throwable exception)
Log an exception to the platform log. This will create a popup in the ui.

Parameters:
exception - the exception

logWarning

public static void logWarning(String message,
                              Throwable exception)
Log a warning to the platform log. This will NOT create a popup in the UI.

Parameters:
message - the message
exception - the exception

logWarning

public static void logWarning(String message)
Log a warning to the platform log. This will NOT create a popup in the UI.

Parameters:
message - the message being logged

logError

public static void logError(String message)
Log a error to the platform log. This will NOT create a popup in the UI.

Parameters:
message - the message being logged

logInfo

public static void logInfo(String message)
Log an exception to the platform log. This will create a popup in the ui.

Parameters:
message - the message

clone

public static <T extends org.eclipse.emf.ecore.EObject> T clone(T eObject)
Clone any EObject.

Type Parameters:
T - the Eobject sub type
Parameters:
eObject - the Eobject instance
Returns:
a clone of the Eobject instance

clone

public static <T extends org.eclipse.emf.ecore.EObject> List<T> clone(List<T> list)
Clone a list of EObjects.

Type Parameters:
T - the EObject sub type the list consists of
Parameters:
list - the list instance
Returns:
a clone of the list and its contents instance

flatCloneList

public static <T extends org.eclipse.emf.ecore.EObject> List<T> flatCloneList(List<T> originalList)
Create a flat clone of the list, the list if cloned but ot its content.

Type Parameters:
T - the list type parameter
Parameters:
originalList - the original list
Returns:
a flat copy

loadEObjectFromResource

public static <T extends org.eclipse.emf.ecore.EObject> T loadEObjectFromResource(org.eclipse.emf.ecore.EClass eClass,
                                                                                  org.eclipse.emf.common.util.URI resourceURI,
                                                                                  boolean checkConstraints)
                                                                       throws IOException
Load an EObject from a resource, the resource is supposed to contain only one root object of the given EClass type. Type T must match EClass type.

Type Parameters:
T - Type of the EObject
Parameters:
eClass - the EClass of the EObject
resourceURI - the resources URI
checkConstraints - whether to perform additional sanity checks. These checks basically try to enforce that a resource contains exactly one object.
Returns:
the object loaded from the resource
Throws:
IOException - if loading the object from the resource fails.

saveEObjectToResource

public static void saveEObjectToResource(List<? extends org.eclipse.emf.ecore.EObject> eObjects,
                                         org.eclipse.emf.common.util.URI resourceURI,
                                         Map<Object,Object> options)
                                  throws IOException
Save a list of EObjects to the resource with the given URI.

Parameters:
eObjects - the EObjects to be saved
resourceURI - the URI of the resource, which should be used to save the EObjects
options - The save options for the resource.
Throws:
IOException - if saving to the resource fails

saveEObjectToResource

public static void saveEObjectToResource(List<? extends org.eclipse.emf.ecore.EObject> eObjects,
                                         org.eclipse.emf.common.util.URI resourceURI)
                                  throws IOException
Save a list of EObjects to the resource with the given URI.

Parameters:
eObjects - the EObjects to be saved
resourceURI - the URI of the resource, which should be used to save the EObjects
Throws:
IOException - if saving to the resource fails

setXmiIdsOnResource

public static void setXmiIdsOnResource(Project project,
                                       org.eclipse.emf.ecore.xmi.XMIResource xmiResource)
Set all IDs contained in the project as XMI IDs for the model elements in the project.

Parameters:
project - a project
xmiResource - the resource that will contain the XMI IDs

saveEObjectToResource

public static void saveEObjectToResource(org.eclipse.emf.ecore.EObject eObject,
                                         org.eclipse.emf.common.util.URI resourceURI)
                                  throws IOException
Save an EObject to a resource.

Parameters:
eObject - the object
resourceURI - the resources URI
Throws:
IOException - if saving to the resource fails.

deleteResourcesWithPrefix

public static void deleteResourcesWithPrefix(org.eclipse.emf.ecore.resource.ResourceSet resourceSet,
                                             String prefix)
                                      throws IOException
Deletes all resources from resourceSet, which string representation of URI starts with prefix.

Parameters:
resourceSet - resource set
prefix - string prefix of the resource path
Throws:
IOException - if deleting the resource fails

getProject

public static Project getProject(org.eclipse.emf.ecore.EObject modelElement)
Get Project that contains a model element.

Parameters:
modelElement - the model element
Returns:
the project or null if the element is not contained in a project.

getModelElementId

public static ModelElementId getModelElementId(org.eclipse.emf.ecore.EObject modelElement)
Searches for the project and then looks for the modelelement id.

Parameters:
modelElement - me
Returns:
id

getParent

public static <T extends org.eclipse.emf.ecore.EObject> T getParent(Class<T> parent,
                                                                    org.eclipse.emf.ecore.EObject child)
Get the EContainer that contains the given model element and whose eContainer is null.

Type Parameters:
T - type of the parent class
Parameters:
parent - the class of the parent
child - the model element whose container should get returned
Returns:
the container

isAssociationClassElement

public static boolean isAssociationClassElement(org.eclipse.emf.ecore.EClass eClazz)
Whether a EClass is a association class. Association classes are not displayed as dedicated elements. A link from one element to another which goes over an association class is displayed by a dedicated widget. This widgets allows to trace transparently without seeing the association class.

Parameters:
eClazz - the EClass
Returns:
if it is an association

getAllContainedModelElements

public static Set<org.eclipse.emf.ecore.EObject> getAllContainedModelElements(org.eclipse.emf.ecore.EObject modelElement,
                                                                              boolean includeTransientContainments)
Get all contained elements of a given element.

Parameters:
modelElement - the model element
includeTransientContainments - true if transient containments should be included in the result
Returns:
a set of contained model elements

getAllContainedModelElements

public static Set<org.eclipse.emf.ecore.EObject> getAllContainedModelElements(org.eclipse.emf.ecore.EObject modelElement,
                                                                              boolean includeTransientContainments,
                                                                              boolean ignoreSingletonDatatypes)
Get all contained elements of a given element.

Parameters:
modelElement - the model element
includeTransientContainments - true if transient containments should be included in the result
ignoreSingletonDatatypes - whether to ignore singleton datatypes like, for example, EString
Returns:
a set of contained model elements

getAllContainedModelElements

public static Set<org.eclipse.emf.ecore.EObject> getAllContainedModelElements(org.eclipse.emf.ecore.resource.Resource resource,
                                                                              boolean includeTransientContainments,
                                                                              boolean ignoreSingletonDatatypes)
Get all contained elements of a given resource.

Parameters:
resource - the resource
includeTransientContainments - true if transient containments should be included in the result
ignoreSingletonDatatypes - whether to ignore singleton datatypes like, for example, EString
Returns:
a set of contained model elements Get all

getAllContainedModelElements

public static Set<org.eclipse.emf.ecore.EObject> getAllContainedModelElements(Collection<org.eclipse.emf.ecore.EObject> modelElements,
                                                                              boolean includeTransientContainments,
                                                                              boolean ignoreSingletonDatatypes)
Get all contained elements of a given collection of model elements.

Parameters:
modelElements - a collection of elements
includeTransientContainments - true if transient containments should be included in the result
ignoreSingletonDatatypes - whether to ignore singleton datatypes like, for example, EString
Returns:
a set of contained model elements

getContainerModelElement

public static org.eclipse.emf.ecore.EObject getContainerModelElement(org.eclipse.emf.ecore.EObject modelElement)
Get the container of an EObject.

Parameters:
modelElement - the model element
Returns:
the container

getAllContainedModelElementsAsList

public static List<org.eclipse.emf.ecore.EObject> getAllContainedModelElementsAsList(org.eclipse.emf.ecore.EObject modelElement,
                                                                                     boolean includeTransientContainments)
Get all contained elements of a given element as a list.

Parameters:
modelElement - the model element
includeTransientContainments - true if transient containments should be included in the result
Returns:
a list of contained model elements

deleteIncomingCrossReferencesFromParent

public static void deleteIncomingCrossReferencesFromParent(Collection<org.eclipse.emf.ecore.EStructuralFeature.Setting> inverseReferences,
                                                           org.eclipse.emf.ecore.EObject modelElement)
Delete the given incoming cross references to the given model element from any other model element in the given project.

Parameters:
inverseReferences - a collection of inverse references
modelElement - the model element

deleteOutgoingCrossReferences

public static void deleteOutgoingCrossReferences(IdEObjectCollection collection,
                                                 org.eclipse.emf.ecore.EObject modelElement)
Delete all outgoing cross references of the given model element to any element in the given collection.

Parameters:
collection - the collection
modelElement - the model element

collectOutgoingCrossReferences

public static List<SettingWithReferencedElement> collectOutgoingCrossReferences(IdEObjectCollection collection,
                                                                                Set<org.eclipse.emf.ecore.EObject> modelElements)
Retrieve all outgoing connections from the model elements to other elements in the collection.

Parameters:
collection - the collection
modelElements - the model elements
Returns:
a List of references

shouldBeCollected

public static boolean shouldBeCollected(IdEObjectCollection collection,
                                        Set<org.eclipse.emf.ecore.EObject> allModelElements,
                                        org.eclipse.emf.ecore.EObject referencedElement)
Checks if the referenced elements is an element in the given collection which is not a singleton, not an ignored data type and not already contained in the given set of elements.

Parameters:
collection - the collection
allModelElements - the set of model elements
referencedElement - the referenced element
Returns:
true, if the specified conditions are met.

getSingleton

public static org.eclipse.emf.ecore.EObject getSingleton(ModelElementId singletonId)
Get the singleton instance for a given model element id.

Parameters:
singletonId - the id
Returns:
the singleton instance

getSingletonModelElementId

public static ModelElementId getSingletonModelElementId(org.eclipse.emf.ecore.EObject singleton)
Get the singleton id for a singleton instance.

Parameters:
singleton - the singleton
Returns:
the id
See Also:
ESSingletonIdResolver.getSingletonModelElementId(org.eclipse.emf.ecore.EObject)

isSingleton

public static boolean isSingleton(org.eclipse.emf.ecore.EObject eObject)
Return whether the given eObject instance is a singelton.

Parameters:
eObject - the instance
Returns:
true if it is a singleton
See Also:
ESSingletonIdResolver.isSingleton(org.eclipse.emf.ecore.EObject)


Copyright © 2015. All Rights Reserved.