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