From 59317c93fbb33c525eff6af00d749beb6aa00dd0 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Thu, 24 Jun 2010 00:17:13 +0000 Subject: [PATCH] [HUDSON-6819] tweaking the implementation a bit so that the code never ever have multiple edges between the same src and dst. Not that I was able to spot where the previous code did that, but this implementation is bit more straightforward. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@32256 71c3de6d-444a-0410-be80-ed276b4c234a --- .../java/hudson/model/DependencyGraph.java | 43 ++++++++----------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/core/src/main/java/hudson/model/DependencyGraph.java b/core/src/main/java/hudson/model/DependencyGraph.java index 280a4e1eab..6e14cc334c 100644 --- a/core/src/main/java/hudson/model/DependencyGraph.java +++ b/core/src/main/java/hudson/model/DependencyGraph.java @@ -83,8 +83,8 @@ import java.awt.image.BufferedImage; */ public final class DependencyGraph implements Comparator { - private Map> forward = new HashMap>(); - private Map> backward = new HashMap>(); + private Map> forward = new HashMap>(); + private Map> backward = new HashMap>(); private boolean built; @@ -136,8 +136,8 @@ public final class DependencyGraph implements Comparator { return get(backward,p,true); } - private List get(Map> map, AbstractProject src, boolean up) { - List v = map.get(src); + private List get(Map> map, AbstractProject src, boolean up) { + List v = map.get(src); if(v==null) return Collections.emptyList(); List result = new ArrayList(v.size()); for (Dependency d : v) result.add(up ? d.getUpstreamProject() : d.getDownstreamProject()); @@ -158,9 +158,9 @@ public final class DependencyGraph implements Comparator { return get(backward,p); } - private List get(Map> map, AbstractProject src) { - List v = map.get(src); - if(v!=null) return v; + private List get(Map> map, AbstractProject src) { + List v = map.get(src); + if(v!=null) return Collections.unmodifiableList(v); else return Collections.emptyList(); } @@ -250,7 +250,7 @@ public final class DependencyGraph implements Comparator { return getTransitive(forward,src,false); } - private Set getTransitive(Map> direction, AbstractProject src, boolean up) { + private Set getTransitive(Map> direction, AbstractProject src, boolean up) { Set visited = new HashSet(); Stack queue = new Stack(); @@ -268,32 +268,26 @@ public final class DependencyGraph implements Comparator { return visited; } - private void add(Map> map, AbstractProject key, Dependency dep) { - List set = map.get(key); + private void add(Map> map, AbstractProject key, Dependency dep) { + List set = map.get(key); if(set==null) { - set = new ArrayList(); + set = new ArrayList(); map.put(key,set); } - for (ListIterator it = set.listIterator(); it.hasNext();) { - Dependency d = it.next(); + for (ListIterator it = set.listIterator(); it.hasNext();) { + DependencyGroup d = it.next(); // Check for existing edge that connects the same two projects: if (d.getUpstreamProject()==dep.getUpstreamProject() && d.getDownstreamProject()==dep.getDownstreamProject()) { - if (d.equals(dep)) - return; // identical with existing edge - - if (d instanceof DependencyGroup) - ((DependencyGroup)d).add(dep); - else - it.set(new DependencyGroup(d,dep)); + d.add(dep); return; } } // Otherwise add to list: - set.add(dep); + set.add(new DependencyGroup(dep)); } - private Map> finalize(Map> m) { - for (Entry> e : m.entrySet()) { + private Map> finalize(Map> m) { + for (Entry> e : m.entrySet()) { Collections.sort( e.getValue(), NAME_COMPARATOR ); e.setValue( Collections.unmodifiableList(e.getValue()) ); } @@ -480,10 +474,9 @@ public final class DependencyGraph implements Comparator { private static class DependencyGroup extends Dependency { private Set group = new LinkedHashSet(); - DependencyGroup(Dependency first, Dependency second) { + DependencyGroup(Dependency first) { super(first.getUpstreamProject(), first.getDownstreamProject()); group.add(first); - group.add(second); } void add(Dependency next) { -- GitLab