Skip to content
Snippets Groups Projects
Commit 07f85e92 authored by Luca Cristoforetti's avatar Luca Cristoforetti
Browse files

Initial proposal of new API

parent ffe3da25
No related branches found
No related tags found
No related merge requests found
package org.polarsys.chess.contracts.profile.chesscontract.ModelUtil;
public interface Filter {
public boolean test(Object o);
}
package org.polarsys.chess.contracts.profile.chesscontract.ModelUtil;
import java.util.ArrayList;
import java.util.Collection;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import eu.fbk.eclipse.standardtools.utils.core.utils.EObjectUtil;
public class ModelAccessUtil {
public static Collection<EObject> getElementChildren(EObject element, boolean recursive, Filter filter) throws Exception {
Collection<EObject> rootcontent = null;
if (element != null) {
if (recursive) {
TreeIterator<EObject> allElements = element.eAllContents();
if (allElements != null) {
rootcontent = EObjectUtil.iterator2Collection(allElements);
}
} else {
rootcontent = element.eContents();
}
if (filter != null) {
Collection<EObject> filtered = new ArrayList<EObject>();
for (EObject object : rootcontent) {
if (filter.test(object)) {
filtered.add(object);
}
}
return filtered;
} else {
return rootcontent;
}
}
throw new Exception("Root element does not exist.");
}
public static Collection<EObject> getElementOwners(EObject element, boolean recursive, Filter filter) {
//TODO: implement this with eContainer()
return null;
}
public void deleteElement(EObject element) {
//TODO: implement this
}
}
package org.polarsys.chess.contracts.profile.chesscontract.ModelUtil;
import java.util.ArrayList;
import java.util.Collection;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Type;
public class UmlElementAccessUtil {
// Solution based on ECore
public Collection<EObject> getEnumerations(Package owner, boolean recursive, Filter filter) throws Exception {
Collection<EObject> enumerations = ModelAccessUtil.getElementChildren(owner, recursive, new EnumerationsFilter());
return applyFilterOnList(filter, enumerations);
}
// Solution based on UML API
public Collection<EObject> getEnumerationsUML(Package owner, boolean recursive, Filter filter) throws Exception {
final Collection<EObject> enumerations = new ArrayList<EObject>();
if (recursive) {
final EList<Element> elements = owner.allOwnedElements();
for (Element element : elements) {
if (element instanceof Enumeration) {
enumerations.add(element);
}
}
} else {
final EList<Type> types = owner.getOwnedTypes();
for (Type type : types) {
if (type instanceof Enumeration) {
enumerations.add((Enumeration) type);
}
}
}
return applyFilterOnList(filter, enumerations);
}
// Solution based on UML API
public Operation getOperation(Class owner, String name, boolean ignoreCase, Filter filter) {
final Operation operation = owner.getOperation(name, null, null, ignoreCase);
return applyFilterOnElement(operation, filter);
}
private Collection<EObject> applyFilterOnList(Filter filter, final Collection<EObject> elements) {
if (filter != null) {
Collection<EObject> filtered = new ArrayList<EObject>();
for (EObject eObject : elements) {
if (filter.test(eObject)) {
filtered.add(eObject);
}
}
return filtered;
}
return elements;
}
private <T> T applyFilterOnElement(T element, Filter filter) {
if (filter != null) {
if (filter.test(element)) {
return element;
}
return null;
}
return element;
}
public boolean isEnumeration(Element enumeration) {
return (applyFilterOnElement(enumeration, new EnumerationsFilter()) != null);
}
}
class EnumerationsFilter implements Filter {
@Override
public boolean test(Object o) {
if (o instanceof Enumeration) {
return true;
}
return false;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment