/** * Returns a collection containing all possible combinations of the input values. * @param <E> the element type * @param values a List of collections of values * @return a collection of Lists, all the same size as the input List, containing all possible * combinations of the values in the collections, in their respective indexes in the List. */ public static <E> Collection<List<E>> fanOutCombinations(List<Collection<E>> values) { Collection<List<E>> retval = new ArrayList<List<E>>(); List<E> soFar = Collections.emptyList(); fanOutCombinations(values, retval, soFar, 0); return retval; }
/** * Finds a common superclass or superinterface for all the classes specified in the arguments. * * @param classes a Collection of Classes * @return a Class that is a common superclass or superinterface */ public static Class<?> findCommonSuperclass(Collection<Class<?>> classes) { return findCommonSuperclasses(classes).iterator().next(); } }
CollectionUtil.groupByClass(objectToInput.keySet(), true);
public void testLinkedHashMapAddEmpty2() throws Exception { LinkedHashMap map = new LinkedHashMap(); try { Map newMap = CollectionUtil.linkedHashMapAdd(map, "nonexistentKey", "newKey", "newValue"); fail("expected IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected } }
CollectionUtil.groupByClass(objectToInput.keySet(), true);
public void testLinkedHashMapAddEmpty1() throws Exception { LinkedHashMap map = new LinkedHashMap(); Map newMap = CollectionUtil.linkedHashMapAdd(map, null, "newKey", "newValue"); Set expectedKeys = new HashSet(Arrays.asList(new Object[] {"newKey"})); assertEquals(expectedKeys, newMap.keySet()); Set expectedValues = new HashSet(Arrays.asList(new Object[] {"newValue"})); assertEquals(expectedValues, new HashSet(newMap.values())); }
Map<ClassDescriptor, List<InterMineObject>> cldToObjectsForCld = new IdentityHashMap<ClassDescriptor, List<InterMineObject>>(); Map<Class<?>, List<InterMineObject>> categorised = CollectionUtil.groupByClass(objects, false); Map<ClassDescriptor, Boolean> cldsDone = new IdentityHashMap<ClassDescriptor, Boolean>();
/** * Returns a collection containing all possible combinations of the input values. * @param <E> the element type * @param values a List of collections of values * @return a collection of Lists, all the same size as the input List, containing all possible * combinations of the values in the collections, in their respective indexes in the List. */ public static <E> Collection<List<E>> fanOutCombinations(List<Collection<E>> values) { Collection<List<E>> retval = new ArrayList<List<E>>(); List<E> soFar = Collections.emptyList(); fanOutCombinations(values, retval, soFar, 0); return retval; }
/** * Finds a common superclass or superinterface for all the classes specified in the arguments. * * @param classes a Collection of Classes * @return a Class that is a common superclass or superinterface */ public static Class<?> findCommonSuperclass(Collection<Class<?>> classes) { return findCommonSuperclasses(classes).iterator().next(); } }
public void testLinkedHashMapSameKey() throws Exception { LinkedHashMap map = new LinkedHashMap(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); map.put("key4", "value4"); Map newMap = CollectionUtil.linkedHashMapAdd(map, "key2", "key2", "newValue"); Set expectedKeys = new HashSet(Arrays.asList(new Object[] { "key1", "key2", "key3", "key4"})); assertEquals(expectedKeys, newMap.keySet()); Set expectedValues = new HashSet(Arrays.asList(new Object[] { "value1", "newValue", "value3", "value4"})); assertEquals(expectedValues, new HashSet(newMap.values())); }
Map<ClassDescriptor, List<InterMineObject>> cldToObjectsForCld = new IdentityHashMap<ClassDescriptor, List<InterMineObject>>(); Map<Class<?>, List<InterMineObject>> categorised = CollectionUtil.groupByClass(objects, false); Map<ClassDescriptor, Boolean> cldsDone = new IdentityHashMap<ClassDescriptor, Boolean>();
private static <E> void fanOutCombinations(List<Collection<E>> values, Collection<List<E>> retval, List<E> soFar, int index) { if (index == values.size() - 1) { for (E value : values.get(index)) { List<E> solution = new ArrayList<E>(soFar); solution.add(value); retval.add(solution); } } else { for (E value : values.get(index)) { List<E> solution = new ArrayList<E>(soFar); solution.add(value); fanOutCombinations(values, retval, solution, index + 1); } } }
public void testFindCommonSuperclasses() throws Exception { Set<Class<?>> input = new HashSet<Class<?>>(); input.add(Employee.class); input.add(Contractor.class); input.add(Manager.class); assertEquals(Collections.singleton(Employable.class), CollectionUtil.findCommonSuperclasses(input)); input.clear(); input.add(DynamicUtil.composeClass(Company.class, Broke.class)); input.add(DynamicUtil.composeClass(Company.class)); assertEquals(Collections.singleton(Company.class), CollectionUtil.findCommonSuperclasses(input)); input.clear(); input.add(DynamicUtil.composeClass(Company.class)); input.add(Types.class); assertEquals(Collections.singleton(InterMineObject.class), CollectionUtil.findCommonSuperclasses(input)); input.clear(); input.add(DynamicUtil.composeClass(Contractor.class, Broke.class)); input.add(DynamicUtil.composeClass(Employee.class, Broke.class)); assertEquals(new HashSet(Arrays.asList(Employable.class, Broke.class)), CollectionUtil.findCommonSuperclasses(input)); } }
public void testLinkedHashMapAddValue2() throws Exception { LinkedHashMap map = new LinkedHashMap(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); map.put("key4", "value4"); Map newMap = CollectionUtil.linkedHashMapAdd(map, "key4", "newKey", "newValue"); Set expectedKeys = new HashSet(Arrays.asList(new Object[] { "key1", "key2", "key3", "key4", "newKey"})); assertEquals(expectedKeys, newMap.keySet()); Set expectedValues = new HashSet(Arrays.asList(new Object[] { "value1", "value2", "value3", "value4", "newValue"})); assertEquals(expectedValues, new HashSet(newMap.values())); }
expected.put(String.class, lString); assertEquals(expected, CollectionUtil.groupByClass(c, false)); expected.put(Comparable.class, lSerialisables); assertEquals(expected, CollectionUtil.groupByClass(c, true));
private static <E> void fanOutCombinations(List<Collection<E>> values, Collection<List<E>> retval, List<E> soFar, int index) { if (index == values.size() - 1) { for (E value : values.get(index)) { List<E> solution = new ArrayList<E>(soFar); solution.add(value); retval.add(solution); } } else { for (E value : values.get(index)) { List<E> solution = new ArrayList<E>(soFar); solution.add(value); fanOutCombinations(values, retval, solution, index + 1); } } }
public void testLinkedHashMapAddValue1() throws Exception { LinkedHashMap map = new LinkedHashMap(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); map.put("key4", "value4"); Map newMap = CollectionUtil.linkedHashMapAdd(map, "key1", "newKey", "newValue"); Set expectedKeys = new HashSet(Arrays.asList(new Object[] { "key1", "newKey", "key2", "key3", "key4" })); assertEquals(expectedKeys, newMap.keySet()); Set expectedValues = new HashSet(Arrays.asList(new Object[] { "value1", "newValue", "value2", "value3", "value4"})); assertEquals(expectedValues, new HashSet(newMap.values())); }
.fanOutCombinations(values)) { if (keysToSourceObjects.containsKey(valueSet)) { throw new ObjectStoreException("Duplicate objects found for pk "
public void testLinkedHashMapAddAfterNull() throws Exception { LinkedHashMap map = new LinkedHashMap(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); map.put("key4", "value4"); Map newMap = CollectionUtil.linkedHashMapAdd(map, null, "newKey", "newValue"); Set expectedKeys = new HashSet(Arrays.asList(new Object[] { "newKey", "key1", "key2", "key3", "key4"})); assertEquals(expectedKeys, newMap.keySet()); Set expectedValues = new HashSet(Arrays.asList(new Object[] { "newValue", "value1", "value2", "value3", "value4"})); assertEquals(expectedValues, new HashSet(newMap.values())); }
.fanOutCombinations(values)) { if (keysToSourceObjects.containsKey(valueSet)) { throw new ObjectStoreException("Duplicate objects found for pk "