diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/IOrderLineGroup.java b/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/IOrderLineGroup.java
index 19b7e9ebf..8bd284a54 100644
--- a/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/IOrderLineGroup.java
+++ b/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/IOrderLineGroup.java
@@ -20,23 +20,13 @@
package org.navalplanner.business.orders.entities;
+import org.navalplanner.business.trees.ITreeNode;
+
/**
* Container of {@link OrderElement}.
*
* @author Óscar González Fernández
*/
-public interface IOrderLineGroup {
+public interface IOrderLineGroup extends ITreeNode {
- public void add(OrderElement orderElement);
-
- public void remove(OrderElement orderElement);
-
- public void replace(OrderElement oldOrderElement,
- OrderElement newOrderElement);
-
- public void up(OrderElement orderElement);
-
- public void down(OrderElement orderElement);
-
- public void add(int position, OrderElement orderElement);
}
\ No newline at end of file
diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/OrderLineGroupManipulator.java b/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/OrderLineGroupManipulator.java
index 2df6f266a..266db0b7b 100644
--- a/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/OrderLineGroupManipulator.java
+++ b/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/OrderLineGroupManipulator.java
@@ -20,15 +20,16 @@
package org.navalplanner.business.orders.entities;
-import java.util.Collections;
import java.util.List;
+import org.navalplanner.business.trees.TreeNodeOnList;
/**
* Implementation of {@link IOrderLineGroup}.
* @author Óscar González Fernández
*/
-public class OrderLineGroupManipulator implements IOrderLineGroup {
+public class OrderLineGroupManipulator extends
+ TreeNodeOnList {
public static OrderLineGroupManipulator createManipulatorForOrder(
List orderElements) {
@@ -40,79 +41,40 @@ public class OrderLineGroupManipulator implements IOrderLineGroup {
return new OrderLineGroupManipulator(group, children);
}
- private final List orderElements;
- private final OrderLineGroup parent;
-
private OrderLineGroupManipulator(OrderLineGroup parent,
List orderElements) {
- this.parent = parent;
- this.orderElements = orderElements;
+ super(parent, orderElements);
+ }
+
+ protected void setParentIfRequired(OrderElement orderElement) {
+ if (getParent() != null) {
+ orderElement.setParent(getParent());
+ }
}
@Override
- public void add(OrderElement orderElement) {
- setParentIfRequired(orderElement);
- orderElements.add(orderElement);
- addSchedulingStateToParent(orderElement);
- }
-
- private void setParentIfRequired(OrderElement orderElement) {
- if (this.parent != null) {
- orderElement.setParent(this.parent);
- }
+ protected void onChildAdded(OrderElement newChild) {
+ addSchedulingStateToParent(newChild);
}
private void addSchedulingStateToParent(OrderElement orderElement) {
- if (this.parent != null) {
+ final OrderLineGroup parent = getParent();
+ if (parent != null) {
SchedulingState schedulingState = orderElement.getSchedulingState();
removeSchedulingStateFromParent(orderElement);
- this.parent.getSchedulingState().add(schedulingState);
+ parent.getSchedulingState().add(schedulingState);
}
}
+ @Override
+ protected void onChildRemoved(OrderElement previousChild) {
+ removeSchedulingStateFromParent(previousChild);
+ }
+
private void removeSchedulingStateFromParent(OrderElement orderElement) {
SchedulingState schedulingState = orderElement.getSchedulingState();
if (!schedulingState.isRoot()) {
schedulingState.getParent().removeChild(schedulingState);
}
}
-
- @Override
- public void remove(OrderElement orderElement) {
- orderElements.remove(orderElement);
- removeSchedulingStateFromParent(orderElement);
- }
-
- @Override
- public void replace(OrderElement oldOrderElement, OrderElement orderElement) {
- setParentIfRequired(orderElement);
- Collections.replaceAll(orderElements, oldOrderElement, orderElement);
- addSchedulingStateToParent(orderElement);
- }
-
- @Override
- public void down(OrderElement orderElement) {
- int position = orderElements.indexOf(orderElement);
- if (position < orderElements.size() - 1) {
- orderElements.remove(position);
- orderElements.add(position + 1, orderElement);
- }
- }
-
- @Override
- public void up(OrderElement orderElement) {
- int position = orderElements.indexOf(orderElement);
- if (position > 0) {
- orderElements.remove(position);
- orderElements.add(position - 1, orderElement);
- }
- }
-
- @Override
- public void add(int position, OrderElement orderElement) {
- setParentIfRequired(orderElement);
- orderElements.add(position, orderElement);
- addSchedulingStateToParent(orderElement);
- }
-
-}
+}
\ No newline at end of file
diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/trees/ITreeNode.java b/navalplanner-business/src/main/java/org/navalplanner/business/trees/ITreeNode.java
new file mode 100644
index 000000000..d4ca373a8
--- /dev/null
+++ b/navalplanner-business/src/main/java/org/navalplanner/business/trees/ITreeNode.java
@@ -0,0 +1,41 @@
+/*
+ * This file is part of ###PROJECT_NAME###
+ *
+ * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
+ * Desenvolvemento Tecnolóxico de Galicia
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ */
+package org.navalplanner.business.trees;
+
+
+/**
+ * Represents an entity that can work as a node at a tree
+ * @author Óscar González Fernández
+ */
+public interface ITreeNode {
+
+ public void add(T newChild);
+
+ public void remove(T existentChild);
+
+ public void replace(T previousChild, T newChild);
+
+ public void up(T existentChild);
+
+ public void down(T existentChild);
+
+ public void add(int position, T newChild);
+
+}
diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/trees/TreeNodeOnList.java b/navalplanner-business/src/main/java/org/navalplanner/business/trees/TreeNodeOnList.java
new file mode 100644
index 000000000..e1e479b90
--- /dev/null
+++ b/navalplanner-business/src/main/java/org/navalplanner/business/trees/TreeNodeOnList.java
@@ -0,0 +1,95 @@
+/*
+ * This file is part of ###PROJECT_NAME###
+ *
+ * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
+ * Desenvolvemento Tecnolóxico de Galicia
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ */
+package org.navalplanner.business.trees;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Implementation of {@link ITreeNode} that mutates a list
+ * @author Óscar González Fernández
+ */
+public abstract class TreeNodeOnList implements ITreeNode {
+
+ private final List children;
+ private final P parent;
+
+ protected TreeNodeOnList(P parent, List children) {
+ this.parent = parent;
+ this.children = children;
+ }
+
+ @Override
+ public void add(T newChild) {
+ setParentIfRequired(newChild);
+ children.add(newChild);
+ onChildAdded(newChild);
+ }
+
+ protected P getParent() {
+ return parent;
+ }
+
+ protected abstract void setParentIfRequired(T newChild);
+
+ protected abstract void onChildAdded(T newChild);
+
+ protected abstract void onChildRemoved(T previousChild);
+
+ @Override
+ public void remove(T previousChild) {
+ children.remove(previousChild);
+ onChildRemoved(previousChild);
+ }
+
+ @Override
+ public void replace(T previousChild, T newChild) {
+ setParentIfRequired(newChild);
+ Collections.replaceAll(children, previousChild, newChild);
+ onChildAdded(newChild);
+ onChildRemoved(previousChild);
+ }
+
+ @Override
+ public void down(T existentChild) {
+ int position = children.indexOf(existentChild);
+ if (position < children.size() - 1) {
+ children.remove(position);
+ children.add(position + 1, existentChild);
+ }
+ }
+
+ @Override
+ public void up(T existentChild) {
+ int position = children.indexOf(existentChild);
+ if (position > 0) {
+ children.remove(position);
+ children.add(position - 1, existentChild);
+ }
+ }
+
+ @Override
+ public void add(int position, T newChild) {
+ setParentIfRequired(newChild);
+ children.add(position, newChild);
+ onChildAdded(newChild);
+ }
+
+}