AnnotationUtil { public static <TOKEN_TYPE extends Annotation> String getSurroundingText( JCas jCas, Annotation annotation, Class<TOKEN_TYPE> tokenClass, int numberOfTokens, boolean before) { if (numberOfTokens < 1) throw new IllegalArgumentException( "numberOfTokens must be greater than zero. Actual values is: " + numberOfTokens); String documentText = jCas.getDocumentText(); int start; int end; if (before) { start = 0; end = annotation.getBegin(); List<TOKEN_TYPE> anns = JCasUtil.selectPreceding(jCas, tokenClass, annotation, numberOfTokens); if (anns.size() > 0) { start = anns.get(0).getBegin(); } } else { start = annotation.getEnd(); end = documentText.length(); List<TOKEN_TYPE> anns = JCasUtil.selectFollowing(jCas, tokenClass, annotation, numberOfTokens); if (anns.size() > 0) { end = anns.get(anns.size() - 1).getEnd(); } } return documentText.substring(start, end); }  static T selectFirstMatching(
      JCas jCas,
      Class<T> selectedType,
      Annotation annotation); static List<T> selectMatching(
      JCas jCas,
      Class<T> selectedType,
      Annotation annotation); static boolean contains(Annotation bigAnnotation, Annotation smallAnnotation); static boolean overlaps(Annotation annotation1, Annotation annotation2); static int size(Annotation annotation); static String getSurroundingText(
      JCas jCas,
      Annotation annotation,
      Class<TOKEN_TYPE> tokenClass,
      int numberOfTokens,
      boolean before); static void sort(List<T> annotations); static int[] getAnnotationsExtent(List<? extends Annotation> annotations);  }
@Test public void testGetSurroundingTexts() throws UIMAException { tokenBuilder.buildTokens(jCas, "AAA BBB CCC DDDD EEEE FFFF"); Annotation sa = new Annotation(jCas, 8, 11); sa.addToIndexes(); assertEquals("CCC", sa.getCoveredText()); assertEquals("BBB ", AnnotationUtil.getSurroundingText(jCas, sa, Token.class, 1, true)); assertEquals("AAA BBB ", AnnotationUtil.getSurroundingText(jCas, sa, Token.class, 2, true)); assertEquals("AAA BBB ", AnnotationUtil.getSurroundingText(jCas, sa, Token.class, 3, true)); assertEquals("AAA BBB ", AnnotationUtil.getSurroundingText(jCas, sa, Token.class, 4, true)); assertEquals(" DDDD", AnnotationUtil.getSurroundingText(jCas, sa, Token.class, 1, false)); assertEquals(" DDDD EEEE", AnnotationUtil.getSurroundingText(jCas, sa, Token.class, 2, false)); assertEquals( " DDDD EEEE FFFF", AnnotationUtil.getSurroundingText(jCas, sa, Token.class, 3, false)); assertEquals( " DDDD EEEE FFFF", AnnotationUtil.getSurroundingText(jCas, sa, Token.class, 4, false)); IllegalArgumentException iae = null; try { AnnotationUtil.getSurroundingText(jCas, sa, Token.class, 0, true); } catch (IllegalArgumentException e) { iae = e; } assertNotNull(iae); }