### Question:
EntityProcessorImpl implements EntityProcessor { static List<EntityAnnotationGroup> getAnnotationGroups(List<EntityAnnotation> annotationList, boolean longestOnly) { List<EntityAnnotationGroup> groups = new ArrayList<>(); Collections.sort(annotationList, Collections.reverseOrder()); PeekingIterator<EntityAnnotation> iter = Iterators.peekingIterator(annotationList.iterator()); while (iter.hasNext()) { EntityAnnotationGroup group = new EntityAnnotationGroup(); group.add(iter.next()); Set<Entity> entitiesInGroup = new HashSet<>(); while (iter.hasNext() && group.intersects(iter.peek())) { if (!entitiesInGroup.contains(iter.peek().getToken())) { entitiesInGroup.add(iter.peek().getToken()); group.add(iter.next()); } else { iter.next(); } } if (longestOnly) { Iterator<EntityAnnotation> groupIter = group.iterator(); int longest = group.peek().length(); while (groupIter.hasNext()) { EntityAnnotation annot = groupIter.next(); if (annot.length() < longest) { groupIter.remove(); } } } groups.add(group); } return groups; } @Inject protected  EntityProcessorImpl(EntityRecognizer recognizer); @Override List<EntityAnnotation> annotateEntities(EntityFormatConfiguration config);  }### Answer:
@Test public void testGetSeparateAnnotationGroups() { EntityAnnotation annot1 = new EntityAnnotation(mockEntity, 0, 4); EntityAnnotation annot2 = new EntityAnnotation(mockEntity2, 5, 10); annotationList.add(annot1); annotationList.add(annot2); EntityAnnotationGroup expected1 = new EntityAnnotationGroup(); expected1.add(annot2); EntityAnnotationGroup expected2 = new EntityAnnotationGroup(); expected2.add(annot1); assertThat(EntityProcessorImpl.getAnnotationGroups(annotationList, false), contains(expected1, expected2)); }
@Test public void testGetJoinedAnnotationGroups() { EntityAnnotation annot1 = new EntityAnnotation(mockEntity, 0, 4); EntityAnnotation annot2 = new EntityAnnotation(mockEntity2, 3, 5); annotationList.add(annot1); annotationList.add(annot2); EntityAnnotationGroup expected = new EntityAnnotationGroup(); expected.add(annot1); expected.add(annot2); assertThat(EntityProcessorImpl.getAnnotationGroups(annotationList, false), contains(expected)); }
@Test public void testGetLongestAnnotationGroups() { EntityAnnotation annot1 = new EntityAnnotation(mockEntity, 0, 4); EntityAnnotation annot2 = new EntityAnnotation(mockEntity2, 3, 5); annotationList.add(annot1); annotationList.add(annot2); EntityAnnotationGroup expected = new EntityAnnotationGroup(); expected.add(annot1); assertThat(EntityProcessorImpl.getAnnotationGroups(annotationList, true), contains(expected)); }
@Test public void testGetMultipleLongestAnnotationGroups() { EntityAnnotation annot1 = new EntityAnnotation(mockEntity, 0, 4); EntityAnnotation annot2 = new EntityAnnotation(mockEntity2, 3, 10); annotationList.add(annot1); annotationList.add(annot2); EntityAnnotationGroup expected = new EntityAnnotationGroup(); expected.add(annot2); assertThat(EntityProcessorImpl.getAnnotationGroups(annotationList, true), contains(expected)); }
@Test public void testUniqueAnnotations() { EntityAnnotation annot1 = new EntityAnnotation(mockEntity, 0, 4); EntityAnnotation annot2 = new EntityAnnotation(mockEntity, 3, 5); annotationList.add(annot1); annotationList.add(annot2); EntityAnnotationGroup group1 = new EntityAnnotationGroup(); group1.add(annot1); assertThat(EntityProcessorImpl.getAnnotationGroups(annotationList, true), contains(group1)); }