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
10
11
12
13
14
15
16
17
18
19
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
27 private final ID id;
28
29
30 private Set<Node<?, ?>> nodeSet = new HashSet<>();
31
32
33
34
35
36 public Network(final ID id)
37 {
38 super();
39 this.id = id;
40 }
41
42
43
44
45
46
47 public Network(final ID id, final Collection<? extends L> collection)
48 {
49 super(collection);
50 this.id = id;
51 }
52
53
54
55
56 public final ID getId()
57 {
58 return this.id;
59 }
60
61
62
63
64 public final Set<Node<?, ?>> getNodeSet()
65 {
66 return this.nodeSet;
67 }
68
69
70
71
72 public final void setNodeSet(final Set<Node<?, ?>> nodeSet)
73 {
74 this.nodeSet = nodeSet;
75 }
76
77
78
79
80
81
82 public final boolean isInNetwork(final Node<?, ?> node)
83 {
84 return this.nodeSet.contains(node);
85 }
86
87
88
89
90
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
106
107
108
109
110 @SuppressWarnings("checkstyle:designforextension")
111 public boolean deleteNode(final Node<?, ?> deleteThis) throws NetworkException
112 {
113
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 }