1 package jalview.datamodel.features;
3 import java.util.ArrayList;
7 * Each node of the NCList tree consists of a range, and (optionally) the NCList
8 * of ranges it encloses
12 class NCNode<V extends ContiguousI> implements ContiguousI
15 * deep size (number of ranges included)
22 * null, or an object holding contained subregions of this nodes region
24 private NCList<V> subregions;
27 * Constructor given a list of ranges
31 NCNode(List<V> ranges)
37 * Constructor given a single range
43 List<V> ranges = new ArrayList<V>();
48 NCNode(V entry, NCList<V> newNCList)
51 subregions = newNCList;
52 size = 1 + newNCList.size();
58 protected void build(List<V> ranges)
62 if (!ranges.isEmpty())
64 region = ranges.get(0);
66 if (ranges.size() > 1)
68 subregions = new NCList<V>(ranges.subList(1, ranges.size()));
75 return region.getBegin();
81 return region.getEnd();
85 * Formats the node as a bracketed list e.g.
88 * [1-100 [10-30 [10-20]], 15-30 [20-20]]
92 public String toString() {
93 StringBuilder sb = new StringBuilder(10 * size);
94 sb.append(region.getBegin()).append("-").append(region.getEnd());
95 if (subregions != null)
97 sb.append(" ").append(subregions.toString());
102 void prettyPrint(StringBuilder sb, int offset, int indent) {
103 for (int i = 0 ; i < offset ; i++) {
106 sb.append(region.getBegin()).append("-").append(region.getEnd());
107 if (subregions != null)
109 sb.append(System.lineSeparator());
110 subregions.prettyPrint(sb, offset + 2, indent);
114 * Add any ranges that overlap the from-to range to the result list
120 void findOverlaps(long from, long to, List<V> result)
122 if (region.getBegin() <= to && region.getEnd() >= from)
126 if (subregions != null)
128 subregions.findOverlaps(from, to, result);
133 * Add one range to this subrange
137 synchronized void add(V entry)
139 if (entry.getBegin() < region.getBegin() || entry.getEnd() > region.getEnd()) {
140 throw new IllegalArgumentException(String.format(
141 "adding improper subrange %d-%d to range %d-%d",
142 entry.getBegin(), entry.getEnd(), region.getBegin(),
145 if (subregions == null)
147 subregions = new NCList<V>(entry);
151 subregions.add(entry);
157 * Answers true if the data held satisfy the rules of construction of an
158 * NCList, else false.
165 * we don't handle reverse ranges
167 if (region != null && region.getBegin() > region.getEnd())
171 if (subregions == null)
175 return subregions.isValid(getBegin(), getEnd());
179 * Adds all contained entries to the given list
183 void getEntries(List<V> entries)
186 if (subregions != null)
188 subregions.getEntries(entries);
193 * Answers true if this object contains the given entry (by object equals
199 boolean contains(V entry)
205 if (entry.equals(region))
209 return subregions == null ? false : subregions.contains(entry);
213 * Answers the 'root' region modelled by this object
223 * Answers the (possibly null) contained regions within this object
227 NCList<V> getSubRegions()
233 * Nulls the subregion reference if it is empty (after a delete entry
236 void deleteSubRegionsIfEmpty()
238 if (subregions != null && subregions.size() == 0)
245 * Answers the (deep) size of this node i.e. the number of ranges it models