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

File TreeBuilder1.java

 

Coverage histogram

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

Code metrics

14
31
6
3
129
80
16
0.52
5.17
2
2.67

Classes

Class Line # Actions
TreeBuilder1 29 29 0% 15 0
1.0100%
TreeBuilder1.ITreeBuilderListener 31 0 - 0 0
-1.0 -
TreeBuilder1.Slot 42 2 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.tmp;
21   
22    import java.util.ArrayDeque;
23    import java.util.Deque;
24   
25    /**
26    * @version $Id: 7702d6e8df569d122229c1bfec8f0a5732ad22c0 $
27    * @since 4.0M1
28    */
 
29    public class TreeBuilder1<T>
30    {
 
31    public interface ITreeBuilderListener<T>
32    {
33    void beginItem(int depth, T data);
34   
35    void beginLevel(int depth, T prev);
36   
37    void endItem(int depth, T data);
38   
39    void endLevel(int i, T prev);
40    }
41   
 
42    private static class Slot<T>
43    {
44    private final T data;
45   
46    private final int val;
47   
 
48  7 toggle public Slot(int pos, T data)
49    {
50  7 this.val = pos;
51  7 this.data = data;
52    }
53    }
54   
55    private static final int MIN = Integer.MIN_VALUE;
56   
57    private boolean fIn;
58   
59    private boolean fInitialized;
60   
61    private ITreeBuilderListener<T> fListener;
62   
63    private T fPrev;
64   
65    private Deque<Slot<T>> fStack = new ArrayDeque<Slot<T>>();
66   
 
67  1 toggle public TreeBuilder1(ITreeBuilderListener<T> listener)
68    {
69  1 fListener = listener;
70    }
71   
 
72  7 toggle public void align(int val, T data)
73    {
74  7 trim(val, true);
75  7 if (!fInitialized) {
76  1 fInitialized = true;
77  1 fIn = true;
78  1 val = MIN;
79    }
80  7 Slot<T> slot = new Slot<T>(val, data);
81  7 if (fIn) {
82  3 fListener.beginLevel(fStack.size(), data);
83    }
84  7 fStack.push(slot);
85  7 fListener.beginItem(fStack.size(), data);
86  7 fPrev = data;
87  7 fIn = true;
88    }
89   
 
90  1 toggle public void finish()
91    {
92  1 trim(MIN, true);
93    }
94   
95    /**
96    * @param val
97    */
 
98  1 toggle public void trim(int val)
99    {
100  1 trim(val, true);
101    }
102   
103    /**
104    * @param val
105    */
 
106  10 toggle public void trim(int val, boolean includeValue)
107    {
108  15 while (!fStack.isEmpty()) {
109  14 Slot<T> peek = fStack.peek();
110  14 if (peek.val < val || peek.val == val && !includeValue) {
111  7 break;
112    }
113  7 if (!fIn) {
114  2 fListener.endLevel(fStack.size(), fPrev);
115    }
116  7 fStack.pop();
117  7 fListener.endItem(fStack.size(), peek.data);
118  7 fIn = false;
119  7 fPrev = peek.data;
120   
121  7 if (peek.val == val) {
122  2 break;
123    }
124    }
125  10 if (fInitialized && fStack.isEmpty()) {
126  1 fListener.endLevel(fStack.size(), fPrev);
127    }
128    }
129    }