@Test public void testOverlapAnnotationIndex() throws Exception { JCas jcas = JCasFactory.createJCas("de.julielab.jcore.types.jcore-all-types"); Token t1 = new Token(jcas, 0, 3); Token t2 = new Token(jcas, 2, 8); Token t3 = new Token(jcas, 7, 10); Token t4 = new Token(jcas, 11, 18); Token t5 = new Token(jcas, 15, 21); Token t6 = new Token(jcas, 22, 27); t1.addToIndexes(); t2.addToIndexes(); t3.addToIndexes(); t4.addToIndexes(); t5.addToIndexes(); t6.addToIndexes(); JCoReOverlapAnnotationIndex<Token> index = new JCoReOverlapAnnotationIndex<>(jcas, Token.type); List<Token> result = index.search(t2).collect(Collectors.toList()); assertTrue(result.contains(t1)); assertTrue(result.contains(t2)); assertTrue(result.contains(t3)); assertEquals(3, result.size()); result = index.search(t1).collect(Collectors.toList()); assertTrue(result.contains(t1)); assertTrue(result.contains(t2)); assertEquals(2, result.size()); result = index.search(t4).collect(Collectors.toList()); assertTrue(result.contains(t4)); assertTrue(result.contains(t5)); assertEquals(2, result.size()); result = index.search(t6).collect(Collectors.toList()); assertTrue(result.contains(t6)); assertEquals(1, result.size()); }
public <T extends Annotation> Stream<E> search(T a) { if (!frozen) throw new IllegalStateException( "This index is not frozen and cannot be used yet. Freeze the index before searching."); if (beginIndex.isEmpty()) return Stream.empty(); int begin = a.getBegin(); int end = a.getEnd(); int indexBeginAfterEnd = insertionPoint(JCoReTools.binarySearch(beginIndex, an -> an.getBegin(), end)); int indexEndBeforeBegin = insertionPoint(JCoReTools.binarySearch(endIndex, an -> an.getEnd(), begin)); if (indexBeginAfterEnd < endIndex.size() - indexEndBeforeBegin) { List<E> beginBeforeEnd = new ArrayList<>(beginIndex.subList(0, indexBeginAfterEnd)); ArrayList<E> result = new ArrayList<>(); for (E e : beginBeforeEnd) { if (e.getEnd() > begin) result.add(e); } return result.stream(); } else { List<E> endAfterBegin = new ArrayList<>(endIndex.subList(indexEndBeforeBegin, endIndex.size())); ArrayList<E> result = new ArrayList<>(); for (E e : endAfterBegin) { if (e.getBegin() < end) result.add(e); } return result.stream(); } }
JCoReOverlapAnnotationIndex implements JCoReAnnotationIndex<E> { public <T extends Annotation> Stream<E> search(T a) { if (!frozen) throw new IllegalStateException( "This index is not frozen and cannot be used yet. Freeze the index before searching."); if (beginIndex.isEmpty()) return Stream.empty(); int begin = a.getBegin(); int end = a.getEnd(); int indexBeginAfterEnd = insertionPoint(JCoReTools.binarySearch(beginIndex, an -> an.getBegin(), end)); int indexEndBeforeBegin = insertionPoint(JCoReTools.binarySearch(endIndex, an -> an.getEnd(), begin)); if (indexBeginAfterEnd < endIndex.size() - indexEndBeforeBegin) { List<E> beginBeforeEnd = new ArrayList<>(beginIndex.subList(0, indexBeginAfterEnd)); ArrayList<E> result = new ArrayList<>(); for (E e : beginBeforeEnd) { if (e.getEnd() > begin) result.add(e); } return result.stream(); } else { List<E> endAfterBegin = new ArrayList<>(endIndex.subList(indexEndBeforeBegin, endIndex.size())); ArrayList<E> result = new ArrayList<>(); for (E e : endAfterBegin) { if (e.getBegin() < end) result.add(e); } return result.stream(); } } }
JCoReOverlapAnnotationIndex implements JCoReAnnotationIndex<E> { public <T extends Annotation> Stream<E> search(T a) { if (!frozen) throw new IllegalStateException( "This index is not frozen and cannot be used yet. Freeze the index before searching."); if (beginIndex.isEmpty()) return Stream.empty(); int begin = a.getBegin(); int end = a.getEnd(); int indexBeginAfterEnd = insertionPoint(JCoReTools.binarySearch(beginIndex, an -> an.getBegin(), end)); int indexEndBeforeBegin = insertionPoint(JCoReTools.binarySearch(endIndex, an -> an.getEnd(), begin)); if (indexBeginAfterEnd < endIndex.size() - indexEndBeforeBegin) { List<E> beginBeforeEnd = new ArrayList<>(beginIndex.subList(0, indexBeginAfterEnd)); ArrayList<E> result = new ArrayList<>(); for (E e : beginBeforeEnd) { if (e.getEnd() > begin) result.add(e); } return result.stream(); } else { List<E> endAfterBegin = new ArrayList<>(endIndex.subList(indexEndBeforeBegin, endIndex.size())); ArrayList<E> result = new ArrayList<>(); for (E e : endAfterBegin) { if (e.getBegin() < end) result.add(e); } return result.stream(); } } JCoReOverlapAnnotationIndex();  JCoReOverlapAnnotationIndex(JCas jcas, int type);  JCoReOverlapAnnotationIndex(JCas jcas, Type type); }
JCoReOverlapAnnotationIndex implements JCoReAnnotationIndex<E> { public <T extends Annotation> Stream<E> search(T a) { if (!frozen) throw new IllegalStateException( "This index is not frozen and cannot be used yet. Freeze the index before searching."); if (beginIndex.isEmpty()) return Stream.empty(); int begin = a.getBegin(); int end = a.getEnd(); int indexBeginAfterEnd = insertionPoint(JCoReTools.binarySearch(beginIndex, an -> an.getBegin(), end)); int indexEndBeforeBegin = insertionPoint(JCoReTools.binarySearch(endIndex, an -> an.getEnd(), begin)); if (indexBeginAfterEnd < endIndex.size() - indexEndBeforeBegin) { List<E> beginBeforeEnd = new ArrayList<>(beginIndex.subList(0, indexBeginAfterEnd)); ArrayList<E> result = new ArrayList<>(); for (E e : beginBeforeEnd) { if (e.getEnd() > begin) result.add(e); } return result.stream(); } else { List<E> endAfterBegin = new ArrayList<>(endIndex.subList(indexEndBeforeBegin, endIndex.size())); ArrayList<E> result = new ArrayList<>(); for (E e : endAfterBegin) { if (e.getBegin() < end) result.add(e); } return result.stream(); } } JCoReOverlapAnnotationIndex();  JCoReOverlapAnnotationIndex(JCas jcas, int type);  JCoReOverlapAnnotationIndex(JCas jcas, Type type); void index(JCas jCas, int type); @SuppressWarnings("unchecked") void index(JCas jCas, Type type); void index(E annotation); void freeze(); Stream<E> search(T a); void melt(); @Override void add(E a); }
JCoReOverlapAnnotationIndex implements JCoReAnnotationIndex<E> { public <T extends Annotation> Stream<E> search(T a) { if (!frozen) throw new IllegalStateException( "This index is not frozen and cannot be used yet. Freeze the index before searching."); if (beginIndex.isEmpty()) return Stream.empty(); int begin = a.getBegin(); int end = a.getEnd(); int indexBeginAfterEnd = insertionPoint(JCoReTools.binarySearch(beginIndex, an -> an.getBegin(), end)); int indexEndBeforeBegin = insertionPoint(JCoReTools.binarySearch(endIndex, an -> an.getEnd(), begin)); if (indexBeginAfterEnd < endIndex.size() - indexEndBeforeBegin) { List<E> beginBeforeEnd = new ArrayList<>(beginIndex.subList(0, indexBeginAfterEnd)); ArrayList<E> result = new ArrayList<>(); for (E e : beginBeforeEnd) { if (e.getEnd() > begin) result.add(e); } return result.stream(); } else { List<E> endAfterBegin = new ArrayList<>(endIndex.subList(indexEndBeforeBegin, endIndex.size())); ArrayList<E> result = new ArrayList<>(); for (E e : endAfterBegin) { if (e.getBegin() < end) result.add(e); } return result.stream(); } } JCoReOverlapAnnotationIndex();  JCoReOverlapAnnotationIndex(JCas jcas, int type);  JCoReOverlapAnnotationIndex(JCas jcas, Type type); void index(JCas jCas, int type); @SuppressWarnings("unchecked") void index(JCas jCas, Type type); void index(E annotation); void freeze(); Stream<E> search(T a); void melt(); @Override void add(E a);  }