1 package edu.uci.ics.jung.graph.util;
2
3 import java.io.Serializable;
4 import java.util.Collection;
5
6 import edu.uci.ics.jung.graph.DirectedGraph;
7 import edu.uci.ics.jung.graph.Forest;
8 import edu.uci.ics.jung.graph.Graph;
9 import edu.uci.ics.jung.graph.Tree;
10 import edu.uci.ics.jung.graph.UndirectedGraph;
11
12
13
14
15
16
17
18
19
20
21 public class Graphs {
22
23
24
25
26
27
28
29
30 public static <V,E> Graph<V,E> synchronizedGraph(Graph<V,E> graph) {
31 return new SynchronizedGraph<V,E>(graph);
32 }
33
34
35
36
37
38
39
40
41 public static <V,E> DirectedGraph<V,E> synchronizedDirectedGraph(DirectedGraph<V,E> graph) {
42 return new SynchronizedDirectedGraph<V,E>(graph);
43 }
44
45
46
47
48
49
50
51
52 public static <V,E> UndirectedGraph<V,E> synchronizedUndirectedGraph(UndirectedGraph<V,E> graph) {
53 return new SynchronizedUndirectedGraph<V,E>(graph);
54 }
55
56
57
58
59
60
61
62
63 public static <V,E> SynchronizedForest<V,E> synchronizedForest(Forest<V,E> forest) {
64 return new SynchronizedForest<V,E>(forest);
65 }
66
67
68
69
70
71
72
73
74 public static <V,E> SynchronizedTree<V,E> synchronizedTree(Tree<V,E> tree) {
75 return new SynchronizedTree<V,E>(tree);
76 }
77
78
79
80
81
82
83
84
85 public static <V,E> Graph<V,E> unmodifiableGraph(Graph<V,E> graph) {
86 return new UnmodifiableGraph<V,E>(graph);
87 }
88
89
90
91
92
93
94
95
96 public static <V,E> DirectedGraph<V,E> unmodifiableDirectedGraph(DirectedGraph<V,E> graph) {
97 return new UnmodifiableDirectedGraph<V,E>(graph);
98 }
99
100
101
102
103
104
105
106
107 public static <V,E> UndirectedGraph<V,E> unmodifiableUndirectedGraph(UndirectedGraph<V,E> graph) {
108 return new UnmodifiableUndirectedGraph<V,E>(graph);
109 }
110
111
112
113
114
115
116
117
118 public static <V,E> UnmodifiableTree<V,E> unmodifiableTree(Tree<V,E> tree) {
119 return new UnmodifiableTree<V,E>(tree);
120 }
121
122
123
124
125
126
127
128
129 public static <V,E> UnmodifiableForest<V,E> unmodifiableForest(Forest<V,E> forest) {
130 return new UnmodifiableForest<V,E>(forest);
131 }
132
133
134 @SuppressWarnings("serial")
135 static abstract class SynchronizedAbstractGraph<V,E> implements Graph<V,E>, Serializable {
136 protected Graph<V,E> delegate;
137
138 private SynchronizedAbstractGraph(Graph<V, E> delegate) {
139 if(delegate == null) {
140 throw new NullPointerException();
141 }
142 this.delegate = delegate;
143 }
144
145
146
147
148 public EdgeType getDefaultEdgeType()
149 {
150 return delegate.getDefaultEdgeType();
151 }
152
153
154
155
156 public synchronized boolean addEdge(E e, V v1, V v2, EdgeType edgeType) {
157 return delegate.addEdge(e, v1, v2, edgeType);
158 }
159
160
161
162
163 public synchronized boolean addEdge(E e, Collection<? extends V>
164 vertices, EdgeType edgeType)
165 {
166 return delegate.addEdge(e, vertices, edgeType);
167 }
168
169
170
171
172 public synchronized boolean addEdge(E e, V v1, V v2) {
173 return delegate.addEdge(e, v1, v2);
174 }
175
176
177
178
179 public synchronized boolean addVertex(V vertex) {
180 return delegate.addVertex(vertex);
181 }
182
183
184
185
186 public synchronized boolean isIncident(V vertex, E edge) {
187 return delegate.isIncident(vertex, edge);
188 }
189
190
191
192
193 public synchronized boolean isNeighbor(V v1, V v2) {
194 return delegate.isNeighbor(v1, v2);
195 }
196
197
198
199
200 public synchronized int degree(V vertex) {
201 return delegate.degree(vertex);
202 }
203
204
205
206
207 public synchronized E findEdge(V v1, V v2) {
208 return delegate.findEdge(v1, v2);
209 }
210
211
212
213
214 public synchronized Collection<E> findEdgeSet(V v1, V v2)
215 {
216 return delegate.findEdgeSet(v1, v2);
217 }
218
219
220
221
222 public synchronized Collection<E> getEdges() {
223 return delegate.getEdges();
224 }
225
226
227
228
229 public synchronized Collection<E> getEdges(EdgeType edgeType) {
230 return delegate.getEdges(edgeType);
231 }
232
233
234
235
236 public synchronized Pair<V> getEndpoints(E edge) {
237 return delegate.getEndpoints(edge);
238 }
239
240
241
242
243 public synchronized Collection<E> getIncidentEdges(V vertex) {
244 return delegate.getIncidentEdges(vertex);
245 }
246
247
248
249
250 public synchronized Collection<V> getIncidentVertices(E edge) {
251 return delegate.getIncidentVertices(edge);
252 }
253
254
255
256
257 public synchronized Collection<E> getInEdges(V vertex) {
258 return delegate.getInEdges(vertex);
259 }
260
261
262
263
264 public synchronized Collection<V> getNeighbors(V vertex) {
265 return delegate.getNeighbors(vertex);
266 }
267
268
269
270
271 public synchronized V getOpposite(V vertex, E edge) {
272 return delegate.getOpposite(vertex, edge);
273 }
274
275
276
277
278 public synchronized Collection<E> getOutEdges(V vertex) {
279 return delegate.getOutEdges(vertex);
280 }
281
282
283
284
285 public synchronized Collection<V> getPredecessors(V vertex) {
286 return delegate.getPredecessors(vertex);
287 }
288
289
290
291
292 public synchronized Collection<V> getSuccessors(V vertex) {
293 return delegate.getSuccessors(vertex);
294 }
295
296
297
298
299 public synchronized Collection<V> getVertices() {
300 return delegate.getVertices();
301 }
302
303
304
305
306 public synchronized int getEdgeCount() {
307 return delegate.getEdgeCount();
308 }
309
310
311
312
313 public synchronized int getEdgeCount(EdgeType edge_type)
314 {
315 return delegate.getEdgeCount(edge_type);
316 }
317
318
319
320
321 public synchronized int getVertexCount() {
322 return delegate.getVertexCount();
323 }
324
325
326
327
328 public synchronized int inDegree(V vertex) {
329 return delegate.inDegree(vertex);
330 }
331
332
333
334
335 public synchronized EdgeType getEdgeType(E edge) {
336 return delegate.getEdgeType(edge);
337 }
338
339
340
341
342 public synchronized boolean isPredecessor(V v1, V v2) {
343 return delegate.isPredecessor(v1, v2);
344 }
345
346
347
348
349 public synchronized boolean isSuccessor(V v1, V v2) {
350 return delegate.isSuccessor(v1, v2);
351 }
352
353
354
355
356 public synchronized int getNeighborCount(V vertex) {
357 return delegate.getNeighborCount(vertex);
358 }
359
360
361
362
363 public synchronized int getPredecessorCount(V vertex) {
364 return delegate.getPredecessorCount(vertex);
365 }
366
367
368
369
370 public synchronized int getSuccessorCount(V vertex) {
371 return delegate.getSuccessorCount(vertex);
372 }
373
374
375
376
377 public synchronized int outDegree(V vertex) {
378 return delegate.outDegree(vertex);
379 }
380
381
382
383
384 public synchronized boolean removeEdge(E edge) {
385 return delegate.removeEdge(edge);
386 }
387
388
389
390
391 public synchronized boolean removeVertex(V vertex) {
392 return delegate.removeVertex(vertex);
393 }
394
395
396
397
398 public synchronized V getDest(E directed_edge) {
399 return delegate.getDest(directed_edge);
400 }
401
402
403
404
405 public synchronized V getSource(E directed_edge) {
406 return delegate.getSource(directed_edge);
407 }
408
409
410
411
412 public synchronized boolean isDest(V vertex, E edge) {
413 return delegate.isDest(vertex, edge);
414 }
415
416
417
418
419 public synchronized boolean isSource(V vertex, E edge) {
420 return delegate.isSource(vertex, edge);
421 }
422
423
424
425
426 public synchronized int getIncidentCount(E edge)
427 {
428 return delegate.getIncidentCount(edge);
429 }
430
431
432
433
434 public synchronized boolean addEdge(E hyperedge, Collection<? extends V> vertices) {
435 return delegate.addEdge(hyperedge, vertices);
436 }
437
438
439
440
441 public synchronized boolean containsEdge(E edge) {
442 return delegate.containsEdge(edge);
443 }
444
445
446
447
448 public synchronized boolean containsVertex(V vertex) {
449 return delegate.containsVertex(vertex);
450 }
451
452 }
453
454 @SuppressWarnings("serial")
455 static class SynchronizedGraph<V,E> extends SynchronizedAbstractGraph<V,E> implements Serializable {
456
457 private SynchronizedGraph(Graph<V,E> delegate) {
458 super(delegate);
459 }
460 }
461
462 @SuppressWarnings("serial")
463 static class SynchronizedUndirectedGraph<V,E> extends SynchronizedAbstractGraph<V,E>
464 implements UndirectedGraph<V,E>, Serializable {
465 private SynchronizedUndirectedGraph(UndirectedGraph<V,E> delegate) {
466 super(delegate);
467 }
468 }
469
470 @SuppressWarnings("serial")
471 static class SynchronizedDirectedGraph<V,E> extends SynchronizedAbstractGraph<V,E>
472 implements DirectedGraph<V,E>, Serializable {
473
474 private SynchronizedDirectedGraph(DirectedGraph<V,E> delegate) {
475 super(delegate);
476 }
477
478 @Override
479 public synchronized V getDest(E directed_edge) {
480 return ((DirectedGraph<V,E>)delegate).getDest(directed_edge);
481 }
482
483 @Override
484 public synchronized V getSource(E directed_edge) {
485 return ((DirectedGraph<V,E>)delegate).getSource(directed_edge);
486 }
487
488 @Override
489 public synchronized boolean isDest(V vertex, E edge) {
490 return ((DirectedGraph<V,E>)delegate).isDest(vertex, edge);
491 }
492
493 @Override
494 public synchronized boolean isSource(V vertex, E edge) {
495 return ((DirectedGraph<V,E>)delegate).isSource(vertex, edge);
496 }
497 }
498
499 @SuppressWarnings("serial")
500 static class SynchronizedTree<V,E> extends SynchronizedForest<V,E> implements Tree<V,E> {
501
502
503
504
505
506 public SynchronizedTree(Tree<V, E> delegate) {
507 super(delegate);
508 }
509
510 public synchronized int getDepth(V vertex) {
511 return ((Tree<V,E>)delegate).getDepth(vertex);
512 }
513
514 public synchronized int getHeight() {
515 return ((Tree<V,E>)delegate).getHeight();
516 }
517
518 public synchronized V getRoot() {
519 return ((Tree<V,E>)delegate).getRoot();
520 }
521 }
522
523 @SuppressWarnings("serial")
524 static class SynchronizedForest<V,E> extends SynchronizedDirectedGraph<V,E> implements Forest<V,E> {
525
526
527
528
529
530 public SynchronizedForest(Forest<V, E> delegate) {
531 super(delegate);
532 }
533
534 public synchronized Collection<Tree<V, E>> getTrees() {
535 return ((Forest<V,E>)delegate).getTrees();
536 }
537
538 public int getChildCount(V vertex)
539 {
540 return ((Forest<V,E>)delegate).getChildCount(vertex);
541 }
542
543 public Collection<E> getChildEdges(V vertex)
544 {
545 return ((Forest<V,E>)delegate).getChildEdges(vertex);
546 }
547
548 public Collection<V> getChildren(V vertex)
549 {
550 return ((Forest<V,E>)delegate).getChildren(vertex);
551 }
552
553 public V getParent(V vertex)
554 {
555 return ((Forest<V,E>)delegate).getParent(vertex);
556 }
557
558 public E getParentEdge(V vertex)
559 {
560 return ((Forest<V,E>)delegate).getParentEdge(vertex);
561 }
562 }
563
564 @SuppressWarnings("serial")
565 static abstract class UnmodifiableAbstractGraph<V,E> implements Graph<V,E>, Serializable {
566 protected Graph<V,E> delegate;
567
568
569 private UnmodifiableAbstractGraph(Graph<V, E> delegate) {
570 if(delegate == null) {
571 throw new NullPointerException();
572 }
573 this.delegate = delegate;
574 }
575
576
577
578
579 public EdgeType getDefaultEdgeType()
580 {
581 return delegate.getDefaultEdgeType();
582 }
583
584
585
586
587 public boolean addEdge(E e, V v1, V v2, EdgeType edgeType) {
588 throw new UnsupportedOperationException();
589 }
590
591
592
593
594 public boolean addEdge(E e, Collection<? extends V> vertices,
595 EdgeType edgeType)
596 {
597 throw new UnsupportedOperationException();
598 }
599
600
601
602
603 public boolean addEdge(E e, V v1, V v2) {
604 throw new UnsupportedOperationException();
605 }
606
607
608
609
610 public boolean addVertex(V vertex) {
611 throw new UnsupportedOperationException();
612 }
613
614
615
616
617 public boolean isIncident(V vertex, E edge) {
618 return delegate.isIncident(vertex, edge);
619 }
620
621
622
623
624 public boolean isNeighbor(V v1, V v2) {
625 return delegate.isNeighbor(v1, v2);
626 }
627
628
629
630
631 public int degree(V vertex) {
632 return delegate.degree(vertex);
633 }
634
635
636
637
638 public E findEdge(V v1, V v2) {
639 return delegate.findEdge(v1, v2);
640 }
641
642
643
644
645 public Collection<E> findEdgeSet(V v1, V v2)
646 {
647 return delegate.findEdgeSet(v1, v2);
648 }
649
650
651
652
653 public Collection<E> getEdges() {
654 return delegate.getEdges();
655 }
656
657
658
659
660 public int getEdgeCount() {
661 return delegate.getEdgeCount();
662 }
663
664
665
666
667 public int getEdgeCount(EdgeType edge_type)
668 {
669 return delegate.getEdgeCount(edge_type);
670 }
671
672
673
674
675 public int getVertexCount() {
676 return delegate.getVertexCount();
677 }
678
679
680
681
682 public Collection<E> getEdges(EdgeType edgeType) {
683 return delegate.getEdges(edgeType);
684 }
685
686
687
688
689 public Pair<V> getEndpoints(E edge) {
690 return delegate.getEndpoints(edge);
691 }
692
693
694
695
696 public Collection<E> getIncidentEdges(V vertex) {
697 return delegate.getIncidentEdges(vertex);
698 }
699
700
701
702
703 public Collection<V> getIncidentVertices(E edge) {
704 return delegate.getIncidentVertices(edge);
705 }
706
707
708
709
710 public Collection<E> getInEdges(V vertex) {
711 return delegate.getInEdges(vertex);
712 }
713
714
715
716
717 public Collection<V> getNeighbors(V vertex) {
718 return delegate.getNeighbors(vertex);
719 }
720
721
722
723
724 public V getOpposite(V vertex, E edge) {
725 return delegate.getOpposite(vertex, edge);
726 }
727
728
729
730
731 public Collection<E> getOutEdges(V vertex) {
732 return delegate.getOutEdges(vertex);
733 }
734
735
736
737
738 public Collection<V> getPredecessors(V vertex) {
739 return delegate.getPredecessors(vertex);
740 }
741
742
743
744
745 public Collection<V> getSuccessors(V vertex) {
746 return delegate.getSuccessors(vertex);
747 }
748
749
750
751
752 public Collection<V> getVertices() {
753 return delegate.getVertices();
754 }
755
756
757
758
759 public int inDegree(V vertex) {
760 return delegate.inDegree(vertex);
761 }
762
763
764
765
766 public EdgeType getEdgeType(E edge) {
767 return delegate.getEdgeType(edge);
768 }
769
770
771
772
773 public boolean isPredecessor(V v1, V v2) {
774 return delegate.isPredecessor(v1, v2);
775 }
776
777
778
779
780 public boolean isSuccessor(V v1, V v2) {
781 return delegate.isSuccessor(v1, v2);
782 }
783
784
785
786
787 public int getNeighborCount(V vertex) {
788 return delegate.getNeighborCount(vertex);
789 }
790
791
792
793
794 public int getPredecessorCount(V vertex) {
795 return delegate.getPredecessorCount(vertex);
796 }
797
798
799
800
801 public int getSuccessorCount(V vertex) {
802 return delegate.getSuccessorCount(vertex);
803 }
804
805
806
807
808 public int outDegree(V vertex) {
809 return delegate.outDegree(vertex);
810 }
811
812
813
814
815 public boolean removeEdge(E edge) {
816 throw new UnsupportedOperationException();
817 }
818
819
820
821
822 public boolean removeVertex(V vertex) {
823 throw new UnsupportedOperationException();
824 }
825
826
827
828
829 public V getDest(E directed_edge) {
830 return delegate.getDest(directed_edge);
831 }
832
833
834
835
836 public V getSource(E directed_edge) {
837 return delegate.getSource(directed_edge);
838 }
839
840
841
842
843 public boolean isDest(V vertex, E edge) {
844 return delegate.isDest(vertex, edge);
845 }
846
847
848
849
850 public boolean isSource(V vertex, E edge) {
851 return delegate.isSource(vertex, edge);
852 }
853
854
855
856
857 public int getIncidentCount(E edge)
858 {
859 return delegate.getIncidentCount(edge);
860 }
861
862
863
864
865 public boolean addEdge(E hyperedge, Collection<? extends V> vertices) {
866 return delegate.addEdge(hyperedge, vertices);
867 }
868
869
870
871
872 public boolean containsEdge(E edge) {
873 return delegate.containsEdge(edge);
874 }
875
876
877
878
879 public boolean containsVertex(V vertex) {
880 return delegate.containsVertex(vertex);
881 }
882 }
883
884 @SuppressWarnings("serial")
885 static class UnmodifiableGraph<V,E> extends UnmodifiableAbstractGraph<V,E> implements Serializable {
886 private UnmodifiableGraph(Graph<V,E> delegate) {
887 super(delegate);
888 }
889 }
890
891 @SuppressWarnings("serial")
892 static class UnmodifiableDirectedGraph<V,E> extends UnmodifiableAbstractGraph<V,E>
893 implements DirectedGraph<V,E>, Serializable {
894 private UnmodifiableDirectedGraph(DirectedGraph<V,E> delegate) {
895 super(delegate);
896 }
897
898 @Override
899 public V getDest(E directed_edge) {
900 return ((DirectedGraph<V,E>)delegate).getDest(directed_edge);
901 }
902
903 @Override
904 public V getSource(E directed_edge) {
905 return ((DirectedGraph<V,E>)delegate).getSource(directed_edge);
906 }
907
908 @Override
909 public boolean isDest(V vertex, E edge) {
910 return ((DirectedGraph<V,E>)delegate).isDest(vertex, edge);
911 }
912
913 @Override
914 public boolean isSource(V vertex, E edge) {
915 return ((DirectedGraph<V,E>)delegate).isSource(vertex, edge);
916 }
917 }
918
919 @SuppressWarnings("serial")
920 static class UnmodifiableUndirectedGraph<V,E> extends UnmodifiableAbstractGraph<V,E>
921 implements UndirectedGraph<V,E>, Serializable {
922 private UnmodifiableUndirectedGraph(UndirectedGraph<V,E> delegate) {
923 super(delegate);
924 }
925 }
926
927 @SuppressWarnings("serial")
928 static class UnmodifiableForest<V,E> extends UnmodifiableGraph<V,E>
929 implements Forest<V,E>, Serializable {
930 private UnmodifiableForest(Forest<V,E> delegate) {
931 super(delegate);
932 }
933
934 public Collection<Tree<V, E>> getTrees() {
935 return ((Forest<V,E>)delegate).getTrees();
936 }
937
938 public int getChildCount(V vertex)
939 {
940 return ((Forest<V,E>)delegate).getChildCount(vertex);
941 }
942
943 public Collection<E> getChildEdges(V vertex)
944 {
945 return ((Forest<V,E>)delegate).getChildEdges(vertex);
946 }
947
948 public Collection<V> getChildren(V vertex)
949 {
950 return ((Forest<V,E>)delegate).getChildren(vertex);
951 }
952
953 public V getParent(V vertex)
954 {
955 return ((Forest<V,E>)delegate).getParent(vertex);
956 }
957
958 public E getParentEdge(V vertex)
959 {
960 return ((Forest<V,E>)delegate).getParentEdge(vertex);
961 }
962 }
963
964 @SuppressWarnings("serial")
965 static class UnmodifiableTree<V,E> extends UnmodifiableForest<V,E>
966 implements Tree<V,E>, Serializable {
967 private UnmodifiableTree(Tree<V,E> delegate) {
968 super(delegate);
969 }
970
971 public int getDepth(V vertex) {
972 return ((Tree<V,E>)delegate).getDepth(vertex);
973 }
974
975 public int getHeight() {
976 return ((Tree<V,E>)delegate).getHeight();
977 }
978
979 public V getRoot() {
980 return ((Tree<V,E>)delegate).getRoot();
981 }
982
983 @Override
984 public Collection<Tree<V, E>> getTrees() {
985 return ((Tree<V,E>)delegate).getTrees();
986 }
987 }
988
989 }