CombinationOfIntegers { public Set<Set<Integer>> calculate(Set<Integer> originalSet) { validateInputSet(originalSet); Set<Set<Integer>> sets = new HashSet<Set<Integer>>(); if (originalSet.isEmpty()) { sets.add(new HashSet<Integer>()); return sets; } List<Integer> list = new ArrayList<Integer>(originalSet); Integer head = list.get(0); Set<Integer> rest = new HashSet<Integer>(list.subList(1, list.size())); for (Set<Integer> set : calculate(rest)) { Set<Integer> newSet = new HashSet<Integer>(); newSet.add(head); newSet.addAll(set); sets.add(newSet); sets.add(set); } return sets; }  Set<Set<Integer>> calculate(Set<Integer> originalSet);  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullInstancesAsInput() { combinationOfIntegers.calculate(null); }
@Test public void shouldJustContainEmptySetIfInputSetIsEmpty() { Set<Integer> input = new HashSet<Integer>(); Set<Set<Integer>> result = combinationOfIntegers.calculate(input); assertSetContainsSet(result); }
@Test public void shouldCalculateEveryCombination() { Set<Integer> input = new HashSet<Integer>(); input.add(1); input.add(2); Set<Set<Integer>> result = combinationOfIntegers.calculate(input); assertSetContainsSet(result); assertSetContainsSet(result, 1); assertSetContainsSet(result, 2); assertSetContainsSet(result, 1, 2); }