From: gmungoc Date: Wed, 21 Jun 2017 15:26:56 +0000 (+0100) Subject: JAL-2480 tidy generic initialisations, SequenceFeatures.varargToTypes X-Git-Tag: Release_2_10_3b1~198 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=e6798fd04b1d7a35836a2e84deae5a94a35b88b9;p=jalview.git JAL-2480 tidy generic initialisations, SequenceFeatures.varargToTypes --- diff --git a/src/jalview/datamodel/features/FeatureStore.java b/src/jalview/datamodel/features/FeatureStore.java index 3e80966..84e7736 100644 --- a/src/jalview/datamodel/features/FeatureStore.java +++ b/src/jalview/datamodel/features/FeatureStore.java @@ -281,7 +281,7 @@ public class FeatureStore { if (nestedFeatures == null) { - nestedFeatures = new NCList(feature); + nestedFeatures = new NCList<>(feature); return true; } return nestedFeatures.add(feature, false); @@ -455,7 +455,7 @@ public class FeatureStore */ public List findOverlappingFeatures(long start, long end) { - List result = new ArrayList(); + List result = new ArrayList<>(); findNonNestedFeatures(start, end, result); @@ -633,7 +633,7 @@ public class FeatureStore /* * add non-nested features (may be all features for many cases) */ - List result = new ArrayList(); + List result = new ArrayList<>(); result.addAll(nonNestedFeatures); /* @@ -667,7 +667,7 @@ public class FeatureStore { return Collections.emptyList(); } - return new ArrayList(contactFeatureStarts); + return new ArrayList<>(contactFeatureStarts); } /** @@ -682,7 +682,7 @@ public class FeatureStore { return Collections.emptyList(); } - return new ArrayList(nonPositionalFeatures); + return new ArrayList<>(nonPositionalFeatures); } /** @@ -972,7 +972,7 @@ public class FeatureStore public List getFeaturesForGroup(boolean positional, String group) { - List result = new ArrayList(); + List result = new ArrayList<>(); /* * if we know features don't include the target group, no need diff --git a/src/jalview/datamodel/features/NCList.java b/src/jalview/datamodel/features/NCList.java index a6a23e7..b8160d3 100644 --- a/src/jalview/datamodel/features/NCList.java +++ b/src/jalview/datamodel/features/NCList.java @@ -77,7 +77,7 @@ public class NCList public NCList(T entry) { this(); - subranges.add(new NCNode(entry)); + subranges.add(new NCNode<>(entry)); size = 1; } @@ -95,7 +95,7 @@ public class NCList */ protected List buildSubranges(List ranges) { - List sublists = new ArrayList(); + List sublists = new ArrayList<>(); if (ranges.isEmpty()) { @@ -174,7 +174,7 @@ public class NCList /* * all subranges precede this one - add it on the end */ - subranges.add(new NCNode(entry)); + subranges.add(new NCNode<>(entry)); return true; } @@ -196,7 +196,7 @@ public class NCList /* * new entry lies between subranges j-1 j */ - subranges.add(j, new NCNode(entry)); + subranges.add(j, new NCNode<>(entry)); return true; } @@ -244,7 +244,7 @@ public class NCList * entry spans two subranges but doesn't enclose any * so just add it */ - subranges.add(j, new NCNode(entry)); + subranges.add(j, new NCNode<>(entry)); return true; } } @@ -265,7 +265,7 @@ public class NCList } else { - subranges.add(new NCNode(entry)); + subranges.add(new NCNode<>(entry)); } return true; @@ -323,9 +323,9 @@ public class NCList protected synchronized void addEnclosingRange(T entry, final int i, final int j) { - NCList newNCList = new NCList(); + NCList newNCList = new NCList<>(); newNCList.addNodes(subranges.subList(i, j + 1)); - NCNode newNode = new NCNode(entry, newNCList); + NCNode newNode = new NCNode<>(entry, newNCList); for (int k = j; k >= i; k--) { subranges.remove(k); @@ -354,7 +354,7 @@ public class NCList */ public List findOverlaps(long from, long to) { - List result = new ArrayList(); + List result = new ArrayList<>(); findOverlaps(from, to, result); @@ -554,7 +554,7 @@ public class NCList */ public List getEntries() { - List result = new ArrayList(); + List result = new ArrayList<>(); getEntries(result); return result; } diff --git a/src/jalview/datamodel/features/NCNode.java b/src/jalview/datamodel/features/NCNode.java index c1be242..007f3b1 100644 --- a/src/jalview/datamodel/features/NCNode.java +++ b/src/jalview/datamodel/features/NCNode.java @@ -42,7 +42,7 @@ class NCNode implements ContiguousI */ NCNode(V range) { - List ranges = new ArrayList(); + List ranges = new ArrayList<>(); ranges.add(range); build(ranges); } diff --git a/src/jalview/datamodel/features/SequenceFeatures.java b/src/jalview/datamodel/features/SequenceFeatures.java index 8f6d496..52da8c7 100644 --- a/src/jalview/datamodel/features/SequenceFeatures.java +++ b/src/jalview/datamodel/features/SequenceFeatures.java @@ -112,15 +112,11 @@ public class SequenceFeatures implements SequenceFeaturesI public List findFeatures(int from, int to, String... type) { - List result = new ArrayList(); + List result = new ArrayList<>(); - for (String featureType : varargToTypes(type)) + for (FeatureStore featureSet : varargToTypes(type)) { - FeatureStore features = featureStore.get(featureType); - if (features != null) - { - result.addAll(features.findOverlappingFeatures(from, to)); - } + result.addAll(featureSet.findOverlappingFeatures(from, to)); } return result; @@ -132,7 +128,7 @@ public class SequenceFeatures implements SequenceFeaturesI @Override public List getAllFeatures(String... type) { - List result = new ArrayList(); + List result = new ArrayList<>(); result.addAll(getPositionalFeatures(type)); @@ -149,7 +145,7 @@ public class SequenceFeatures implements SequenceFeaturesI { if (ontologyTerm == null || ontologyTerm.length == 0) { - return new ArrayList(); + return new ArrayList<>(); } Set featureTypes = getFeatureTypes(ontologyTerm); @@ -165,13 +161,9 @@ public class SequenceFeatures implements SequenceFeaturesI { int result = 0; - for (String featureType : varargToTypes(type)) + for (FeatureStore featureSet : varargToTypes(type)) { - FeatureStore featureSet = featureStore.get(featureType); - if (featureSet != null) - { - result += featureSet.getFeatureCount(positional); - } + result += featureSet.getFeatureCount(positional); } return result; } @@ -184,16 +176,11 @@ public class SequenceFeatures implements SequenceFeaturesI { int result = 0; - for (String featureType : varargToTypes(type)) + for (FeatureStore featureSet : varargToTypes(type)) { - FeatureStore featureSet = featureStore.get(featureType); - if (featureSet != null) - { - result += featureSet.getTotalFeatureLength(); - } + result += featureSet.getTotalFeatureLength(); } return result; - } /** @@ -202,45 +189,41 @@ public class SequenceFeatures implements SequenceFeaturesI @Override public List getPositionalFeatures(String... type) { - List result = new ArrayList(); + List result = new ArrayList<>(); - for (String featureType : varargToTypes(type)) + for (FeatureStore featureSet : varargToTypes(type)) { - FeatureStore featureSet = featureStore.get(featureType); - if (featureSet != null) - { - result.addAll(featureSet.getPositionalFeatures()); - } + result.addAll(featureSet.getPositionalFeatures()); } return result; } /** * A convenience method that converts a vararg for feature types to an - * Iterable, replacing the value with the stored feature types if it is null - * or empty + * Iterable over matched feature sets in key order * * @param type * @return */ - protected Iterable varargToTypes(String... type) + protected Iterable varargToTypes(String... type) { if (type == null || type.length == 0) { /* - * no vararg parameter supplied + * no vararg parameter supplied - return all */ - return featureStore.keySet(); + return featureStore.values(); } - /* - * else make a copy of the list, and remove any null value just in case, - * as it would cause errors looking up the features Map - * sort in alphabetical order for consistent output behaviour - */ - List types = new ArrayList(Arrays.asList(type)); - types.remove(null); - Collections.sort(types); + List types = new ArrayList<>(); + List args = Arrays.asList(type); + for (Entry featureType : featureStore.entrySet()) + { + if (args.contains(featureType.getKey())) + { + types.add(featureType.getValue()); + } + } return types; } @@ -250,15 +233,11 @@ public class SequenceFeatures implements SequenceFeaturesI @Override public List getContactFeatures(String... type) { - List result = new ArrayList(); + List result = new ArrayList<>(); - for (String featureType : varargToTypes(type)) + for (FeatureStore featureSet : varargToTypes(type)) { - FeatureStore featureSet = featureStore.get(featureType); - if (featureSet != null) - { - result.addAll(featureSet.getContactFeatures()); - } + result.addAll(featureSet.getContactFeatures()); } return result; } @@ -269,15 +248,11 @@ public class SequenceFeatures implements SequenceFeaturesI @Override public List getNonPositionalFeatures(String... type) { - List result = new ArrayList(); + List result = new ArrayList<>(); - for (String featureType : varargToTypes(type)) + for (FeatureStore featureSet : varargToTypes(type)) { - FeatureStore featureSet = featureStore.get(featureType); - if (featureSet != null) - { - result.addAll(featureSet.getNonPositionalFeatures()); - } + result.addAll(featureSet.getNonPositionalFeatures()); } return result; } @@ -321,17 +296,11 @@ public class SequenceFeatures implements SequenceFeaturesI public Set getFeatureGroups(boolean positionalFeatures, String... type) { - Set groups = new HashSet(); + Set groups = new HashSet<>(); - Iterable types = varargToTypes(type); - - for (String featureType : types) + for (FeatureStore featureSet : varargToTypes(type)) { - FeatureStore featureSet = featureStore.get(featureType); - if (featureSet != null) - { - groups.addAll(featureSet.getFeatureGroups(positionalFeatures)); - } + groups.addAll(featureSet.getFeatureGroups(positionalFeatures)); } return groups; @@ -344,7 +313,7 @@ public class SequenceFeatures implements SequenceFeaturesI public Set getFeatureTypesForGroups(boolean positionalFeatures, String... groups) { - Set result = new HashSet(); + Set result = new HashSet<>(); for (Entry featureType : featureStore.entrySet()) { @@ -372,7 +341,7 @@ public class SequenceFeatures implements SequenceFeaturesI @Override public Set getFeatureTypes(String... soTerm) { - Set types = new HashSet(); + Set types = new HashSet<>(); for (Entry entry : featureStore.entrySet()) { String type = entry.getKey(); @@ -459,20 +428,12 @@ public class SequenceFeatures implements SequenceFeaturesI public List getFeaturesForGroup(boolean positional, String group, String... type) { - List result = new ArrayList(); - Iterable types = varargToTypes(type); - - for (String featureType : types) + List result = new ArrayList<>(); + for (FeatureStore featureSet : varargToTypes(type)) { - /* - * check whether the feature type is present, and also - * whether it has features for the specified group - */ - FeatureStore features = featureStore.get(featureType); - if (features != null - && features.getFeatureGroups(positional).contains(group)) + if (featureSet.getFeatureGroups(positional).contains(group)) { - result.addAll(features.getFeaturesForGroup(positional, group)); + result.addAll(featureSet.getFeaturesForGroup(positional, group)); } } return result; diff --git a/test/jalview/datamodel/SequenceTest.java b/test/jalview/datamodel/SequenceTest.java index 2496a5b..e40d1a5 100644 --- a/test/jalview/datamodel/SequenceTest.java +++ b/test/jalview/datamodel/SequenceTest.java @@ -440,11 +440,14 @@ public class SequenceTest sq.sequenceChanged(); assertEquals(12, sq.findPosition(8)); cursor = (SequenceCursor) PA.getValue(sq, "cursor"); - assertEquals("test:Pos12:Col9:startCol3:endCol10:tok1", + // sequenceChanged() invalidates cursor.lastResidueColumn + cursor = (SequenceCursor) PA.getValue(sq, "cursor"); + assertEquals("test:Pos12:Col9:startCol3:endCol0:tok1", cursor.toString()); // findPosition with cursor accepts base 1 column values assertEquals(13, ((Sequence) sq).findPosition(10, cursor)); - assertEquals(13, sq.findPosition(9)); + assertEquals(13, sq.findPosition(9)); // F13 + // lastResidueColumn has now been found and saved in cursor assertEquals("test:Pos13:Col10:startCol3:endCol10:tok1", PA.getValue(sq, "cursor").toString()); } diff --git a/test/jalview/datamodel/features/SequenceFeaturesTest.java b/test/jalview/datamodel/features/SequenceFeaturesTest.java index b7b52a7..a144f03 100644 --- a/test/jalview/datamodel/features/SequenceFeaturesTest.java +++ b/test/jalview/datamodel/features/SequenceFeaturesTest.java @@ -10,8 +10,11 @@ import jalview.datamodel.SequenceFeature; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Set; +import junit.extensions.PA; + import org.testng.annotations.Test; public class SequenceFeaturesTest @@ -886,12 +889,15 @@ public class SequenceFeaturesTest * no type specified - get all types stored * they are returned in keyset (alphabetical) order */ - Iterable types = sf.varargToTypes(); - Iterator iterator = types.iterator(); + Map featureStores = (Map) PA + .getValue(sf, "featureStore"); + + Iterable types = sf.varargToTypes(); + Iterator iterator = types.iterator(); assertTrue(iterator.hasNext()); - assertEquals(iterator.next(), "Cath"); + assertSame(iterator.next(), featureStores.get("Cath")); assertTrue(iterator.hasNext()); - assertEquals(iterator.next(), "Metal"); + assertSame(iterator.next(), featureStores.get("Metal")); assertFalse(iterator.hasNext()); /* @@ -901,9 +907,9 @@ public class SequenceFeaturesTest types = sf.varargToTypes(new String[] {}); iterator = types.iterator(); assertTrue(iterator.hasNext()); - assertEquals(iterator.next(), "Cath"); + assertSame(iterator.next(), featureStores.get("Cath")); assertTrue(iterator.hasNext()); - assertEquals(iterator.next(), "Metal"); + assertSame(iterator.next(), featureStores.get("Metal")); assertFalse(iterator.hasNext()); /* @@ -919,9 +925,9 @@ public class SequenceFeaturesTest types = sf.varargToTypes((String[]) null); iterator = types.iterator(); assertTrue(iterator.hasNext()); - assertEquals(iterator.next(), "Cath"); + assertSame(iterator.next(), featureStores.get("Cath")); assertTrue(iterator.hasNext()); - assertEquals(iterator.next(), "Metal"); + assertSame(iterator.next(), featureStores.get("Metal")); assertFalse(iterator.hasNext()); /* @@ -930,29 +936,27 @@ public class SequenceFeaturesTest types = sf.varargToTypes("Metal"); iterator = types.iterator(); assertTrue(iterator.hasNext()); - assertEquals(iterator.next(), "Metal"); + assertSame(iterator.next(), featureStores.get("Metal")); assertFalse(iterator.hasNext()); /* * two types specified - get sorted alphabetically */ - types = sf.varargToTypes("Metal", "Helix"); + types = sf.varargToTypes("Metal", "Cath"); iterator = types.iterator(); assertTrue(iterator.hasNext()); - assertEquals(iterator.next(), "Helix"); + assertSame(iterator.next(), featureStores.get("Cath")); assertTrue(iterator.hasNext()); - assertEquals(iterator.next(), "Metal"); + assertSame(iterator.next(), featureStores.get("Metal")); assertFalse(iterator.hasNext()); /* - * null type included - should get removed + * null type included - should be ignored */ types = sf.varargToTypes("Metal", null, "Helix"); iterator = types.iterator(); assertTrue(iterator.hasNext()); - assertEquals(iterator.next(), "Helix"); - assertTrue(iterator.hasNext()); - assertEquals(iterator.next(), "Metal"); + assertSame(iterator.next(), featureStores.get("Metal")); assertFalse(iterator.hasNext()); }