Java Code Examples for org.eclipse.xtext.xbase.lib.IteratorExtensions#groupBy()

The following examples show how to use org.eclipse.xtext.xbase.lib.IteratorExtensions#groupBy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IteratorExtensionsTest.java    From xtext-lib with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testGroupBy() {
	List<Pair<Integer, String>> pairs = new ArrayList<Pair<Integer, String>>();
	pairs.add(new Pair<Integer, String>(1, "A"));
	pairs.add(new Pair<Integer, String>(1, "a"));
	pairs.add(new Pair<Integer, String>(2, "B"));
	pairs.add(new Pair<Integer, String>(2, "b"));
	Function1<Pair<Integer, String>, Integer> computeKeys = new Function1<Pair<Integer, String>, Integer>() {

		@Override
		public Integer apply(Pair<Integer, String> p) {
			return p.getKey();
		}
	};
	Map<Integer, List<Pair<Integer, String>>> map = IteratorExtensions.groupBy(pairs.iterator(), computeKeys);
	Assert.assertEquals("Expected grouped map size", 2, map.size());
	Assert.assertTrue("Contains 1 as key", map.keySet().contains(1));
	Assert.assertEquals("Contains 2 entries for key 1", 2, map.get(1).size());
	Assert.assertEquals("Contains entry 1->A for key 1", new Pair<Integer, String>(1, "A"), map.get(1).get(0));
	Assert.assertEquals("Contains entry 1->a for key 1", new Pair<Integer, String>(1, "a"), map.get(1).get(1));
	Assert.assertTrue("Contains 2 as key", map.keySet().contains(2));
	Assert.assertEquals("Contains 2 entries for key 2", 2, map.get(2).size());
	Assert.assertEquals("Contains entry 2->B for key 2", new Pair<Integer, String>(2, "B"), map.get(2).get(0));
	Assert.assertEquals("Contains entry 2->b for key 2", new Pair<Integer, String>(2, "b"), map.get(2).get(1));
}
 
Example 2
Source File: IteratorExtensionsTest.java    From xtext-lib with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testGroupBy__WhenCalculatedKeyNull() {
	List<String> names = new ArrayList<String>();
	names.add("Mueller");
	names.add("Schneider");
	names.add("Schmidt");
	names.add("Koch");
	Function1<String, String> computeKeys = new Function1<String, String>() {

		@Override
		public String apply(String p) {
			return p.contains("y") ? "y" : null;
		}
	};
	Map<String, List<String>> map = IteratorExtensions.groupBy(names.iterator(), computeKeys);
	Assert.assertEquals("Expected grouped map size", 1, map.size());
	Assert.assertTrue("Contains null as key", map.keySet().contains(null));
	Assert.assertEquals("Contains 4 entries for key null", 4, map.get(null).size());
}
 
Example 3
Source File: IteratorExtensionsTest.java    From xtext-lib with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testGroupBy__WhenNoValuesForKey() {
	List<String> names = new ArrayList<String>();
	names.add("Mueller");
	names.add("Schneider");
	names.add("Schmidt");
	names.add("Koch");
	Function1<String, Boolean> computeKeys = new Function1<String, Boolean>() {

		@Override
		public Boolean apply(String p) {
			return p.contains("y");
		}
	};
	Map<Boolean, List<String>> map = IteratorExtensions.groupBy(names.iterator(), computeKeys);
	Assert.assertEquals("Expected grouped map size", 1, map.size());
	Assert.assertTrue("Contains FALSE as key", map.keySet().contains(Boolean.FALSE));
	Assert.assertEquals("Contains 4 entries for key Boolean.FALSE", 4, map.get(Boolean.FALSE).size());
	Assert.assertTrue("Contains entry Mueller for key FALSE", map.get(Boolean.FALSE).contains("Mueller"));
	Assert.assertNull("Contains no entry for key Boolean.TRUE", map.get(Boolean.TRUE));
}
 
Example 4
Source File: IteratorExtensionsTest.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testGroupBy__WhenEmptyList() {
	List<Pair<Integer, String>> pairs = new ArrayList<Pair<Integer, String>>();
	Function1<Pair<Integer, String>, Integer> computeKeys = new Function1<Pair<Integer, String>, Integer>() {

		@Override
		public Integer apply(Pair<Integer, String> p) {
			return p.getKey();
		}
	};
	Map<Integer, List<Pair<Integer, String>>> map = IteratorExtensions.groupBy(pairs.iterator(), computeKeys);
	Assert.assertEquals("Expected grouped map size", 0, map.size());
}
 
Example 5
Source File: IteratorExtensionsTest.java    From xtext-lib with Eclipse Public License 2.0 4 votes vote down vote up
@Test(expected = NullPointerException.class)
public void testGroupBy__WhenFunctionNull() {
	List<Pair<Integer, String>> pairs = new ArrayList<Pair<Integer, String>>();
	Function1<Pair<Integer, String>, Integer> computeKeys = null;
	IteratorExtensions.groupBy(pairs.iterator(), computeKeys);
}