JAL-2446 FeatureStore.isEmpty() added
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 10 Apr 2017 08:53:53 +0000 (09:53 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 10 Apr 2017 08:53:53 +0000 (09:53 +0100)
src/jalview/datamodel/features/FeatureStore.java
test/jalview/datamodel/features/FeatureStoreTest.java

index cb4bd6f..4e70dda 100644 (file)
@@ -23,6 +23,7 @@ public class FeatureStore
 
   /*
    * Non-positional features have no (zero) start/end position.
+   * Kept as a separate list in case this criterion changes in future.
    */
   List<SequenceFeature> nonPositionalFeatures;
 
@@ -609,4 +610,21 @@ public class FeatureStore
 
     return removed;
   }
+
+  /**
+   * Answers true if this store has no features, else false
+   * 
+   * @return
+   */
+  public boolean isEmpty()
+  {
+    boolean hasFeatures = !nonNestedFeatures.isEmpty()
+            || (contactFeatureStarts != null && !contactFeatureStarts
+                    .isEmpty())
+            || (nonPositionalFeatures != null && !nonPositionalFeatures
+                    .isEmpty())
+            || (nestedFeatures != null && nestedFeatures.size() > 0);
+
+    return !hasFeatures;
+  }
 }
index 88a7616..171b7c3 100644 (file)
@@ -366,4 +366,60 @@ public class FeatureStoreTest
     assertFalse(fs.addFeature(sf1));
     assertFalse(fs.addFeature(sf2));
   }
+
+  @Test(groups = "Functional")
+  public void testIsEmpty()
+  {
+    FeatureStore fs = new FeatureStore();
+    assertTrue(fs.isEmpty());
+
+    /*
+     * non-nested feature
+     */
+    SequenceFeature sf1 = new SequenceFeature("Cath", "", 10, 20,
+            Float.NaN, null);
+    fs.addFeature(sf1);
+    assertFalse(fs.isEmpty());
+    fs.delete(sf1);
+    assertTrue(fs.isEmpty());
+
+    /*
+     * non-positional feature
+     */
+    sf1 = new SequenceFeature("Cath", "", 0, 0, Float.NaN, null);
+    fs.addFeature(sf1);
+    assertFalse(fs.isEmpty());
+    fs.delete(sf1);
+    assertTrue(fs.isEmpty());
+
+    /*
+     * contact feature
+     */
+    sf1 = new SequenceFeature("Disulfide bond", "", 19, 49, Float.NaN, null);
+    fs.addFeature(sf1);
+    assertFalse(fs.isEmpty());
+    fs.delete(sf1);
+    assertTrue(fs.isEmpty());
+
+    /*
+     * sf2, sf3 added as nested features
+     */
+    sf1 = new SequenceFeature("Cath", "", 19, 49, Float.NaN, null);
+    SequenceFeature sf2 = new SequenceFeature("Cath", "", 20, 40,
+            Float.NaN, null);
+    SequenceFeature sf3 = new SequenceFeature("Cath", "", 25, 35,
+            Float.NaN, null);
+    fs.addFeature(sf1);
+    fs.addFeature(sf2);
+    fs.addFeature(sf3);
+    assertTrue(fs.delete(sf1));
+    // FeatureStore should now only contain features in the NCList
+    assertEquals(fs.nonNestedFeatures.size(), 0);
+    assertEquals(fs.nestedFeatures.size(), 2);
+    assertFalse(fs.isEmpty());
+    assertTrue(fs.delete(sf2));
+    assertFalse(fs.isEmpty());
+    assertTrue(fs.delete(sf3));
+    assertTrue(fs.isEmpty()); // all gone
+  }
 }