View Javadoc
1   package org.opentrafficsim.core.network;
2   
3   import java.io.Serializable;
4   import java.util.Collection;
5   import java.util.HashSet;
6   import java.util.Set;
7   
8   /**
9    * A Network consists of a set of links. Each link has, in its turn, a start node and an end node.
10   * <p>
11   * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
13   * <p>
14   * @version Aug 19, 2014 <br>
15   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
17   * @author <a href="http://www.citg.tudelft.nl">Guus Tamminga</a>
18   * @param <ID> the ID type of the network.
19   * @param <L>
20   */
21  public class Network<ID, L extends Link<?, ?>> extends HashSet<L> implements Serializable
22  {
23      /** */
24      private static final long serialVersionUID = 20150102L;
25  
26      /** network id. */
27      private final ID id;
28  
29      /** HashSet of Nodes. */
30      private Set<Node<?, ?>> nodeSet = new HashSet<>();
31  
32      /**
33       * Construction of an empty network.
34       * @param id the network id.
35       */
36      public Network(final ID id)
37      {
38          super();
39          this.id = id;
40      }
41  
42      /**
43       * Construction of a network with an initial set of links.
44       * @param id the network id.
45       * @param collection the initial collection of links.
46       */
47      public Network(final ID id, final Collection<? extends L> collection)
48      {
49          super(collection);
50          this.id = id;
51      }
52  
53      /**
54       * @return id
55       */
56      public final ID getId()
57      {
58          return this.id;
59      }
60  
61      /**
62       * @return nodeSet
63       */
64      public final Set<Node<?, ?>> getNodeSet()
65      {
66          return this.nodeSet;
67      }
68  
69      /**
70       * @param nodeSet set nodeSet
71       */
72      public final void setNodeSet(final Set<Node<?, ?>> nodeSet)
73      {
74          this.nodeSet = nodeSet;
75      }
76  
77      /**
78       * Determine if a node is part of this Network.
79       * @param node AbstractNode&lt;?, ?&gt;; the node
80       * @return true or false
81       */
82      public final boolean isInNetwork(final Node<?, ?> node)
83      {
84          return this.nodeSet.contains(node);
85      }
86  
87      /**
88       * Add a node to this Network.
89       * @param node Node; the node that must be added
90       * @throws NetworkException if the node is already part of this network
91       */
92      public final void addNode(final Node<?, ?> node) throws NetworkException
93      {
94          if (isInNetwork(node))
95          {
96              throw new NetworkException("Adding Node " + node.getId().toString() + ". This Node is  already in the Set");
97          }
98          else
99          {
100             this.nodeSet.add(node);
101         }
102     }
103 
104     /**
105      * Delete a node from this network.
106      * @param deleteThis AbstractNode&lt;?, ?&gt;; the node that must be deleted
107      * @return boolean
108      * @throws NetworkException on network inconsistency Note: method can be overridden, e.g. by the ExpansionNetwork.
109      */
110     @SuppressWarnings("checkstyle:designforextension")
111     public boolean deleteNode(final Node<?, ?> deleteThis) throws NetworkException
112     {
113         // TODO ensure that no links are orphaned due to removal of the node
114         if (isInNetwork(deleteThis))
115         {
116             this.nodeSet.remove(deleteThis);
117             return true;
118         }
119         throw new NetworkException("Deleting" + deleteThis.getId().toString() + " failed. Possible cause:"
120             + " node is not a member of the given Network");
121     }
122 }