RollingTextBlock extends RawTextProcessor { public RollingTextBlock roll(String nextText) { if (LOG.isDebugEnabled()) { LOG.debug("roll"); LOG.debug("nextText: " + nextText.replace('\n', '¶').replace('\r', '¶')); } this.processText(); int prevLength = this.block1.length(); List<Annotation<?>> annotations = new ArrayList<>(); for (Annotation<?> annotation : this.getAnnotations()) { int newStart = annotation.getStart() - prevLength; int newEnd = annotation.getEnd() - prevLength; if (newEnd > 0 || (newStart == 0 && newEnd == 0)) { if (newStart < 0) newStart = 0; Annotation<?> newAnnotation = annotation.getAnnotation(newStart, newEnd); annotations.add(newAnnotation); if (LOG.isTraceEnabled()) { LOG.trace("Moved " + annotation + " to " + newStart + ", " + newEnd); } } else { if (LOG.isTraceEnabled()) { LOG.trace("Removed annotation " + annotation + ", newEnd = " + newEnd); } } } RollingTextBlock textBlock = new RollingTextBlock(this, nextText, annotations); return textBlock; } RollingTextBlock(boolean processByDefault, CurrentFileProvider currentFileProvider, String sessionId); private  RollingTextBlock(RollingTextBlock predecessor, String nextText, List<Annotation<?>> annotations); RollingTextBlock roll(String nextText); AnnotatedText getRawTextBlock(); @Override void onNextFile(File file);  }
@Test public void testRoll() throws Exception { System.setProperty("config.file", "src/test/resources/test.conf"); ConfigFactory.invalidateCaches(); final Config config = ConfigFactory.load(); final String sessionId = "test"; String[] labels = new String[0]; RollingTextBlock textBlock = new RollingTextBlock(true, null, sessionId); textBlock = textBlock.roll("One "); List<Annotation<String>> annotations = new ArrayList<>(); annotations.add(new Annotation<String>(0, "One".length(), "1", labels)); textBlock.addAnnotations(annotations); textBlock = textBlock.roll("Two "); annotations = new ArrayList<>(); annotations.add(new Annotation<String>("One ".length(), "One Two".length(), "2", labels)); textBlock.addAnnotations(annotations); textBlock = textBlock.roll("Three "); annotations = new ArrayList<>(); annotations.add(new Annotation<String>("One Two ".length(), "One Two Three".length(), "3", labels)); textBlock.addAnnotations(annotations); textBlock = textBlock.roll("Four "); annotations = new ArrayList<>(); annotations.add(new Annotation<String>("One Two Three ".length(), "One Two Three Four".length(), "4", labels)); textBlock.addAnnotations(annotations); textBlock = textBlock.roll("Five"); assertEquals("Two Three Four Five", textBlock.getText()); annotations = textBlock.getAnnotations(String.class); System.out.println(annotations.toString()); assertEquals(3, annotations.size()); int i = 0; for (Annotation<String> annotation : annotations) { if (i == 0) { assertEquals("2", annotation.getData()); assertEquals(0, annotation.getStart()); assertEquals("Two".length(), annotation.getEnd()); } else if (i == 1) { assertEquals("3", annotation.getData()); assertEquals("Two ".length(), annotation.getStart()); assertEquals("Two Three".length(), annotation.getEnd()); } else if (i == 2) { assertEquals("4", annotation.getData()); assertEquals("Two Three ".length(), annotation.getStart()); assertEquals("Two Three Four".length(), annotation.getEnd()); } i++; } }