uima-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Adam Lally" <ala...@alum.rpi.edu>
Subject Re: Java 1.5 Generics
Date Fri, 09 Nov 2007 15:34:15 GMT
+1 to wait until next release.  When we get to that point, here's
something to consider for the JCAS - a way to enable the use of the
Java 5 enhanced for loop.  This is based on code from Pablo Dubuoe,
who does have an ICLA on file and told me it was OK to contribute
(he's going on vacation otherwise he would post it himself):

  /**
   * Allows for foreach constructs over JCases as follow: <br/>
   *
   * for(SubTypeOfTOP annot:UimaJava5.indexedFs(jcas,SubTypeOfTOP.class)){...}
   */
  public static <T extends TOP> Iterable<T> indexedFs(final JCas aJCas,
          final Class<T> clazz) throws CASRuntimeException {
    try {
      final int type = clazz.getField("type").getInt(clazz);
      return new Iterable<T>() {

        @SuppressWarnings("unchecked")
        public Iterator<T> iterator() {
          return aJCas.getJFSIndexRepository().getAllIndexedFS(type);
        }
      };
    } catch (SecurityException e) {
      throw new CASRuntimeException(e);
    } catch (IllegalAccessException e) {
      throw new CASRuntimeException(e);
    } catch (NoSuchFieldException e) {
      throw new CASRuntimeException(e);
    }
  }

It's a static utility method now but could be added as an instance of JCAS.
  -Adam

Mime
View raw message