All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.clementlevallois.utils.PerformCombinations Maven / Gradle / Ivy

package net.clementlevallois.utils;

/**
 *
 * @author C. Levallois code adapted from a sample found on Internet.
 */
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author LEVALLOIS
 */
public class PerformCombinations {

    private final String[] table;

    /**
     *
     * @param table
     * @throws InterruptedException
     * @throws IOException
     */
    public PerformCombinations(String[] table) throws InterruptedException, IOException {
        this.table = table;

    }

    /**
     *
     * @return
     * @throws InterruptedException
     * @throws IOException
     */
    public Set call() throws InterruptedException, IOException {

        int i = 0;
        Set set = new HashSet();

        //finds all pairs (2) of the brands
        int[] indices;

        CombinationGenerator x = new CombinationGenerator(table.length, 2);

        StringBuilder combination;

        while (x.hasMore()) {
            combination = new StringBuilder();
            indices = x.getNext();
            for (int j = 0; j < indices.length; j++) {
                combination.append(table[indices[j]]).append(",");
            }

            // save all these pairs in a set
            set.add(combination.toString());

        }
        return set;

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy