Skip to content

Instantly share code, notes, and snippets.

@adamvduke
Created January 19, 2011 18:14
Show Gist options
  • Save adamvduke/786573 to your computer and use it in GitHub Desktop.
Save adamvduke/786573 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class Dependencies {
Map <String, Collection <String>> dependencyMap = new HashMap <String, Collection <String>>();
public void addDirect( String target, String... deps ) {
dependencyMap.put( target, Arrays.asList( deps ) );
}
public Collection <String> depsFor( String string ) {
return depsFor( string, new ArrayList <String>() );
}
private Collection <String> depsFor( String target, ArrayList <String> transitive ) {
Collection <String> directDependencies = directDependendcies( target );
if ( directDependencies == null ) {
return transitive;
}
for ( String dep : directDependencies ) {
if ( !transitive.contains( dep ) ) {
transitive.add( dep );
transitive.addAll( depsFor( dep, transitive ) );
}
}
return transitive;
}
private Collection <String> directDependendcies( String target ) {
Collection <String> directDependencies = dependencyMap.get( target );
return directDependencies;
}
public static boolean hasEquivalentDeps( Collection <String> left, Collection <String> right ) {
if ( left.containsAll( right ) && right.containsAll( left ) ) {
return true;
}
return false;
}
}
import java.util.Arrays;
import junit.framework.Assert;
import org.junit.Test;
public class WhenCalculatingDependencies {
@Test
public void shouldBeAbleToRememberFirstLevel() {
Dependencies dependencies = new Dependencies();
dependencies.addDirect( "A", "B", "C" );
Assert.assertTrue( Dependencies.hasEquivalentDeps( Arrays.asList( "B", "C" ), dependencies.depsFor( "A" ) ) );
dependencies.addDirect( "D", "C", "E" );
Assert.assertTrue( Dependencies.hasEquivalentDeps( Arrays.asList( "C", "E" ), dependencies.depsFor( "D" ) ) );
}
@Test
public void shouldNotCareAboutOrder() {
Dependencies dependencies = new Dependencies();
dependencies.addDirect( "A", "B", "C" );
Assert.assertTrue( Dependencies.hasEquivalentDeps( Arrays.asList( "C", "B" ), dependencies.depsFor( "A" ) ) );
}
// case sensitivity
@Test
public void shouldBeAbleToCalculateTransitiveDependencies_2levels() throws Exception {
Dependencies dependencies = new Dependencies();
dependencies.addDirect( "A", "B", "C" );
dependencies.addDirect( "B", "C", "E" );
Assert.assertTrue( "B, C, E for A", Dependencies.hasEquivalentDeps( Arrays.asList( "B", "C", "E" ), dependencies.depsFor( "A" ) ) );
}
@Test
public void shouldBeAbleToCalculateTransitiveDependencies_3levels() throws Exception {
Dependencies dependencies = new Dependencies();
dependencies.addDirect( "A", "B", "C" );
dependencies.addDirect( "B", "C", "E" );
dependencies.addDirect( "E", "G" );
Assert.assertTrue( "B, C, E, G for A", Dependencies.hasEquivalentDeps( Arrays.asList( "B", "C", "E", "G" ), dependencies.depsFor( "A" ) ) );
}
@Test
public void shouldBeAbleToCalculateTransitiveDependencies_Nlevels() throws Exception {
Dependencies dependencies = new Dependencies();
dependencies.addDirect( "A", "B", "C" );
dependencies.addDirect( "B", "C", "E" );
dependencies.addDirect( "C", "G" );
dependencies.addDirect( "D", "A", "F" );
dependencies.addDirect( "E", "F" );
dependencies.addDirect( "F", "H" );
Assert.assertTrue( Dependencies.hasEquivalentDeps( Arrays.asList( "B", "C", "E", "F", "G", "H" ), dependencies.depsFor( "A" ) ) );
Assert.assertTrue( Dependencies.hasEquivalentDeps( Arrays.asList( "C", "E", "F", "G", "H" ), dependencies.depsFor( "B" ) ) );
Assert.assertTrue( Dependencies.hasEquivalentDeps( Arrays.asList( "G" ), dependencies.depsFor( "C" ) ) );
Assert.assertTrue( Dependencies.hasEquivalentDeps( Arrays.asList( "A", "B", "C", "E", "F", "G", "H" ), dependencies.depsFor( "D" ) ) );
Assert.assertTrue( Dependencies.hasEquivalentDeps( Arrays.asList( "F", "H" ), dependencies.depsFor( "E" ) ) );
Assert.assertTrue( Dependencies.hasEquivalentDeps( Arrays.asList( "H" ), dependencies.depsFor( "F" ) ) );
}
@Test
public void shouldBeAbleToHandleCircularDependencies_level0() throws Exception {
Dependencies dependencies = new Dependencies();
dependencies.addDirect( "a", "a" );
Assert.assertTrue( Dependencies.hasEquivalentDeps( Arrays.asList( "a" ), dependencies.depsFor( "a" ) ) );
}
@Test
public void shouldBeAbleToHandleCircularDependencies_level1() throws Exception {
Dependencies dependencies = new Dependencies();
dependencies.addDirect( "a", "b" );
dependencies.addDirect( "b", "a" );
Assert.assertTrue( Dependencies.hasEquivalentDeps( Arrays.asList( "a", "b" ), dependencies.depsFor( "a" ) ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment