/** * Produces a Set of possible mappings from items in the firstSet onto items in the secondSet, * where the mapping maps from one item to another item that is equal according to the * comparator. * * For example, if firstSet is {a1, b1, c2}, and secondSet is {d1, e1, f2}, and the comparator * returns zero when comparing items with the same number, then the result will be a Set that * contains two maps, and these are {a1->d1, b1->e1, c2->f2}, and {a1->e1, * b1->d1, c2->f2}. For more examples, see the MappingUtilTest.java file. * * If firstSet is smaller than secondSet, then there are possible mappings, but if firstSet is * bigger than secondSet, then there are no possible mappings. * * @param firstSet the set of items to map from * @param secondSet the set of items to map to * @param comparator a comparator for the items in the two sets that returns 0 for the required * equivalence operation * @param <T> The element type of the sets * @return a Set of Maps from items in the firstSet onto items in the secondSet */ @SuppressWarnings("unchecked") public static <T> Set<Map<T, T>> findCombinations(Set<T> firstSet, Set<T> secondSet, Comparator<? super T> comparator) { return findCombinations(firstSet, secondSet, comparator, (MappingUtilChecker<T>) DEFAULT_CHECKER); }
/** * Produces a Set of possible combinations of multiple mappings (as produced by * findCombinations), where all mappings in a multiple mapping combination map onto distinct * objects. * * For example, if combinations contains {{a1->b1}, {a1->c1}}, then the result will be a * Set that contains three Sets: * <ol><li>{{a1->b1}}</li> * <li>{{a1->c1}}</li> * <li>{{a1->b1}, {a1->c1}}</li></ol> * * @param combinations a Set of Maps, each of which is a mapping from one set of items onto * another set of items * @param <T> The element type * @return a Set of Sets of Maps, where each Set of Maps is a set of mappings that have all * items being mapped onto disjoint */ public static <T> Set<Set<Map<T, T>>> findMultipleCombinations(Set<Map<T, T>> combinations) { Set<Set<Map<T, T>>> retval = new LinkedHashSet<Set<Map<T, T>>>(); // the result we will // return. Set<Map<T, T>> newCombinations = new LinkedHashSet<Map<T, T>>(combinations); // clone, so we don't alter. // Actually, we don't need to do this as long as there isn't multi-threaded // access, since we restore the Set in the end. Set<Map<T, T>> combinationsSoFar = new LinkedHashSet<Map<T, T>>(); // An empty set. recurseFindMultipleCombinations(retval, newCombinations, combinationsSoFar); return retval; }
public void testMultiple1() throws Exception { Set firstSet = new HashSet(); Set secondSet = new HashSet(); firstSet.add(a1); secondSet.add(b1); secondSet.add(c1); Comparator comparator = new MappingUtilTestComparator(); Set combinations = MappingUtil.findCombinations(firstSet, secondSet, comparator); Set multiCombinations = MappingUtil.findMultipleCombinations(combinations); Map firstMapping = new HashMap(); firstMapping.put(a1, b1); Map secondMapping = new HashMap(); secondMapping.put(a1, c1); Set firstCombination = new HashSet(); firstCombination.add(firstMapping); Set secondCombination = new HashSet(); secondCombination.add(secondMapping); Set thirdCombination = new HashSet(); thirdCombination.add(firstMapping); thirdCombination.add(secondMapping); Set result = new HashSet(); result.add(firstCombination); result.add(secondCombination); result.add(thirdCombination); assertEquals(result, multiCombinations); }
secondSet.add(h2); Comparator comparator = new MappingUtilTestComparator(); Set combinations = MappingUtil.findCombinations(firstSet, secondSet, comparator); Set multiCombinations = MappingUtil.findMultipleCombinations(combinations);
secondSet.add(h2); Comparator comparator = new MappingUtilTestComparator(); Set combinations = MappingUtil.findCombinations(firstSet, secondSet, comparator); Set multiCombinations = MappingUtil.findMultipleCombinations(combinations);
/** * Produces a Set of possible mappings from items in the firstSet onto items in the secondSet, * where the mapping maps from one item to another item that is equal according to the * comparator. * * For example, if firstSet is {a1, b1, c2}, and secondSet is {d1, e1, f2}, and the comparator * returns zero when comparing items with the same number, then the result will be a Set that * contains two maps, and these are {a1->d1, b1->e1, c2->f2}, and {a1->e1, * b1->d1, c2->f2}. For more examples, see the MappingUtilTest.java file. * * If firstSet is smaller than secondSet, then there are possible mappings, but if firstSet is * bigger than secondSet, then there are no possible mappings. * * @param firstSet the set of items to map from * @param secondSet the set of items to map to * @param comparator a comparator for the items in the two sets that returns 0 for the required * equivalence operation * @param <T> The element type of the sets * @return a Set of Maps from items in the firstSet onto items in the secondSet */ @SuppressWarnings("unchecked") public static <T> Set<Map<T, T>> findCombinations(Set<T> firstSet, Set<T> secondSet, Comparator<? super T> comparator) { return findCombinations(firstSet, secondSet, comparator, (MappingUtilChecker<T>) DEFAULT_CHECKER); }
/** * Produces a Set of possible combinations of multiple mappings (as produced by * findCombinations), where all mappings in a multiple mapping combination map onto distinct * objects. * * For example, if combinations contains {{a1->b1}, {a1->c1}}, then the result will be a * Set that contains three Sets: * <ol><li>{{a1->b1}}</li> * <li>{{a1->c1}}</li> * <li>{{a1->b1}, {a1->c1}}</li></ol> * * @param combinations a Set of Maps, each of which is a mapping from one set of items onto * another set of items * @param <T> The element type * @return a Set of Sets of Maps, where each Set of Maps is a set of mappings that have all * items being mapped onto disjoint */ public static <T> Set<Set<Map<T, T>>> findMultipleCombinations(Set<Map<T, T>> combinations) { Set<Set<Map<T, T>>> retval = new LinkedHashSet<Set<Map<T, T>>>(); // the result we will // return. Set<Map<T, T>> newCombinations = new LinkedHashSet<Map<T, T>>(combinations); // clone, so we don't alter. // Actually, we don't need to do this as long as there isn't multi-threaded // access, since we restore the Set in the end. Set<Map<T, T>> combinationsSoFar = new LinkedHashSet<Map<T, T>>(); // An empty set. recurseFindMultipleCombinations(retval, newCombinations, combinationsSoFar); return retval; }
Collection<Map<AbstractTable, AbstractTable>> c = MappingUtil.findCombinations(precompQuery .getFrom(), query.getFrom(), new AbstractTableComparator(), new OptimiserMappingChecker(precompQuery.getWhere(), query.getWhere())); .findMultipleCombinations(mappings);
Map<T, T> soFar = Collections.emptyMap(); Set<Map<T, T>> retval = new LinkedHashSet<Map<T, T>>(); findCombinations(retval, firstList, secondList, comparator, checker, soFar, 0, takenSeconds); return retval;
recurseFindMultipleCombinations(retval, combinations, combinationsSoFar); recurseFindMultipleCombinations(retval, combinations, combinationsSoFar);
Collection<Map<AbstractTable, AbstractTable>> c = MappingUtil.findCombinations(precompQuery .getFrom(), query.getFrom(), new AbstractTableComparator(), new OptimiserMappingChecker(precompQuery.getWhere(), query.getWhere())); .findMultipleCombinations(mappings);
Map<T, T> soFar = Collections.emptyMap(); Set<Map<T, T>> retval = new LinkedHashSet<Map<T, T>>(); findCombinations(retval, firstList, secondList, comparator, checker, soFar, 0, takenSeconds); return retval;
recurseFindMultipleCombinations(retval, combinations, combinationsSoFar); recurseFindMultipleCombinations(retval, combinations, combinationsSoFar);
private static <T> void findCombinations(Set<Map<T, T>> retval, List<T> firstList, List<T> secondList, Comparator<? super T> comparator, MappingUtilChecker<T> checker, Map<T, T> soFar, int firstIndex, Set<Integer> takenSeconds) { if (firstIndex >= firstList.size()) { retval.add(soFar); } else { T firstElement = firstList.get(firstIndex); for (int i = 0; i < secondList.size(); i++) { if (!takenSeconds.contains(new Integer(i))) { T secondElement = secondList.get(i); if (comparator.compare(firstElement, secondElement) == 0) { Set<Integer> newTakenSeconds = new HashSet<Integer>(takenSeconds); Map<T, T> newSoFar = new LinkedHashMap<T, T>(soFar); newTakenSeconds.add(new Integer(i)); newSoFar.put(firstElement, secondElement); if (checker.check(newSoFar)) { findCombinations(retval, firstList, secondList, comparator, checker, newSoFar, firstIndex + 1, newTakenSeconds); } } } } } }
private static <T> void findCombinations(Set<Map<T, T>> retval, List<T> firstList, List<T> secondList, Comparator<? super T> comparator, MappingUtilChecker<T> checker, Map<T, T> soFar, int firstIndex, Set<Integer> takenSeconds) { if (firstIndex >= firstList.size()) { retval.add(soFar); } else { T firstElement = firstList.get(firstIndex); for (int i = 0; i < secondList.size(); i++) { if (!takenSeconds.contains(new Integer(i))) { T secondElement = secondList.get(i); if (comparator.compare(firstElement, secondElement) == 0) { Set<Integer> newTakenSeconds = new HashSet<Integer>(takenSeconds); Map<T, T> newSoFar = new LinkedHashMap<T, T>(soFar); newTakenSeconds.add(new Integer(i)); newSoFar.put(firstElement, secondElement); if (checker.check(newSoFar)) { findCombinations(retval, firstList, secondList, comparator, checker, newSoFar, firstIndex + 1, newTakenSeconds); } } } } } }
public void testAWrongMapping() throws Exception { Set firstSet = new HashSet(); Set secondSet = new HashSet(); firstSet.add(a1); firstSet.add(b1); firstSet.add(c3); firstSet.add(d4); secondSet.add(e1); secondSet.add(f2); secondSet.add(g3); secondSet.add(h4); Set resultSet = new HashSet(); Comparator comparator = new MappingUtilTestComparator(); assertEquals(resultSet, MappingUtil.findCombinations(firstSet, secondSet, comparator)); }
public void testAnotherSimpleMapping() throws Exception { Set firstSet = new HashSet(); Set secondSet = new HashSet(); firstSet.add(a1); firstSet.add(b2); firstSet.add(c3); firstSet.add(d4); secondSet.add(e1); secondSet.add(f2); secondSet.add(g3); secondSet.add(h4); Set resultSet = new HashSet(); Map result = new HashMap(); result.put(a1, e1); result.put(b2, f2); result.put(c3, g3); result.put(d4, h4); resultSet.add(result); Comparator comparator = new MappingUtilTestComparator(); assertEquals(resultSet, MappingUtil.findCombinations(firstSet, secondSet, comparator)); }
public void testYetAnotherSimpleMapping() throws Exception { Set firstSet = new HashSet(); Set secondSet = new HashSet(); firstSet.add(a1); firstSet.add(b2); firstSet.add(c3); firstSet.add(d4); secondSet.add(e4); secondSet.add(f3); secondSet.add(g2); secondSet.add(h1); Set resultSet = new HashSet(); Map result = new HashMap(); result.put(a1, h1); result.put(b2, g2); result.put(c3, f3); result.put(d4, e4); resultSet.add(result); Comparator comparator = new MappingUtilTestComparator(); assertEquals(resultSet, MappingUtil.findCombinations(firstSet, secondSet, comparator)); }
assertEquals(resultSet, MappingUtil.findCombinations(firstSet, secondSet, comparator)); assertEquals(resultSet2, MappingUtil.findCombinations(secondSet, firstSet, comparator));
Set combinations = MappingUtil.findCombinations(firstSet, secondSet, comparator, checker);