X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2Ffeatures%2FNCNode.java;h=007f3b12cbd7b8821bd9b84676c38d38f38dfcc4;hb=c2e16e95b564932deda33dc59ecaea367e7b33b0;hp=d4c7b0c33f6c6f9d55594417561bfa9c7e83eda7;hpb=51728d3951398f9c12d7017c776953f17322cc68;p=jalview.git diff --git a/src/jalview/datamodel/features/NCNode.java b/src/jalview/datamodel/features/NCNode.java index d4c7b0c..007f3b1 100644 --- a/src/jalview/datamodel/features/NCNode.java +++ b/src/jalview/datamodel/features/NCNode.java @@ -1,5 +1,7 @@ package jalview.datamodel.features; +import jalview.datamodel.ContiguousI; + import java.util.ArrayList; import java.util.List; @@ -9,7 +11,7 @@ import java.util.List; * * @param */ -class NCNode +class NCNode implements ContiguousI { /* * deep size (number of ranges included) @@ -18,10 +20,13 @@ class NCNode private V region; + /* + * null, or an object holding contained subregions of this nodes region + */ private NCList subregions; /** - * Constructor + * Constructor given a list of ranges * * @param ranges */ @@ -31,17 +36,24 @@ class NCNode } /** - * Constructo + * Constructor given a single range * * @param range */ NCNode(V range) { - List ranges = new ArrayList(); + List ranges = new ArrayList<>(); ranges.add(range); build(ranges); } + NCNode(V entry, NCList newNCList) + { + region = entry; + subregions = newNCList; + size = 1 + newNCList.size(); + } + /** * @param ranges */ @@ -59,16 +71,25 @@ class NCNode } } - int getStart() + @Override + public int getBegin() { return region.getBegin(); } - int getEnd() + @Override + public int getEnd() { return region.getEnd(); } + /** + * Formats the node as a bracketed list e.g. + * + *
+   * [1-100 [10-30 [10-20]], 15-30 [20-20]]
+   * 
+ */ @Override public String toString() { StringBuilder sb = new StringBuilder(10 * size); @@ -80,6 +101,17 @@ class NCNode return sb.toString(); } + void prettyPrint(StringBuilder sb, int offset, int indent) { + for (int i = 0 ; i < offset ; i++) { + sb.append(" "); + } + sb.append(region.getBegin()).append("-").append(region.getEnd()); + if (subregions != null) + { + sb.append(System.lineSeparator()); + subregions.prettyPrint(sb, offset + 2, indent); + } + } /** * Add any ranges that overlap the from-to range to the result list * @@ -87,7 +119,7 @@ class NCNode * @param to * @param result */ - void addOverlaps(long from, long to, List result) + void findOverlaps(long from, long to, List result) { if (region.getBegin() <= to && region.getEnd() >= from) { @@ -104,7 +136,7 @@ class NCNode * * @param entry */ - public synchronized void add(V entry) + synchronized void add(V entry) { if (entry.getBegin() < region.getBegin() || entry.getEnd() > region.getEnd()) { throw new IllegalArgumentException(String.format( @@ -120,5 +152,104 @@ class NCNode { subregions.add(entry); } + size++; + } + + /** + * Answers true if the data held satisfy the rules of construction of an + * NCList, else false. + * + * @return + */ + boolean isValid() + { + /* + * we don't handle reverse ranges + */ + if (region != null && region.getBegin() > region.getEnd()) + { + return false; + } + if (subregions == null) + { + return true; + } + return subregions.isValid(getBegin(), getEnd()); + } + + /** + * Adds all contained entries to the given list + * + * @param entries + */ + void getEntries(List entries) + { + entries.add(region); + if (subregions != null) + { + subregions.getEntries(entries); + } + } + + /** + * Answers true if this object contains the given entry (by object equals + * test), else false + * + * @param entry + * @return + */ + boolean contains(V entry) + { + if (entry == null) + { + return false; + } + if (entry.equals(region)) + { + return true; + } + return subregions == null ? false : subregions.contains(entry); + } + + /** + * Answers the 'root' region modelled by this object + * + * @return + */ + V getRegion() + { + return region; + } + + /** + * Answers the (possibly null) contained regions within this object + * + * @return + */ + NCList getSubRegions() + { + return subregions; + } + + /** + * Nulls the subregion reference if it is empty (after a delete entry + * operation) + */ + void deleteSubRegionsIfEmpty() + { + if (subregions != null && subregions.size() == 0) + { + subregions = null; + } + } + + /** + * Answers the (deep) size of this node i.e. the number of ranges it models + * + * @return + */ + int size() + { + return size; } }