Line | Hits | Source |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2003, the JUNG Project and the Regents of the University | |
3 | * of California | |
4 | * All rights reserved. | |
5 | * | |
6 | * This software is open-source under the BSD license; see either | |
7 | * "license.txt" or | |
8 | * http://jung.sourceforge.net/license.txt for a description. | |
9 | * | |
10 | * Created on Mar 8, 2004 | |
11 | */ | |
12 | package edu.uci.ics.jung.graph.predicates; | |
13 | ||
14 | import java.util.Iterator; | |
15 | import java.util.Set; | |
16 | ||
17 | import edu.uci.ics.jung.graph.ArchetypeEdge; | |
18 | import edu.uci.ics.jung.graph.DirectedEdge; | |
19 | import edu.uci.ics.jung.graph.Edge; | |
20 | import edu.uci.ics.jung.graph.UndirectedEdge; | |
21 | import edu.uci.ics.jung.graph.Vertex; | |
22 | import edu.uci.ics.jung.utils.Pair; | |
23 | ||
24 | /** | |
25 | * <p>A predicate that checks to see whether the specified edge | |
26 | * is parallel to any other edge. A negation of this predicate | |
27 | * may be used as an edge constraint that will prevent the | |
28 | * constrained graph from accepting parallel edges. This | |
29 | * predicate is probably not appropriate for use as a subset | |
30 | * specification.</p> | |
31 | * | |
32 | * <p>Two distinct edges are considered to be <i>parallel</i> to one another | |
33 | * if the following conditions hold: | |
34 | * <ul> | |
35 | * <li/>the edges are both directed or both undirected | |
36 | * <li/>if undirected, the incident vertex sets for each edge are the same | |
37 | * <li/>if directed, the edges have the same source vertex and the same | |
38 | * destination vertex | |
39 | * </ul> | |
40 | * </p> | |
41 | * | |
42 | * @author Joshua O'Madadhain | |
43 | */ | |
44 | public class ParallelEdgePredicate extends EdgePredicate | |
45 | { | |
46 | private static ParallelEdgePredicate instance; | |
47 | private static final String message = "ParallelEdgePredicate"; | |
48 | ||
49 | protected ParallelEdgePredicate() | |
50 | { | |
51 | 68 | super(); |
52 | 68 | } |
53 | ||
54 | public static ParallelEdgePredicate getInstance() | |
55 | { | |
56 | 211 | if (instance == null) |
57 | 68 | instance = new ParallelEdgePredicate(); |
58 | 211 | return instance; |
59 | } | |
60 | ||
61 | public String toString() | |
62 | { | |
63 | 0 | return message; |
64 | } | |
65 | ||
66 | /** | |
67 | * <p>Returns <code>true</code> if there exists an | |
68 | * edge which is parallel to the specified edge.</p> | |
69 | * | |
70 | * @see Vertex#findEdgeSet(Vertex) | |
71 | */ | |
72 | public boolean evaluateEdge(ArchetypeEdge ae) | |
73 | { | |
74 | 147870 | Edge e = (Edge)ae; |
75 | 147870 | Pair endpoints = e.getEndpoints(); |
76 | 147870 | Vertex u = (Vertex)(endpoints.getFirst()); |
77 | 147870 | Vertex v = (Vertex)(endpoints.getSecond()); |
78 | 147870 | Set s = u.findEdgeSet(v); |
79 | 147870 | if (isDirected(e)) |
80 | 3717 | return evaluateDirectedEdge((DirectedEdge)e, s.iterator()); |
81 | else | |
82 | 144153 | return evaluateUndirectedEdge((UndirectedEdge)e, s.iterator()); |
83 | } | |
84 | ||
85 | protected boolean evaluateDirectedEdge(DirectedEdge de, Iterator s_iter) | |
86 | { | |
87 | 3752 | while (s_iter.hasNext()) |
88 | { | |
89 | 48 | Edge f = (Edge)s_iter.next(); |
90 | 48 | if (de != f && isDirected(f)) |
91 | { | |
92 | 13 | DirectedEdge df = (DirectedEdge)f; |
93 | 13 | if ((df.getSource() == de.getSource()) && |
94 | (df.getDest() == de.getDest())) | |
95 | 13 | return true; |
96 | } | |
97 | } | |
98 | 3704 | return false; |
99 | } | |
100 | ||
101 | protected boolean evaluateUndirectedEdge(UndirectedEdge ue, Iterator s_iter) | |
102 | { | |
103 | 144203 | while (s_iter.hasNext()) |
104 | { | |
105 | 57 | Edge f = (Edge)s_iter.next(); |
106 | 57 | if (ue != f && !isDirected(f)) |
107 | { | |
108 | 7 | if (f.getIncidentVertices().equals(ue.getIncidentVertices())) |
109 | 7 | return true; |
110 | } | |
111 | } | |
112 | 144146 | return false; |
113 | } | |
114 | ||
115 | protected boolean isDirected(Edge e) | |
116 | { | |
117 | 147901 | if (e instanceof DirectedEdge) |
118 | 3737 | return true; |
119 | 144164 | else if (e instanceof UndirectedEdge) |
120 | 144164 | return false; |
121 | else | |
122 | 0 | throw new IllegalArgumentException(e + "is neither directed nor undirected"); |
123 | } | |
124 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |