ItEr41S20CUGravacionModelosUnidadesTraballoItEr40S25: Creating interface ITreeNode and default implementation.
OrderLineGroupManipulator implements this interface
This commit is contained in:
parent
4b29ed1591
commit
4d0c4ebfb1
4 changed files with 160 additions and 72 deletions
|
|
@ -20,23 +20,13 @@
|
|||
|
||||
package org.navalplanner.business.orders.entities;
|
||||
|
||||
import org.navalplanner.business.trees.ITreeNode;
|
||||
|
||||
/**
|
||||
* Container of {@link OrderElement}. <br />
|
||||
*
|
||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||
*/
|
||||
public interface IOrderLineGroup {
|
||||
public interface IOrderLineGroup extends ITreeNode<OrderElement> {
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
@ -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}. <br />
|
||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||
*/
|
||||
public class OrderLineGroupManipulator implements IOrderLineGroup {
|
||||
public class OrderLineGroupManipulator extends
|
||||
TreeNodeOnList<OrderElement, OrderLineGroup> {
|
||||
|
||||
public static OrderLineGroupManipulator createManipulatorForOrder(
|
||||
List<OrderElement> orderElements) {
|
||||
|
|
@ -40,79 +41,40 @@ public class OrderLineGroupManipulator implements IOrderLineGroup {
|
|||
return new OrderLineGroupManipulator(group, children);
|
||||
}
|
||||
|
||||
private final List<OrderElement> orderElements;
|
||||
private final OrderLineGroup parent;
|
||||
|
||||
private OrderLineGroupManipulator(OrderLineGroup parent,
|
||||
List<OrderElement> 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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.navalplanner.business.trees;
|
||||
|
||||
|
||||
/**
|
||||
* Represents an entity that can work as a node at a tree<br />
|
||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||
*/
|
||||
public interface ITreeNode<T> {
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.navalplanner.business.trees;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Implementation of {@link ITreeNode} that mutates a list <br />
|
||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||
*/
|
||||
public abstract class TreeNodeOnList<T, P> implements ITreeNode<T> {
|
||||
|
||||
private final List<T> children;
|
||||
private final P parent;
|
||||
|
||||
protected TreeNodeOnList(P parent, List<T> 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);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue