88public class Vertex <T > implements Comparable <Vertex <T >> {
99 private DirectedGraph graph ;
1010 private T data ;
11- private Set <Edge <T >> incoming = null ;
12- private Set <Edge <T >> outgoing = null ;
11+ private Set <Edge <T >> incoming = Collections . EMPTY_SET ;
12+ private Set <Edge <T >> outgoing = Collections . EMPTY_SET ;
1313 int id ;
1414
1515 public Vertex (DirectedGraph graph , T data , int id ) {
@@ -24,8 +24,8 @@ public void addEdgeTo(Vertex destination) {
2424
2525 public void addEdgeTo (Vertex destination , Object type ) {
2626 Edge edge = new Edge <T >(this , destination , type );
27- getOutgoingEdges (). add (edge );
28- destination .getIncomingEdges (). add (edge );
27+ addOutgoingEdge (edge );
28+ destination .addIncomingEdge (edge );
2929 graph .edges ().add (edge );
3030 }
3131
@@ -42,14 +42,14 @@ public void addEdgeTo(T destination, Object type) {
4242 public boolean removeEdgeTo (Vertex destination ) {
4343 for (Edge edge : getOutgoingEdges ()) {
4444 if (edge .getDestination () == destination ) {
45- getOutgoingEdges (). remove (edge );
46- edge .getDestination ().getIncomingEdges (). remove (edge );
45+ removeOutgoingEdge (edge );
46+ edge .getDestination ().removeIncomingEdge (edge );
4747 graph .edges ().remove (edge );
4848 if (outDegree () == 0 ) {
49- outgoing = null ;
49+ outgoing = Collections . EMPTY_SET ;
5050 }
5151 if (destination .inDegree () == 0 ) {
52- destination .incoming = null ;
52+ destination .incoming = Collections . EMPTY_SET ;
5353 }
5454 return true ;
5555 }
@@ -60,18 +60,18 @@ public boolean removeEdgeTo(Vertex destination) {
6060
6161 public void removeAllIncomingEdges () {
6262 for (Edge edge : getIncomingEdges ()) {
63- edge .getSource ().getOutgoingEdges (). remove (edge );
63+ edge .getSource ().removeOutgoingEdge (edge );
6464 graph .edges ().remove (edge );
6565 }
66- incoming = null ;
66+ incoming = Collections . EMPTY_SET ;
6767 }
6868
6969 public void removeAllOutgoingEdges () {
7070 for (Edge edge : getOutgoingEdges ()) {
71- edge .getDestination ().getIncomingEdges (). remove (edge );
71+ edge .getDestination ().removeIncomingEdge (edge );
7272 graph .edges ().remove (edge );
7373 }
74- outgoing = null ;
74+ outgoing = Collections . EMPTY_SET ;
7575 }
7676
7777 public void removeAllEdges () {
@@ -80,11 +80,11 @@ public void removeAllEdges() {
8080 }
8181
8282 public int inDegree () {
83- return ( incoming == null ) ? 0 : incoming .size ();
83+ return incoming .size ();
8484 }
8585
8686 public int outDegree () {
87- return ( outgoing == null ) ? 0 : outgoing .size ();
87+ return outgoing .size ();
8888 }
8989
9090 public Iterable <Edge <T >> getIncomingEdgesOfType (Object type ) {
@@ -172,13 +172,41 @@ public Edge<T> getOutgoingEdge() {
172172 }
173173
174174 public Set <Edge <T >> getIncomingEdges () {
175- if (incoming == null ) incoming = new HashSet <Edge <T >>();
175+ return incoming ;
176+ }
177+
178+ public void addIncomingEdge (Edge <T > edge ) {
179+ getIncomingEdgesForWrite ().add (edge );
180+ }
181+
182+ public void removeIncomingEdge (Edge <T > edge ) {
183+ if (incoming .size () == 0 ) return ;
184+ incoming .remove (edge );
185+ if (incoming .size () == 0 ) incoming = Collections .EMPTY_SET ;
186+ }
187+
188+ private Set <Edge <T >> getIncomingEdgesForWrite () {
189+ if (incoming == Collections .EMPTY_SET ) incoming = new HashSet <Edge <T >>();
176190
177191 return incoming ;
178192 }
179193
194+ public void addOutgoingEdge (Edge <T > edge ) {
195+ getOutgoingEdgesForWrite ().add (edge );
196+ }
197+
198+ public void removeOutgoingEdge (Edge <T > edge ) {
199+ if (outgoing .size () == 0 ) return ;
200+ outgoing .remove (edge );
201+ if (outgoing .size () == 0 ) outgoing = Collections .EMPTY_SET ;
202+ }
203+
180204 public Set <Edge <T >> getOutgoingEdges () {
181- if (outgoing == null ) outgoing = new HashSet <Edge <T >>();
205+ return outgoing ;
206+ }
207+
208+ private Set <Edge <T >> getOutgoingEdgesForWrite () {
209+ if (outgoing == Collections .EMPTY_SET ) outgoing = new HashSet <Edge <T >>();
182210
183211 return outgoing ;
184212 }
0 commit comments