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>
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.getSize();
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()));
74 return region.getBegin();
79 return region.getEnd();
83 * Formats the node as a bracketed list e.g.
86 * [1-100 [10-30 [10-20]], 15-30 [20-20]]
90 public String toString() {
91 StringBuilder sb = new StringBuilder(10 * size);
92 sb.append(region.getBegin()).append("-").append(region.getEnd());
93 if (subregions != null)
95 sb.append(" ").append(subregions.toString());
100 void prettyPrint(StringBuilder sb, int offset, int indent) {
101 for (int i = 0 ; i < offset ; i++) {
104 sb.append(region.getBegin()).append("-").append(region.getEnd());
105 if (subregions != null)
107 sb.append(System.lineSeparator());
108 subregions.prettyPrint(sb, offset + 2, indent);
112 * Add any ranges that overlap the from-to range to the result list
118 void findOverlaps(long from, long to, List<V> result)
120 if (region.getBegin() <= to && region.getEnd() >= from)
124 if (subregions != null)
126 subregions.findOverlaps(from, to, result);
131 * Add one range to this subrange
135 synchronized void add(V entry)
137 if (entry.getBegin() < region.getBegin() || entry.getEnd() > region.getEnd()) {
138 throw new IllegalArgumentException(String.format(
139 "adding improper subrange %d-%d to range %d-%d",
140 entry.getBegin(), entry.getEnd(), region.getBegin(),
143 if (subregions == null)
145 subregions = new NCList<V>(entry);
149 subregions.add(entry);
154 * Answers true if the data held satisfy the rules of construction of an
155 * NCList, else false.
161 if (subregions == null)
165 return subregions.isValid(getStart(), getEnd());
169 * Adds all contained entries to the given list
173 void getEntries(List<V> entries)
176 if (subregions != null)
178 subregions.getEntries(entries);