Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.datamodel.features

File NCNode.java

 

Coverage histogram

../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

32
53
17
1
275
149
37
0.7
3.12
17
2.18

Classes

Class Line # Actions
NCNode 34 53 37 2
0.9803921698%
 

Contributing tests

This file is covered by 45 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.datamodel.features;
22   
23    import jalview.datamodel.ContiguousI;
24   
25    import java.util.ArrayList;
26    import java.util.List;
27   
28    /**
29    * Each node of the NCList tree consists of a range, and (optionally) the NCList
30    * of ranges it encloses
31    *
32    * @param <V>
33    */
 
34    class NCNode<V extends ContiguousI> implements ContiguousI
35    {
36    /*
37    * deep size (number of ranges included)
38    */
39    private int size;
40   
41    private V region;
42   
43    /*
44    * null, or an object holding contained subregions of this nodes region
45    */
46    private NCList<V> subregions;
47   
48    /**
49    * Constructor given a list of ranges
50    *
51    * @param ranges
52    */
 
53  44 toggle NCNode(List<V> ranges)
54    {
55  44 build(ranges);
56    }
57   
58    /**
59    * Constructor given a single range
60    *
61    * @param range
62    */
 
63  363 toggle NCNode(V range)
64    {
65  363 List<V> ranges = new ArrayList<>();
66  363 ranges.add(range);
67  363 build(ranges);
68    }
69   
 
70  44 toggle NCNode(V entry, NCList<V> newNCList)
71    {
72  44 region = entry;
73  44 subregions = newNCList;
74  44 size = 1 + newNCList.size();
75    }
76   
77    /**
78    * @param ranges
79    */
 
80  407 toggle protected void build(List<V> ranges)
81    {
82  407 size = ranges.size();
83   
84  407 if (!ranges.isEmpty())
85    {
86  407 region = ranges.get(0);
87    }
88  407 if (ranges.size() > 1)
89    {
90  19 subregions = new NCList<V>(ranges.subList(1, ranges.size()));
91    }
92    }
93   
 
94  46099 toggle @Override
95    public int getBegin()
96    {
97  46099 return region.getBegin();
98    }
99   
 
100  32546 toggle @Override
101    public int getEnd()
102    {
103  32546 return region.getEnd();
104    }
105   
106    /**
107    * Formats the node as a bracketed list e.g.
108    *
109    * <pre>
110    * [1-100 [10-30 [10-20]], 15-30 [20-20]]
111    * </pre>
112    */
 
113  80 toggle @Override
114    public String toString() {
115  80 StringBuilder sb = new StringBuilder(10 * size);
116  80 sb.append(region.getBegin()).append("-").append(region.getEnd());
117  80 if (subregions != null)
118    {
119  23 sb.append(" ").append(subregions.toString());
120    }
121  80 return sb.toString();
122    }
123   
 
124  12 toggle void prettyPrint(StringBuilder sb, int offset, int indent) {
125  34 for (int i = 0 ; i < offset ; i++) {
126  22 sb.append(" ");
127    }
128  12 sb.append(region.getBegin()).append("-").append(region.getEnd());
129  12 if (subregions != null)
130    {
131  6 sb.append(System.lineSeparator());
132  6 subregions.prettyPrint(sb, offset + 2, indent);
133    }
134    }
135    /**
136    * Add any ranges that overlap the from-to range to the result list
137    *
138    * @param from
139    * @param to
140    * @param result
141    */
 
142  1062 toggle void findOverlaps(long from, long to, List<V> result)
143    {
144  1062 if (region.getBegin() <= to && region.getEnd() >= from)
145    {
146  1062 result.add(region);
147    }
148  1062 if (subregions != null)
149    {
150  704 subregions.findOverlaps(from, to, result);
151    }
152    }
153   
154    /**
155    * Add one range to this subrange
156    *
157    * @param entry
158    */
 
159  358 toggle synchronized void add(V entry)
160    {
161  358 if (entry.getBegin() < region.getBegin() || entry.getEnd() > region.getEnd()) {
162  2 throw new IllegalArgumentException(String.format(
163    "adding improper subrange %d-%d to range %d-%d",
164    entry.getBegin(), entry.getEnd(), region.getBegin(),
165    region.getEnd()));
166    }
167  356 if (subregions == null)
168    {
169  77 subregions = new NCList<V>(entry);
170    }
171    else
172    {
173  279 subregions.add(entry);
174    }
175  356 size++;
176    }
177   
178    /**
179    * Answers true if the data held satisfy the rules of construction of an
180    * NCList, else false.
181    *
182    * @return
183    */
 
184  4538 toggle boolean isValid()
185    {
186    /*
187    * we don't handle reverse ranges
188    */
189  4538 if (region != null && region.getBegin() > region.getEnd())
190    {
191  1 return false;
192    }
193  4537 if (subregions == null)
194    {
195  1984 return true;
196    }
197  2553 return subregions.isValid(getBegin(), getEnd());
198    }
199   
200    /**
201    * Adds all contained entries to the given list
202    *
203    * @param entries
204    */
 
205  617 toggle void getEntries(List<V> entries)
206    {
207  617 entries.add(region);
208  617 if (subregions != null)
209    {
210  193 subregions.getEntries(entries);
211    }
212    }
213   
214    /**
215    * Answers true if this object contains the given entry (by object equals
216    * test), else false
217    *
218    * @param entry
219    * @return
220    */
 
221  30633 toggle boolean contains(V entry)
222    {
223  30633 if (entry == null)
224    {
225  1 return false;
226    }
227  30632 if (entry.equals(region))
228    {
229  2175 return true;
230    }
231  28457 return subregions == null ? false : subregions.contains(entry);
232    }
233   
234    /**
235    * Answers the 'root' region modelled by this object
236    *
237    * @return
238    */
 
239  1344 toggle V getRegion()
240    {
241  1344 return region;
242    }
243   
244    /**
245    * Answers the (possibly null) contained regions within this object
246    *
247    * @return
248    */
 
249  1344 toggle NCList<V> getSubRegions()
250    {
251  1344 return subregions;
252    }
253   
254    /**
255    * Nulls the subregion reference if it is empty (after a delete entry
256    * operation)
257    */
 
258  189 toggle void deleteSubRegionsIfEmpty()
259    {
260  189 if (subregions != null && subregions.size() == 0)
261    {
262  14 subregions = null;
263    }
264    }
265   
266    /**
267    * Answers the (deep) size of this node i.e. the number of ranges it models
268    *
269    * @return
270    */
 
271  60 toggle int size()
272    {
273  60 return size;
274    }
275    }