This is an automated email from the ASF dual-hosted git repository. rec pushed a commit to branch feature/UIMA-5818-Use-List-instead-of-Collection-where-possible in repository https://gitbox.apache.org/repos/asf/uima-uimafit.git
commit bc7115d6bdacf05f78f147dc267ce9c6b0063dae Author: Richard Eckart de Castilho <[email protected]> AuthorDate: Fri Jul 13 21:55:13 2018 +0200 [UIMA-5818] Use List instead of Collection where possible - Switch from Collection to List in many places --- .../java/org/apache/uima/fit/util/CasUtil.java | 36 +++++++++++----------- .../apache/uima/fit/util/FSCollectionFactory.java | 14 ++++----- .../java/org/apache/uima/fit/util/JCasUtil.java | 8 ++--- .../org/apache/uima/fit/util/JCasUtilTest.java | 11 +++---- 4 files changed, 34 insertions(+), 35 deletions(-) diff --git a/uimafit-core/src/main/java/org/apache/uima/fit/util/CasUtil.java b/uimafit-core/src/main/java/org/apache/uima/fit/util/CasUtil.java index 531f2e6..b29a11f 100644 --- a/uimafit-core/src/main/java/org/apache/uima/fit/util/CasUtil.java +++ b/uimafit-core/src/main/java/org/apache/uima/fit/util/CasUtil.java @@ -33,9 +33,9 @@ import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import java.util.Map; + import org.apache.uima.cas.ArrayFS; import org.apache.uima.cas.CAS; import org.apache.uima.cas.CASRuntimeException; @@ -188,7 +188,7 @@ public final class CasUtil { * @return A collection of the selected type. * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a> */ - public static Collection<FeatureStructure> selectFS(ArrayFS array, Type type) { + public static List<FeatureStructure> selectFS(ArrayFS array, Type type) { return FSCollectionFactory.create(array, type); } @@ -203,12 +203,12 @@ public final class CasUtil { * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a> */ @SuppressWarnings({ "unchecked", "rawtypes" }) - public static Collection<AnnotationFS> select(ArrayFS array, Type type) { + public static List<AnnotationFS> select(ArrayFS array, Type type) { final CAS cas = array.getCAS(); if (!cas.getTypeSystem().subsumes(cas.getAnnotationType(), type)) { throw new IllegalArgumentException("Type [" + type.getName() + "] is not an annotation type"); } - return (Collection) FSCollectionFactory.create(array, type); + return (List) FSCollectionFactory.create(array, type); } /** @@ -753,14 +753,14 @@ public final class CasUtil { * @return the index. * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a> */ - public static Map<AnnotationFS, Collection<AnnotationFS>> indexCovering(CAS cas, Type type, + public static Map<AnnotationFS, List<AnnotationFS>> indexCovering(CAS cas, Type type, Type coveringType) { - Map<AnnotationFS, Collection<AnnotationFS>> index = new HashMap<AnnotationFS, Collection<AnnotationFS>>() { + Map<AnnotationFS, List<AnnotationFS>> index = new HashMap<AnnotationFS, List<AnnotationFS>>() { private static final long serialVersionUID = 1L; @Override - public Collection<AnnotationFS> get(Object paramObject) { - Collection<AnnotationFS> res = super.get(paramObject); + public List<AnnotationFS> get(Object paramObject) { + List<AnnotationFS> res = super.get(paramObject); if (res == null) { return emptyList(); } else { @@ -770,9 +770,9 @@ public final class CasUtil { }; for (AnnotationFS s : select(cas, coveringType)) { for (AnnotationFS u : selectCovered(cas, type, s)) { - Collection<AnnotationFS> c = index.get(u); + List<AnnotationFS> c = index.get(u); if (c == EMPTY_LIST) { - c = new LinkedList<AnnotationFS>(); + c = new ArrayList<AnnotationFS>(); index.put(u, c); } c.add(s); @@ -801,14 +801,14 @@ public final class CasUtil { * @return the index. * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a> */ - public static Map<AnnotationFS, Collection<AnnotationFS>> indexCovered(CAS cas, Type type, + public static Map<AnnotationFS, List<AnnotationFS>> indexCovered(CAS cas, Type type, Type coveredType) { - Map<AnnotationFS, Collection<AnnotationFS>> index = new HashMap<AnnotationFS, Collection<AnnotationFS>>() { + Map<AnnotationFS, List<AnnotationFS>> index = new HashMap<AnnotationFS, List<AnnotationFS>>() { private static final long serialVersionUID = 1L; @Override - public Collection<AnnotationFS> get(Object paramObject) { - Collection<AnnotationFS> res = super.get(paramObject); + public List<AnnotationFS> get(Object paramObject) { + List<AnnotationFS> res = super.get(paramObject); if (res == null) { return emptyList(); } else { @@ -867,9 +867,9 @@ public final class CasUtil { // Record covered annotations for (AnnotationFS covering : memory) { if (covering.getBegin() <= iFSbegin && iFS.getEnd() <= covering.getEnd()) { - Collection<AnnotationFS> c = index.get(covering); + List<AnnotationFS> c = index.get(covering); if (c == EMPTY_LIST) { - c = new LinkedList<AnnotationFS>(); + c = new ArrayList<AnnotationFS>(); index.put(covering, c); } c.add(iFS); @@ -1093,7 +1093,7 @@ public final class CasUtil { throw new IllegalArgumentException("Type [" + type.getName() + "] is not an annotation type"); } - List<AnnotationFS> precedingAnnotations = new LinkedList<AnnotationFS>(); + List<AnnotationFS> precedingAnnotations = new ArrayList<AnnotationFS>(); // Seek annotation in index // withSnapshotIterators() not needed here since we copy the FSes to a list anyway @@ -1172,7 +1172,7 @@ public final class CasUtil { } // add annotations from the iterator into the result list - List<AnnotationFS> followingAnnotations = new LinkedList<AnnotationFS>(); + List<AnnotationFS> followingAnnotations = new ArrayList<AnnotationFS>(); for (int i = 0; i < count && itr.isValid(); i++, itr.moveToNext()) { followingAnnotations.add(itr.get()); } diff --git a/uimafit-core/src/main/java/org/apache/uima/fit/util/FSCollectionFactory.java b/uimafit-core/src/main/java/org/apache/uima/fit/util/FSCollectionFactory.java index 3e18658..7585cf4 100644 --- a/uimafit-core/src/main/java/org/apache/uima/fit/util/FSCollectionFactory.java +++ b/uimafit-core/src/main/java/org/apache/uima/fit/util/FSCollectionFactory.java @@ -167,7 +167,7 @@ public abstract class FSCollectionFactory { * the CAS type. * @return a new collection of all feature structures of the given type. */ - public static <T extends FeatureStructure> Collection<T> create(ArrayFS<T> aArray, Type aType) { + public static <T extends FeatureStructure> List<T> create(ArrayFS<T> aArray, Type aType) { TypeSystem ts = aArray.getCAS().getTypeSystem(); List<FeatureStructure> data = new ArrayList<FeatureStructure>(aArray.size()); for (int i = 0; i < aArray.size(); i++) { @@ -176,7 +176,7 @@ public abstract class FSCollectionFactory { data.add(value); } } - return (Collection<T>) asList(data.toArray(new FeatureStructure[data.size()])); + return (List<T>) asList(data.toArray(new FeatureStructure[data.size()])); } public static <T extends FeatureStructure> ArrayFS<T> createArrayFS(CAS aCas, @@ -548,7 +548,7 @@ public abstract class FSCollectionFactory { } // Using TOP here because FSList is only available in the JCas. - public static <T extends TOP> Collection<T> create(FSList<T> aList, Type type) { + public static <T extends TOP> List<T> create(FSList<T> aList, Type type) { TypeSystem ts = aList.getCAS().getTypeSystem(); List<FeatureStructure> data = new ArrayList<FeatureStructure>(); FSList<T> i = aList; @@ -561,10 +561,10 @@ public abstract class FSCollectionFactory { i = l.getTail(); } - return (Collection<T>) asList(data.toArray(new TOP[data.size()])); + return (List<T>) asList(data.toArray(new TOP[data.size()])); } - public static Collection<String> create(StringList aList) { + public static List<String> create(StringList aList) { List<String> data = new ArrayList<String>(); StringList i = aList; while (i instanceof NonEmptyStringList) { @@ -576,7 +576,7 @@ public abstract class FSCollectionFactory { return asList(data.toArray(new String[data.size()])); } - public static Collection<Integer> create(IntegerList aList) { + public static List<Integer> create(IntegerList aList) { List<Integer> data = new ArrayList<Integer>(); IntegerList i = aList; while (i instanceof NonEmptyIntegerList) { @@ -588,7 +588,7 @@ public abstract class FSCollectionFactory { return asList(data.toArray(new Integer[data.size()])); } - public static Collection<Float> create(FloatList aList) { + public static List<Float> create(FloatList aList) { List<Float> data = new ArrayList<Float>(); FloatList i = aList; while (i instanceof NonEmptyFloatList) { diff --git a/uimafit-core/src/main/java/org/apache/uima/fit/util/JCasUtil.java b/uimafit-core/src/main/java/org/apache/uima/fit/util/JCasUtil.java index 1fb6de2..af0d5b8 100644 --- a/uimafit-core/src/main/java/org/apache/uima/fit/util/JCasUtil.java +++ b/uimafit-core/src/main/java/org/apache/uima/fit/util/JCasUtil.java @@ -154,7 +154,7 @@ public final class JCasUtil { * @return A collection of the selected type. * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a> */ - public static <T extends TOP> Collection<T> select(final FSArray array, final Class<T> type) { + public static <T extends TOP> List<T> select(final FSArray array, final Class<T> type) { return cast(CasUtil.selectFS(array, CasUtil.getType(array.getCAS(), type.getName()))); } @@ -170,7 +170,7 @@ public final class JCasUtil { * @return A collection of the selected type. * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a> */ - public static <T extends TOP> Collection<T> select(final FSList list, final Class<T> type) { + public static <T extends TOP> List<T> select(final FSList list, final Class<T> type) { return cast(FSCollectionFactory.create(list, CasUtil.getType(list.getCAS(), type.getName()))); } @@ -497,7 +497,7 @@ public final class JCasUtil { * @return the index. * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a> */ - public static <T extends Annotation, S extends Annotation> Map<T, Collection<S>> indexCovering( + public static <T extends Annotation, S extends Annotation> Map<T, List<S>> indexCovering( JCas jCas, Class<? extends T> type, Class<? extends S> coveringType) { return cast(CasUtil.indexCovering(jCas.getCas(), getType(jCas, type), getType(jCas, coveringType))); @@ -527,7 +527,7 @@ public final class JCasUtil { * @return the index. * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a> */ - public static <T extends Annotation, S extends Annotation> Map<T, Collection<S>> indexCovered( + public static <T extends Annotation, S extends Annotation> Map<T, List<S>> indexCovered( JCas jCas, Class<? extends T> type, Class<? extends S> coveredType) { return cast(CasUtil .indexCovered(jCas.getCas(), getType(jCas, type), getType(jCas, coveredType))); diff --git a/uimafit-core/src/test/java/org/apache/uima/fit/util/JCasUtilTest.java b/uimafit-core/src/test/java/org/apache/uima/fit/util/JCasUtilTest.java index be5beee..2451930 100644 --- a/uimafit-core/src/test/java/org/apache/uima/fit/util/JCasUtilTest.java +++ b/uimafit-core/src/test/java/org/apache/uima/fit/util/JCasUtilTest.java @@ -15,9 +15,9 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. - - - getCoveredAnnotations() contains code adapted from the UIMA Subiterator class. + * + * + * getCoveredAnnotations() contains code adapted from the UIMA Subiterator class. */ package org.apache.uima.fit.util; @@ -75,7 +75,6 @@ import org.junit.Test; /** * Test cases for {@link JCasUtil}. - * */ public class JCasUtilTest extends ComponentTestBase { /** @@ -139,7 +138,7 @@ public class JCasUtilTest extends ComponentTestBase { // Prepare the index long timeIndexed = System.currentTimeMillis(); - Map<Sentence, Collection<Token>> index = indexCovered(jcas, Sentence.class, Token.class); + Map<Sentence, List<Token>> index = indexCovered(jcas, Sentence.class, Token.class); timeIndexed = System.currentTimeMillis() - timeIndexed; // -- The order of entries in the index is NOT defined! @@ -770,7 +769,7 @@ public class JCasUtilTest extends ComponentTestBase { List<Sentence> sentences = new ArrayList<Sentence>(select(jCas, Sentence.class)); List<Token> tokens = new ArrayList<Token>(select(jCas, Token.class)); - Map<Token, Collection<Sentence>> index = indexCovering(jCas, Token.class, Sentence.class); + Map<Token, List<Sentence>> index = indexCovering(jCas, Token.class, Sentence.class); // Check covering annotations are found assertEquals(asList(sentences.get(0)), index.get(tokens.get(0)));
