1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.wikimodel.util

File TreeBuilder.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

28
52
11
3
188
130
28
0.54
4.73
3.67
2.55

Classes

Class Line # Actions
TreeBuilder 32 52 0% 28 19
0.791208879.1%
TreeBuilder.IPos 39 0 - 0 0
-1.0 -
TreeBuilder.ITreeListener 53 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 1478 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.wikimodel.util;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    /**
26    * This is an internal utility class used as a context to keep in memory the
27    * current state of parsed trees (list items).
28    *
29    * @version $Id: a65caf30ecb590902e0b74aa4117a8bfc683ee12 $
30    * @since 4.0M1
31    */
 
32    public final class TreeBuilder<X extends TreeBuilder.IPos<X>>
33    {
34    /**
35    * This interface identifies position of elements in rows.
36    *
37    * @author MikhailKotelnikov
38    */
 
39    public interface IPos<X extends IPos<X>>
40    {
41    /**
42    * @return <code>true</code> if the underlying data in both positions
43    * are the same
44    */
45    boolean equalsData(X pos);
46   
47    /**
48    * @return the position of the node
49    */
50    int getPos();
51    }
52   
 
53    public interface ITreeListener<X extends IPos<X>>
54    {
55    void onBeginRow(X n);
56   
57    void onBeginTree(X n);
58   
59    void onEndRow(X n);
60   
61    void onEndTree(X n);
62    }
63   
 
64  96647 toggle private static <X extends IPos<X>> void addTail(
65    ITreeListener<X> listener,
66    List<X> firstArray,
67    List<X> secondArray,
68    int secondPos,
69    boolean openTree)
70    {
71  96652 X n = getNode(secondArray, secondPos);
72  96655 if (n == null) {
73  61830 return;
74    }
75  34828 if (openTree) {
76  30534 listener.onBeginTree(n);
77    }
78  34828 listener.onBeginRow(n);
79  34825 firstArray.add(n);
80  34826 addTail(listener, firstArray, secondArray, secondPos + 1, true);
81    }
82   
 
83  61827 toggle private static <X extends IPos<X>> void doAlign(
84    ITreeListener<X> listener,
85    List<X> firstArray,
86    List<X> secondArray,
87    boolean expand)
88    {
89  61826 boolean newTree = true;
90  61835 int f;
91  61832 int s;
92  61832 int firstLen = firstArray.size();
93  61828 int secondLen = secondArray.size();
94  68333 for (f = 0, s = 0; f < firstLen && s < secondLen; f++) {
95  10820 X first = firstArray.get(f);
96  10820 X second = secondArray.get(s);
97  10820 int firstPos = first.getPos();
98  10820 int secondPos = second.getPos();
99  10820 if (firstPos >= secondPos) {
100  10746 if (!first.equalsData(second)) {
101  20 break;
102  10726 } else if (s == secondLen - 1) {
103  4294 newTree = false;
104  4294 break;
105    }
106  6432 s++;
107    }
108    }
109  61830 removeTail(listener, firstArray, f, newTree, expand);
110  61826 if (expand) {
111  61831 addTail(listener, firstArray, secondArray, s, newTree);
112    }
113    }
114   
 
115  193298 toggle private static <X extends IPos<X>> X getNode(List<X> list, int pos)
116    {
117  193315 return pos < 0 || pos >= list.size() ? null : list.get(pos);
118    }
119   
 
120  96649 toggle private static <X extends IPos<X>> void removeTail(
121    ITreeListener<X> listener,
122    List<X> array,
123    int pos,
124    boolean closeTree,
125    boolean remove)
126    {
127  96648 X node = getNode(array, pos);
128  96655 if (node == null) {
129  61832 return;
130    }
131  34826 removeTail(listener, array, pos + 1, true, true);
132  34828 listener.onEndRow(node);
133  34825 if (closeTree) {
134  30528 listener.onEndTree(node);
135    }
136  34825 if (remove) {
137  34825 array.remove(pos);
138    }
139    }
140   
141    public List<X> fList = new ArrayList<X>();
142   
143    private ITreeListener<X> fListener;
144   
 
145  27411 toggle public TreeBuilder(ITreeListener<X> listener)
146    {
147  27412 super();
148  27410 fListener = listener;
149    }
150   
 
151  61826 toggle public void align(List<X> row)
152    {
153  61830 doAlign(fListener, fList, row, true);
154    }
155   
 
156  0 toggle public void align(X pos)
157    {
158  0 List<X> list = new ArrayList<X>();
159  0 if (pos != null) {
160  0 list.add(pos);
161    }
162  0 align(list);
163    }
164   
 
165  1220 toggle public X get(int pos)
166    {
167  1220 return pos >= 0 && pos < fList.size() ? fList.get(pos) : null;
168    }
169   
 
170  1220 toggle public X getPeek()
171    {
172  1220 return get(fList.size() - 1);
173    }
174   
 
175  0 toggle public void trim(List<X> row)
176    {
177  0 doAlign(fListener, fList, row, false);
178    }
179   
 
180  0 toggle public void trim(X pos)
181    {
182  0 List<X> list = new ArrayList<X>();
183  0 if (pos != null) {
184  0 list.add(pos);
185    }
186  0 trim(list);
187    }
188    }