Author: schor Date: Wed Jul 25 19:52:09 2018 New Revision: 1836662 URL: http://svn.apache.org/viewvc?rev=1836662&view=rev Log: [UIMA-5845] make select get/single have consistent return or throw for get/single when nothing is found, based on setting of nullOK(). Clarify javadocs
Modified: uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/cas/SelectFSs.java uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java uima/uv3/uimaj-v3/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java Modified: uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/cas/SelectFSs.java URL: http://svn.apache.org/viewvc/uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/cas/SelectFSs.java?rev=1836662&r1=1836661&r2=1836662&view=diff ============================================================================== --- uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/cas/SelectFSs.java (original) +++ uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/cas/SelectFSs.java Wed Jul 25 19:52:09 2018 @@ -128,15 +128,16 @@ public interface SelectFSs<T extends Fea SelectFSs<T> allViews(boolean allViews); /** - * Applies to the various argument forms of the get method. + * Applies to the various argument forms of the get and single methods. * Indicates that a null value should not throw an exception. * <p> - * Default: null is not OK as a value + * Calling this method is equivalent to nullOK(true). + * If never called, nulls are not OK by default. * @return the updated SelectFSs object */ SelectFSs<T> nullOK(); /** - * Applies to the various argument forms of the get method. + * Applies to the various argument forms of the get and single methods. * Indicates that a null value should or should not throw an exception. * <p> * Default: null is not OK as a value @@ -451,8 +452,8 @@ public interface SelectFSs<T extends Fea */ T get(); // returns first element or null if empty (unless nullOK(false) specified) /** - * @return first element, which must be not null - * @throws CASRuntimeException if element is null or + * @return first element, verifying that the size of the selection is 1 (or maybe 0) + * @throws CASRuntimeException (conditioned on nullOK == false ) if element is null or * if there is more than 1 element in the selection, * or if the selection is empty */ @@ -462,6 +463,8 @@ public interface SelectFSs<T extends Fea * @throws CASRuntimeException if there is more than 1 element in the selection. */ T singleOrNull(); // throws if more than 1 element, returns single or null + + // next are positioning alternatives // get(...) throws if null (unless nullOK specified) /** Modified: uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java URL: http://svn.apache.org/viewvc/uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java?rev=1836662&r1=1836661&r2=1836662&view=diff ============================================================================== --- uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java (original) +++ uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java Wed Jul 25 19:52:09 2018 @@ -123,7 +123,6 @@ public class SelectFSs_impl <T extends F private boolean isFollowing = false; private boolean isPreceding = false; - private boolean isNullOkSpecified = false; // for complex defaulting of get(), get(n) private boolean isAltSource = false; private BoundsUse boundsUse = null; @@ -316,14 +315,12 @@ public class SelectFSs_impl <T extends F @Override public SelectFSs_impl<T> nullOK() { // applies to get() and single() - this.isNullOK = true; - this.isNullOkSpecified = true; - return this; + this.isNullOK = true; + return this; } @Override public SelectFSs_impl<T> nullOK(boolean bNullOk) { // applies to get() and single() this.isNullOK = bNullOk; - this.isNullOkSpecified = true; return this; } @@ -1007,15 +1004,15 @@ public class SelectFSs_impl <T extends F */ @Override public T get() { - return getNullChk(true); + return getNullChk(); } - private T getNullChk(boolean isDoSetTest) { + private T getNullChk() { FSIterator<T> it = fsIterator(); if (it.isValid()) { return it.getNvc(); } - if ((!isDoSetTest || isNullOkSpecified) && !isNullOK) { // if not specified, isNullOK == false + if (!isNullOK) { // if not specified, isNullOK == false throw new CASRuntimeException(CASRuntimeException.SELECT_GET_NO_INSTANCES, ti.getName(), maybeMsgPosition()); } return null; @@ -1030,7 +1027,7 @@ public class SelectFSs_impl <T extends F @Override public T single() { T v = singleOrNull(); - if (v == null) { + if (v == null && !isNullOK) { throw new CASRuntimeException(CASRuntimeException.SELECT_GET_NO_INSTANCES, ti.getName(), maybeMsgPosition()); } return v; @@ -1064,7 +1061,7 @@ public class SelectFSs_impl <T extends F @Override public T get(int offset) { this.shift = offset; - return getNullChk(false); + return getNullChk(); } @Override @@ -1082,7 +1079,7 @@ public class SelectFSs_impl <T extends F @Override public T get(TOP fs) { startAt(fs); - return getNullChk(false); + return getNullChk(); } @Override @@ -1100,7 +1097,7 @@ public class SelectFSs_impl <T extends F @Override public T get(TOP fs, int offset) { startAt(fs, offset); - return getNullChk(false); + return getNullChk(); } @Override @@ -1118,7 +1115,7 @@ public class SelectFSs_impl <T extends F @Override public T get(int begin, int end) { startAt(begin, end); - return getNullChk(false); + return getNullChk(); } @Override @@ -1136,7 +1133,7 @@ public class SelectFSs_impl <T extends F @Override public T get(int begin, int end, int offset) { startAt(begin, end, offset); - return getNullChk(false); + return getNullChk(); } @Override Modified: uima/uv3/uimaj-v3/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java URL: http://svn.apache.org/viewvc/uima/uv3/uimaj-v3/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java?rev=1836662&r1=1836661&r2=1836662&view=diff ============================================================================== --- uima/uv3/uimaj-v3/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java (original) +++ uima/uv3/uimaj-v3/trunk/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java Wed Jul 25 19:52:09 2018 @@ -378,7 +378,7 @@ public class AnnotationIteratorTest exte } } assertTrue(x); - assertNull(annotIndex.select().coveredBy(3, 3).get()); + assertNull(annotIndex.select().coveredBy(3, 3).nullOK().get()); assertNotNull(annotIndex.select().get(3)); assertNull(annotIndex.select().nullOK().coveredBy(3, 5).get(3)); x = false;