### Question:
CombinationGeneratorFlexible { @Nonnull @ReturnsMutableCopy public ICommonsSet <ICommonsList <DATATYPE>> getCombinations (@Nonnull final ICommonsList <DATATYPE> aElements) { ValueEnforcer.notNull (aElements, "Elements"); final ICommonsSet <ICommonsList <DATATYPE>> aAllResults = new CommonsHashSet <> (); iterateAllCombinations (aElements, aAllResults::add); return aAllResults; } CombinationGeneratorFlexible(@Nonnegative final int nSlotCount, final boolean bAllowEmpty); void iterateAllCombinations(@Nonnull final ICommonsList <DATATYPE> aElements,
                                      @Nonnull final Consumer <? super ICommonsList <DATATYPE>> aCallback); @Nonnull @ReturnsMutableCopy ICommonsSet <ICommonsList <DATATYPE>> getCombinations(@Nonnull final ICommonsList <DATATYPE> aElements); static void iterateAllCombinations(@Nonnull final ICommonsList <DATATYPE> aElements,
                                                        final boolean bAllowEmpty,
                                                        @Nonnull final Consumer <? super ICommonsList <DATATYPE>> aCallback); @Nonnull @ReturnsMutableCopy static ICommonsSet <ICommonsList <DATATYPE>> getCombinations(@Nonnull final ICommonsList <DATATYPE> aElements,
                                                                                  final boolean bAllowEmpty);  }### Answer:
@Test @SuppressFBWarnings ("TQ_NEVER_VALUE_USED_WHERE_ALWAYS_REQUIRED") public void testStringCombination () { final ICommonsList <String> aElements = new CommonsArrayList <> ("A", "B", "B"); CombinationGeneratorFlexible <String> aGenerator = new CombinationGeneratorFlexible <> (3, true); Set <ICommonsList <String>> aResults = aGenerator.getCombinations (aElements); assertEquals (6, aResults.size ()); assertTrue (aResults.contains (new CommonsArrayList <> ())); aResults = aGenerator.getCombinations (new CommonsArrayList <> ()); assertEquals (1, aResults.size ()); assertTrue (aResults.contains (new CommonsArrayList <> ())); aGenerator = new CombinationGeneratorFlexible <> (3, false); aResults = aGenerator.getCombinations (aElements); assertEquals (5, aResults.size ()); assertFalse (aResults.contains (new CommonsArrayList <> ())); aResults = aGenerator.getCombinations (new CommonsArrayList <> ()); assertEquals (1, aResults.size ()); assertTrue (aResults.contains (new CommonsArrayList <> ())); try { aGenerator.getCombinations (null); fail (); } catch (final NullPointerException ex) {} }
@Ignore ("Takes too long") @Test public void testHugeDataSet () { final StopWatch aSW = StopWatch.createdStarted (); try { final Set <ICommonsList <String>> aResult = CombinationGeneratorFlexible.getCombinations (HUGE_LIST, true); LOGGER.info ("Regular: " + aSW.stopAndGetMillis () + " ms with " + aResult.size () + " elements"); } catch (final OutOfMemoryError ex) { LOGGER.error ("Out of memory on test!", ex); } }
@Ignore ("Simply wrong assumption") @Test public void testRedundancy () { final ICommonsList <String> aInputList = new CommonsArrayList <> ("a", "b", "c", "d", "e", "f", "g", "h"); final ICommonsSet <ICommonsList <String>> aSimplePermutations = new CommonsHashSet <> (); CombinationGenerator.addAllPermutations (aInputList, aInputList.size (), aSimplePermutations); final ICommonsSet <ICommonsList <String>> aFlexiblePermutations = CombinationGeneratorFlexible.getCombinations (aInputList, true); assertTrue (aFlexiblePermutations.size () >= aSimplePermutations.size ()); for (final ICommonsList <String> aList : aFlexiblePermutations) assertTrue (aList.toString (), aSimplePermutations.contains (aList)); }