[New] Added Activity.addElementAfter() and .addElementBefore()
This commit is contained in:
parent
42ed0ea801
commit
61451c5a1a
|
@ -166,28 +166,89 @@ public class Activity extends AbstractStrolchRootElement
|
||||||
*
|
*
|
||||||
* @param activityElement
|
* @param activityElement
|
||||||
* the element to add
|
* the element to add
|
||||||
*
|
|
||||||
* @return the element added
|
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
public void addElement(IActivityElement activityElement) {
|
||||||
public <T extends IActivityElement> T addElement(IActivityElement activityElement) {
|
assertCanAdd(activityElement);
|
||||||
|
activityElement.setParent(this);
|
||||||
|
this.elements.put(activityElement.getId(), activityElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* add an activity element to the {@code LinkedHashMap} of {@code IActivityElements} before the given element
|
||||||
|
*
|
||||||
|
* @param element
|
||||||
|
* the element before which to add the other element
|
||||||
|
* @param elementToAdd
|
||||||
|
* the element to add
|
||||||
|
*/
|
||||||
|
public void addElementBefore(IActivityElement element, IActivityElement elementToAdd) {
|
||||||
|
assertCanAdd(elementToAdd);
|
||||||
|
|
||||||
|
Iterator<Entry<String, IActivityElement>> iterator = this.elements.entrySet().iterator();
|
||||||
|
LinkedHashMap<String, IActivityElement> elements = new LinkedHashMap<>();
|
||||||
|
boolean added = false;
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
Entry<String, IActivityElement> next = iterator.next();
|
||||||
|
|
||||||
|
if (!added && next.getValue().equals(element)) {
|
||||||
|
elements.put(elementToAdd.getId(), elementToAdd);
|
||||||
|
elements.put(next.getKey(), next.getValue());
|
||||||
|
added = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
elements.put(next.getKey(), next.getValue());
|
||||||
|
}
|
||||||
|
if (!added)
|
||||||
|
throw new IllegalStateException("Element " + element.getId() + " was not found, couldn't add before!");
|
||||||
|
|
||||||
|
elementToAdd.setParent(this);
|
||||||
|
this.elements = elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* add an activity element to the {@code LinkedHashMap} of {@code IActivityElements} after the given element
|
||||||
|
*
|
||||||
|
* @param element
|
||||||
|
* the element before which to add the other element
|
||||||
|
* @param elementToAdd
|
||||||
|
* the element to add
|
||||||
|
*/
|
||||||
|
public void addElementAfter(IActivityElement element, IActivityElement elementToAdd) {
|
||||||
|
assertCanAdd(elementToAdd);
|
||||||
|
|
||||||
|
Iterator<Entry<String, IActivityElement>> iterator = this.elements.entrySet().iterator();
|
||||||
|
LinkedHashMap<String, IActivityElement> elements = new LinkedHashMap<>();
|
||||||
|
boolean added = false;
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
Entry<String, IActivityElement> next = iterator.next();
|
||||||
|
elements.put(next.getKey(), next.getValue());
|
||||||
|
|
||||||
|
if (!added && next.getValue().equals(element)) {
|
||||||
|
elements.put(elementToAdd.getId(), elementToAdd);
|
||||||
|
added = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!added)
|
||||||
|
throw new IllegalStateException("Element " + element.getId() + " was not found, couldn't add after!");
|
||||||
|
|
||||||
|
elementToAdd.setParent(this);
|
||||||
|
this.elements = elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertCanAdd(IActivityElement elementToAdd) {
|
||||||
assertNotReadonly();
|
assertNotReadonly();
|
||||||
DBC.PRE.assertNotEquals("Can't add element to itself!", this, activityElement);
|
DBC.PRE.assertNotEquals("Can't add element to itself!", this, elementToAdd);
|
||||||
DBC.PRE.assertNull("Parent can't already be set!", activityElement.getParent());
|
DBC.PRE.assertNull("Parent can't already be set!", elementToAdd.getParent());
|
||||||
|
|
||||||
// TODO make sure we can't create a circular dependency
|
// TODO make sure we can't create a circular dependency
|
||||||
|
|
||||||
initElements();
|
initElements();
|
||||||
String id = activityElement.getId();
|
String id = elementToAdd.getId();
|
||||||
if (id == null)
|
if (id == null)
|
||||||
throw new StrolchException("Cannot add IActivityElement without id.");
|
throw new StrolchException("Cannot add IActivityElement without id.");
|
||||||
else if (this.elements.containsKey(id))
|
if (this.elements.containsKey(id))
|
||||||
throw new StrolchException(
|
throw new StrolchException(
|
||||||
"Activiy " + getLocator() + " already contains an activity element with id = " + id);
|
"Activity " + getLocator() + " already contains an activity element with id = " + id);
|
||||||
else {
|
|
||||||
activityElement.setParent(this);
|
|
||||||
return (T) this.elements.put(activityElement.getId(), activityElement);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -701,7 +762,6 @@ public class Activity extends AbstractStrolchRootElement
|
||||||
return findParameter(BAG_RELATIONS, paramKey, assertExists);
|
return findParameter(BAG_RELATIONS, paramKey, assertExists);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
@Override
|
||||||
public <U, T extends Parameter<U>> T findParameter(String bagKey, String paramKey) {
|
public <U, T extends Parameter<U>> T findParameter(String bagKey, String paramKey) {
|
||||||
|
|
||||||
|
@ -710,7 +770,7 @@ public class Activity extends AbstractStrolchRootElement
|
||||||
return parameter;
|
return parameter;
|
||||||
|
|
||||||
if (this.parent != null)
|
if (this.parent != null)
|
||||||
return (T) this.parent.findParameter(bagKey, paramKey);
|
return this.parent.findParameter(bagKey, paramKey);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2016 Robert von Burg <eitch@eitchnet.ch>
|
* Copyright 2016 Robert von Burg <eitch@eitchnet.ch>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@ -26,7 +26,9 @@ import javax.xml.transform.TransformerFactory;
|
||||||
import javax.xml.transform.dom.DOMSource;
|
import javax.xml.transform.dom.DOMSource;
|
||||||
import javax.xml.transform.stream.StreamResult;
|
import javax.xml.transform.stream.StreamResult;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import li.strolch.exception.StrolchException;
|
import li.strolch.exception.StrolchException;
|
||||||
|
@ -248,6 +250,48 @@ public class ActivityTest {
|
||||||
assertFalse(nextElement.isPresent());
|
assertFalse(nextElement.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddAfter() {
|
||||||
|
Activity a = new Activity("bla", "Bla", "Bla", TimeOrdering.SERIES);
|
||||||
|
|
||||||
|
Action a0 = new Action("a0", "A0", "Wait");
|
||||||
|
a.addElement(a0);
|
||||||
|
Action a1 = new Action("a1", "A1", "Consume");
|
||||||
|
a.addElement(a1);
|
||||||
|
Action a2 = new Action("a2", "A2", "Consume");
|
||||||
|
a.addElement(a2);
|
||||||
|
|
||||||
|
Action a3 = new Action("a3", "A3", "Produce");
|
||||||
|
a.addElementAfter(a0, a3);
|
||||||
|
|
||||||
|
Iterator<Map.Entry<String, IActivityElement>> iter = a.elementIterator();
|
||||||
|
assertEquals(a0, iter.next().getValue());
|
||||||
|
assertEquals(a3, iter.next().getValue());
|
||||||
|
assertEquals(a1, iter.next().getValue());
|
||||||
|
assertEquals(a2, iter.next().getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddBefore() {
|
||||||
|
Activity a = new Activity("bla", "Bla", "Bla", TimeOrdering.SERIES);
|
||||||
|
|
||||||
|
Action a0 = new Action("a0", "A0", "Wait");
|
||||||
|
a.addElement(a0);
|
||||||
|
Action a1 = new Action("a1", "A1", "Consume");
|
||||||
|
a.addElement(a1);
|
||||||
|
Action a2 = new Action("a2", "A2", "Consume");
|
||||||
|
a.addElement(a2);
|
||||||
|
|
||||||
|
Action a3 = new Action("a3", "A3", "Produce");
|
||||||
|
a.addElementBefore(a1, a3);
|
||||||
|
|
||||||
|
Iterator<Map.Entry<String, IActivityElement>> iter = a.elementIterator();
|
||||||
|
assertEquals(a0, iter.next().getValue());
|
||||||
|
assertEquals(a3, iter.next().getValue());
|
||||||
|
assertEquals(a1, iter.next().getValue());
|
||||||
|
assertEquals(a2, iter.next().getValue());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testActionsWithState() {
|
public void testActionsWithState() {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue