private void buildSetOfAllUpstreamDependencies(final Set<String> results, final String downstreamDependency) { final Set<String> upstreams = downstreamKeyed.get(downstreamDependency); if (upstreams == null) { return; } for (final String upstream : upstreams) { results.add(upstream); buildSetOfAllUpstreamDependencies(results, upstream); } }
public boolean isValidDependency(final String upstreamDependency, final String downstreamDependency) { Validate.isTrue(MetadataIdentificationUtils.isValid(upstreamDependency), "Upstream dependency is an invalid metadata identification string ('%s')", upstreamDependency); Validate.isTrue(MetadataIdentificationUtils.isValid(downstreamDependency), "Downstream dependency is an invalid metadata identification string ('%s')", downstreamDependency); Validate.isTrue(!upstreamDependency.equals(downstreamDependency), "Upstream dependency cannot be the same as the downstream dependency ('%s')", downstreamDependency); // The simplest possible outcome is the relationship already exists, so // quickly return in that case Set<String> downstream = upstreamKeyed.get(upstreamDependency); if (downstream != null && downstream.contains(downstreamDependency)) { return true; } // Don't need the variable anymore, as we don't care about the other // downstream dependencies downstream = null; // Need to walk the upstream dependency's parent dependency graph, // verifying no presence of the proposed downstream dependency // Need to build a set representing every eventual upstream dependency // of the indicated upstream dependency final Set<String> allUpstreams = new HashSet<String>(); buildSetOfAllUpstreamDependencies(allUpstreams, upstreamDependency); // The dependency is valid if none of the upstreams depend on the // proposed downstream return !allUpstreams.contains(downstreamDependency); }