001//@formatter:off
002/*
003 * Abstract Enterprise Unit - the COMPOSITE implementing the component interface
004 * Code-Beispiel zum Buch Patterns Kompakt, Verlag Springer Vieweg
005 * Copyright 2014 Karl Eilebrecht
006 * 
007 * Licensed under the Apache License, Version 2.0 (the "License"):
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019//@formatter:on
020package de.calamanari.pk.composite;
021
022import java.util.ArrayList;
023import java.util.Collection;
024import java.util.Collections;
025import java.util.List;
026
027/**
028 * Abstract Enterprise Unit is the abstract base class of the COMPOSITE implementing the interface of the component (EnterpriseNode).
029 * 
030 * @author <a href="mailto:Karl.Eilebrecht(a/t)calamanari.de">Karl Eilebrecht</a>
031 */
032public abstract class AbstractEnterpriseUnit implements EnterpriseNode {
033
034    /**
035     * name of unit
036     */
037    private String name = null;
038
039    /**
040     * this unit's parent node
041     */
042    private AbstractEnterpriseUnit parentUnit = null;
043
044    /**
045     * nodes below this unit
046     */
047    private List<EnterpriseNode> childNodes = new ArrayList<>();
048
049    /**
050     * Creates unit of this name
051     * 
052     * @param name enterprise unit name
053     */
054    public AbstractEnterpriseUnit(String name) {
055        this.name = name;
056    }
057
058    /**
059     * Creates unit, name not set, yet
060     */
061    public AbstractEnterpriseUnit() {
062
063    }
064
065    /**
066     * Sets the unit's name
067     * 
068     * @param name enterprise unit name
069     */
070    public void setName(String name) {
071        this.name = name;
072    }
073
074    /**
075     * sets this unit's parent unit
076     * 
077     * @param parentUnit the owning unit
078     */
079    public void setParent(AbstractEnterpriseUnit parentUnit) {
080        this.parentUnit = parentUnit;
081    }
082
083    /**
084     * Adds the given node to this unit's child nodes.
085     * 
086     * @param childNode NOT NULL
087     */
088    public void addChildNode(EnterpriseNode childNode) {
089        if (!this.childNodes.contains(childNode) && childNode != null) {
090            childNodes.add(childNode);
091        }
092    }
093
094    /**
095     * Removes the given child node from this unit's children.
096     * 
097     * @param childNode dependent node
098     * @return true if the given node was one of this unit's child nodes and has been removed, otherwise false
099     */
100    public boolean remove(EnterpriseNode childNode) {
101        return this.childNodes.remove(childNode);
102    }
103
104    /**
105     * Returns the nodes below this node if any.
106     * 
107     * @return collection of nodes, NEVER null
108     */
109    public Collection<EnterpriseNode> getChildNodes() {
110        return Collections.unmodifiableList(childNodes);
111    }
112
113    @Override
114    public String getName() {
115        return name;
116    }
117
118    @Override
119    public EnterpriseNode getParentNode() {
120        return parentUnit;
121    }
122
123    @Override
124    public String getDescription() {
125        return this.getClass().getSimpleName() + "('" + this.name + "')";
126    }
127}