Skip to content
Snippets Groups Projects
Commit aada6752 authored by Simon Spinner's avatar Simon Spinner
Browse files

Add a Status flag to distinguish between CREATE, EXISTS and REMOVE.

parent 5327e74e
No related branches found
No related tags found
No related merge requests found
......@@ -10,13 +10,23 @@ import org.eclipse.emf.ecore.EStructuralFeature;
public abstract class ModelTemplate<P extends EObject, E extends EObject> {
public enum Status {
CREATE, EXISTS, REMOVE
}
private final EStructuralFeature container;
private final Class<E> elementType;
private TemplateContext context;
private TemplateContext context;
private final Status status;
protected ModelTemplate(EStructuralFeature container, Class<E> elementType) {
public ModelTemplate(EStructuralFeature container, Class<E> elementType) {
this(Status.CREATE, container, elementType);
}
public ModelTemplate(Status status, EStructuralFeature container, Class<E> elementType) {
this.container = container;
this.elementType = elementType;
this.status = status;
}
public void setContext(TemplateContext ctx) {
......@@ -32,34 +42,44 @@ public abstract class ModelTemplate<P extends EObject, E extends EObject> {
public abstract boolean matches(E object);
protected E matchesWithCast(Object object) {
if (elementType.isAssignableFrom(object.getClass())) {
E casted = elementType.cast(object);
if (matches(casted)) {
return casted;
}
}
return null;
}
public E find(P parent) {
if (parent == null) {
return null;
}
if (container.isMany()) {
return find((Collection<?>)parent.eGet(container));
return find((List<?>)parent.eGet(container), 0);
} else {
Object value = parent.eGet(container);
if (value != null) {
if (elementType.isAssignableFrom(value.getClass())) {
E casted = elementType.cast(value);
if (matches(casted)) {
return casted;
}
E casted = matchesWithCast(value);
if (casted != null) {
return casted;
}
}
return null;
}
}
protected E find(Collection<?> list) {
for (Object cur : list) {
if (elementType.isAssignableFrom(cur.getClass())) {
E casted = elementType.cast(cur);
if (matches(casted)) {
return casted;
}
protected E find(List<?> list) {
return find(list, 0);
}
protected E find(List<?> list, int startIdx) {
for (int i = startIdx; i < list.size(); i++) {
E casted = matchesWithCast(list.get(i));
if (casted != null) {
return casted;
}
}
return null;
......@@ -86,20 +106,28 @@ public abstract class ModelTemplate<P extends EObject, E extends EObject> {
public void apply(P parent) {
E current = get(parent);
for(ModelTemplate<? super E, ?> cmd : getChildCommands()) {
cmd.apply(current);
if (current != null) {
for(ModelTemplate<? super E, ?> cmd : getChildCommands()) {
cmd.apply(current);
}
}
}
public E get(P parent) {
E ret = find(parent);
if (ret == null) {
if ((status == Status.CREATE) && (ret == null)) {
ret = create(parent);
insert(parent, ret);
if (ret != null) {
insert(parent, ret);
}
}
return ret;
}
public Status getStatus() {
return status;
}
protected Class<E> getElementType() {
return elementType;
}
......
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