Given List of datacenters which are dc1, dc2, dc3 and list of machines, h1, h2, h3, h4 as mentioned below -
Datacenters = dc1, dc2, dc3
Machines = h1, h2, h3, h
Not really sure what you've asked here, but i think i can see where the problem is, each time get call get mapping you only generate 1 row. so i've rewritten the code so it generates all of them and gives you back a list of the maps. so you can do what you need with them.
public class DataCenterMapping {
public static void main(String[] args) {
DatacenterMachineMapping dcm = new DatacenterMachineMapping(
Arrays.asList("dc1", "dc2", "dc3"), Arrays.asList("h1", "h2",
"h3", "h4"));
List<Map<String, String>> coloHost = dcm
.getDatacenterMachineMappings();
System.out.println(coloHost);
}
}
class DatacenterMachineMapping {
private boolean firstCall = true;
private int hostListIndex = 0;
private List<String> datacenterList, hostList;
public DatacenterMachineMapping(List<String> datacenterList,
List<String> hostList) {
this.datacenterList = datacenterList;
this.hostList = hostList;
}
public List<Map<String, String>> getDatacenterMachineMappings() {
List<Map<String, String>> grid = new ArrayList<Map<String, String>>();
for (int i = 0; i < datacenterList.size(); i++) {
Map<String, String> datacenterMachineMapping = new HashMap<String, String>();
String[] line = new String[hostList.size()];
for (int j = 0; j < line.length; j++) {
int off = j + i;
if (off >= datacenterList.size()) {
off -= datacenterList.size();
}
datacenterMachineMapping.put(hostList.get(j) ,datacenterList.get(off));
}
grid.add(datacenterMachineMapping);
}
return grid;
}
}
example output:
[{h4=dc1, h1=dc1, h3=dc3, h2=dc2}, {h4=dc2, h1=dc2, h3=dc1, h2=dc3}, {h4=dc3, h1=dc3, h3=dc2, h2=dc1}]
You're talking math. The answer is (n choose k), where n is the number of machines, and k is the number of datacenters.
The reason is the following: The ordering doesn't really matter, so we'll assume that the Datacenters are always arranged in the same order. For the first data center, we can pick any one of the n
machines. For the second, we can pick any one of the machines, except for the one picked before, thus n * (n-1)
. The next data center will lead to n * (n-1) * (n-2)
possible situations.
Thus, if you had 10 machines, and 4 datacenters, you would have:
10 * 9 * 8 * 7
possibile combinations.
More info here: http://en.wikipedia.org/wiki/Combination
If you want a function to do the work for you , it is in the Apache commons: http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/util/ArithmeticUtils.html#binomialCoefficientDouble%28int,%20int%29
However, if you are actually wanting to generate those combinations, then you need a for loop.