OPENJPA-304. Changes to DepthFirstAnalysis and associated classes and testcases to resolve IBM JDK and Sun JDK differences.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@561363 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Kevin W. Sutter 2007-07-31 15:43:39 +00:00
parent 2bc4c4a552
commit 872d00888d
4 changed files with 38 additions and 35 deletions

View File

@ -216,13 +216,13 @@ public class DepthFirstAnalysis {
List cycle; List cycle;
int pos = 0; int pos = 0;
if (path != null && edge.getFrom() != edge.getTo()) { if (path != null && !edge.getFrom().equals(edge.getTo())) {
// Not a single edge loop // Not a single edge loop
pos = findNodeInPath(edge.getTo(), path); pos = findNodeInPath(edge.getTo(), path);
assert (pos >= 0): _loc.get("node-not-on-path", edge, edge.getTo()); assert (pos >= 0): _loc.get("node-not-on-path", edge, edge.getTo());
} else { } else {
assert (edge.getFrom() == edge.getTo()): assert (edge.getFrom().equals(edge.getTo())):
_loc.get("edge-no-loop", edge).getMessage(); _loc.get("edge-no-loop", edge).getMessage();
path = null; path = null;
} }
cycle = buildCycle(edge, path, pos); cycle = buildCycle(edge, path, pos);
@ -249,8 +249,8 @@ public class DepthFirstAnalysis {
Edge edge = (Edge) itr.next(); Edge edge = (Edge) itr.next();
Object other = edge.getOther(node); Object other = edge.getOther(node);
// Single edge loops are ignored // Single edge loops are ignored
if (node != other) { if (!node.equals(other)) {
if (other == cycleTo) { if (other.equals(cycleTo)) {
// Cycle complete // Cycle complete
path.add(edge); path.add(edge);
found = true; found = true;
@ -279,7 +279,7 @@ public class DepthFirstAnalysis {
int pos = -1; int pos = -1;
if (path != null) { if (path != null) {
for (int i = 0; i < path.size(); i++) { for (int i = 0; i < path.size(); i++) {
if (((Edge)path.get(i)).getFrom() == node) { if (((Edge)path.get(i)).getFrom().equals(node)) {
pos = i; pos = i;
} }
} }

View File

@ -106,9 +106,9 @@ public class Edge {
* given node is not part of this edge. * given node is not part of this edge.
*/ */
public Object getOther(Object node) { public Object getOther(Object node) {
if (_to == node) if (_to.equals(node))
return _from; return _from;
if (_from == node) if (_from.equals(node))
return _to; return _to;
return null; return null;
} }
@ -118,7 +118,7 @@ public class Edge {
* this method returns true if either side is equal to the given node. * this method returns true if either side is equal to the given node.
*/ */
public boolean isTo(Object node) { public boolean isTo(Object node) {
return _to == node || (!_directed && _from == node); return _to.equals(node) || (!_directed && _from.equals(node));
} }
/** /**
@ -127,7 +127,7 @@ public class Edge {
* node. * node.
*/ */
public boolean isFrom(Object node) { public boolean isFrom(Object node) {
return _from == node || (!_directed && _to == node); return _from.equals(node) || (!_directed && _to.equals(node));
} }
/** /**

View File

@ -21,9 +21,9 @@ package org.apache.openjpa.lib.graph;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
/** /**
@ -36,8 +36,11 @@ import java.util.Map;
*/ */
public class Graph { public class Graph {
// map each node to list of edges from that node /**
private final Map _nodes = new HashMap(); * Map each node to list of edges from that node.
* Using a LinkedHashMap to ensure order of iterator processing.
*/
private final Map _nodes = new LinkedHashMap();
/** /**
* Clear the graph. * Clear the graph.

View File

@ -58,23 +58,23 @@ public class TestDepthFirstAnalysis
public void setUpGraph2() { public void setUpGraph2() {
Graph graph = new Graph(); Graph graph = new Graph();
Integer node1 = new Integer(5); Integer node1 = new Integer(1);
Integer node2 = new Integer(4); Integer node2 = new Integer(2);
Integer node3 = new Integer(3); Integer node3 = new Integer(3);
Integer node4 = new Integer(2); Integer node4 = new Integer(4);
Integer node5 = new Integer(1); Integer node5 = new Integer(5);
graph.addNode(node1); graph.addNode(node2); // has to be first node for testcase
graph.addNode(node2);
graph.addNode(node3);
graph.addNode(node4);
graph.addNode(node5); graph.addNode(node5);
graph.addEdge(new Edge(node1, node2, true)); graph.addNode(node4);
graph.addEdge(new Edge(node2, node3, true)); graph.addNode(node3);
graph.addNode(node1);
graph.addEdge(new Edge(node5, node4, true));
graph.addEdge(new Edge(node4, node3, true));
graph.addEdge(new Edge(node3, node3, true)); graph.addEdge(new Edge(node3, node3, true));
graph.addEdge(new Edge(node3, node4, true)); graph.addEdge(new Edge(node3, node2, true));
graph.addEdge(new Edge(node4, node1, true)); graph.addEdge(new Edge(node2, node5, true));
graph.addEdge(new Edge(node4, node2, true)); graph.addEdge(new Edge(node2, node4, true));
graph.addEdge(new Edge(node5, node2, true)); graph.addEdge(new Edge(node1, node4, true));
_dfa = new DepthFirstAnalysis(graph); _dfa = new DepthFirstAnalysis(graph);
} }
@ -97,8 +97,8 @@ public class TestDepthFirstAnalysis
Iterator itr = edges.iterator(); Iterator itr = edges.iterator();
Edge edge0 = (Edge) itr.next(); Edge edge0 = (Edge) itr.next();
Edge edge1 = (Edge) itr.next(); Edge edge1 = (Edge) itr.next();
assertTrue((edge0.getTo() == edge0.getFrom()) assertTrue((edge0.getTo().equals(edge0.getFrom()))
|| edge1.getTo() == edge1.getFrom()); || edge1.getTo().equals(edge1.getFrom()));
} }
public void testBackEdges() { public void testBackEdges() {
@ -108,17 +108,17 @@ public class TestDepthFirstAnalysis
Iterator itr = edges.iterator(); Iterator itr = edges.iterator();
Edge edge0 = (Edge) itr.next(); Edge edge0 = (Edge) itr.next();
Edge edge1 = (Edge) itr.next(); Edge edge1 = (Edge) itr.next();
if (edge0.getTo() == edge0.getFrom()) { if (edge0.getTo().equals(edge0.getFrom())) {
assertTrue(edge0.getCycle() != null && edge0.getCycle().size() == 1); assertTrue(edge0.getCycle() != null && edge0.getCycle().size() == 1);
List cycle = edge1.getCycle(); List cycle = edge1.getCycle();
assertTrue(cycle != null && cycle.size() == 4); assertTrue(cycle != null && cycle.size() == 4);
assertTrue(((Edge)cycle.get(0)).getFrom() == ((Edge)cycle.get(3)).getTo()); assertTrue(((Edge)cycle.get(0)).getFrom().equals(((Edge)cycle.get(3)).getTo()));
} else if (edge1.getTo() == edge1.getFrom()) { } else if (edge1.getTo().equals(edge1.getFrom())) {
assertTrue(edge1.getCycle() != null && edge1.getCycle().size() == 1); assertTrue(edge1.getCycle() != null && edge1.getCycle().size() == 1);
assertTrue(edge1 == edge1.getCycle()); assertTrue(edge1 == edge1.getCycle());
List cycle = edge0.getCycle(); List cycle = edge0.getCycle();
assertTrue(cycle != null && cycle.size() == 4); assertTrue(cycle != null && cycle.size() == 4);
assertTrue(((Edge)cycle.get(0)).getFrom() == ((Edge)cycle.get(3)).getTo()); assertTrue(((Edge)cycle.get(0)).getFrom().equals(((Edge)cycle.get(3)).getTo()));
} else { } else {
// should not happen // should not happen
assertFalse(true); assertFalse(true);
@ -135,11 +135,11 @@ public class TestDepthFirstAnalysis
if (edge0.getCycle() == null) { if (edge0.getCycle() == null) {
List cycle = edge1.getCycle(); List cycle = edge1.getCycle();
assertTrue(cycle != null && cycle.size() == 3); assertTrue(cycle != null && cycle.size() == 3);
assertTrue(((Edge)cycle.get(0)).getFrom() == ((Edge)cycle.get(2)).getTo()); assertTrue(((Edge)cycle.get(0)).getFrom().equals(((Edge)cycle.get(2)).getTo()));
} else if (edge1.getCycle() == null) { } else if (edge1.getCycle() == null) {
List cycle = edge0.getCycle(); List cycle = edge0.getCycle();
assertTrue(cycle != null && cycle.size() == 3); assertTrue(cycle != null && cycle.size() == 3);
assertTrue(((Edge)cycle.get(0)).getFrom() == ((Edge)cycle.get(2)).getTo()); assertTrue(((Edge)cycle.get(0)).getFrom().equals(((Edge)cycle.get(2)).getTo()));
} else { } else {
// should not happen // should not happen
assertFalse(true); assertFalse(true);