private static TestStringMultisetGenerator unionGenerator() { return new TestStringMultisetGenerator() { @Override protected Multiset<String> create(String[] elements) { Multiset<String> multiset1 = LinkedHashMultiset.create(); Multiset<String> multiset2 = LinkedHashMultiset.create(); for (int i = 0; i < elements.length; i++) { String element = elements[i]; if (multiset1.contains(element) || multiset2.contains(element)) { // add to both; the one already containing it will have more multiset1.add(element); multiset2.add(element); } else if (i % 2 == 0) { multiset1.add(elements[i]); } else { multiset2.add(elements[i]); } } return Multisets.union(multiset1, multiset2); } }; }
private static TestStringMultisetGenerator sumGenerator() { return new TestStringMultisetGenerator() { @Override protected Multiset<String> create(String[] elements) { Multiset<String> multiset1 = LinkedHashMultiset.create(); Multiset<String> multiset2 = LinkedHashMultiset.create(); for (int i = 0; i < elements.length; i++) { // add to either; sum should contain all if (i % 2 == 0) { multiset1.add(elements[i]); } else { multiset2.add(elements[i]); } } return Multisets.sum(multiset1, multiset2); } }; }
private static TestStringMultisetGenerator differenceGenerator() { return new TestStringMultisetGenerator() { @Override protected Multiset<String> create(String[] elements) { Multiset<String> multiset1 = LinkedHashMultiset.create(); Multiset<String> multiset2 = LinkedHashMultiset.create(); multiset1.add("equalIn1"); multiset1.add("fewerIn1"); multiset2.add("equalIn1"); multiset2.add("fewerIn1", 3); multiset2.add("onlyIn2", 2); for (int i = 0; i < elements.length; i++) { // add 1 more copy of each element to multiset1 than multiset2 multiset1.add(elements[i], i + 2); multiset2.add(elements[i], i + 1); } return Multisets.difference(multiset1, multiset2); } }; }
private static TestStringMultisetGenerator filteredGenerator() { return new TestStringMultisetGenerator() { @Override protected Multiset<String> create(String[] elements) { Multiset<String> multiset = LinkedHashMultiset.create(); Collections.addAll(multiset, elements); multiset.addAll(ELEMENTS_TO_FILTER_OUT); return Multisets.filter(multiset, PREDICATE); } @Override public List<String> order(List<String> insertionOrder) { return Lists.newArrayList(LinkedHashMultiset.create(insertionOrder)); } }; }
private static TestStringMultisetGenerator concurrentSkipListMultisetGenerator() { return new TestStringMultisetGenerator() { @Override protected Multiset<String> create(String[] elements) { Multiset<String> multiset = new ConcurrentHashMultiset<>(new ConcurrentSkipListMap<String, AtomicInteger>()); Collections.addAll(multiset, elements); return multiset; } @Override public List<String> order(List<String> insertionOrder) { return Ordering.natural().sortedCopy(insertionOrder); } }; }
private static TestStringMultisetGenerator hashMultisetGenerator() { return new TestStringMultisetGenerator() { @Override protected Multiset<String> create(String[] elements) { return HashMultiset.create(asList(elements)); } }; }
private static TestStringMultisetGenerator concurrentHashMultisetGenerator() { return new TestStringMultisetGenerator() { @Override protected Multiset<String> create(String[] elements) { return ConcurrentHashMultiset.create(asList(elements)); } }; }
private static TestStringMultisetGenerator concurrentSkipListMultisetGenerator() { return new TestStringMultisetGenerator() { @Override protected Multiset<String> create(String[] elements) { Multiset<String> multiset = new ConcurrentHashMultiset<String>( new ConcurrentSkipListMap<String, AtomicInteger>()); Collections.addAll(multiset, elements); return multiset; } @Override public List<String> order(List<String> insertionOrder) { return Ordering.natural().sortedCopy(insertionOrder); } }; }
private static TestStringMultisetGenerator unmodifiableSortedMultisetGenerator() { return new TestStringMultisetGenerator() { @Override protected Multiset<String> create(String[] elements) { return Multisets.unmodifiableSortedMultiset( TreeMultiset.create(asList(elements))); } @Override public List<String> order(List<String> insertionOrder) { Collections.sort(insertionOrder); return insertionOrder; } }; }
private static TestStringMultisetGenerator intersectionGenerator() { return new TestStringMultisetGenerator() { @Override protected Multiset<String> create(String[] elements) { Multiset<String> multiset1 = LinkedHashMultiset.create(); Multiset<String> multiset2 = LinkedHashMultiset.create(); multiset1.add("only1"); multiset2.add("only2"); for (int i = 0; i < elements.length; i++) { multiset1.add(elements[i]); multiset2.add(elements[elements.length - 1 - i]); } if (elements.length > 0) { multiset1.add(elements[0]); } if (elements.length > 1) { /* * When a test requests a multiset with duplicates, our plan of * "add an extra item 0 to A and an extra item 1 to B" really means * "add an extra item 0 to A and B," which isn't what we want. */ if (!Objects.equal(elements[0], elements[1])) { multiset2.add(elements[1], 2); } } return Multisets.intersection(multiset1, multiset2); } }; }