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

File LogTreeNode.java

 

Coverage histogram

../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

24
40
12
2
209
112
27
0.68
3.33
6
2.25

Classes

Class Line # Actions
LogTreeNode 38 30 0% 20 28
0.5172413651.7%
LogTreeNode.LogTreeNodeIterator 40 10 0% 7 2
0.888888988.9%
 

Contributing tests

This file is covered by 3 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.logging;
21   
22    import java.io.Serializable;
23    import java.util.Collections;
24    import java.util.Iterator;
25    import java.util.LinkedList;
26    import java.util.List;
27    import java.util.concurrent.ConcurrentLinkedQueue;
28   
29    import org.xwiki.logging.event.BeginLogEvent;
30    import org.xwiki.logging.event.LogEvent;
31   
32    /**
33    * Logs organized as a tree.
34    *
35    * @version $Id: b4dbb8f2f35dcd4e2a51854490b422d1991d307c $
36    * @since 5.4M1
37    */
 
38    public class LogTreeNode extends BeginLogEvent implements Iterable<LogEvent>, Serializable
39    {
 
40    private static class LogTreeNodeIterator implements Iterator<LogEvent>
41    {
42    private final Iterator<LogEvent> rootIterator;
43   
44    private Iterator<LogEvent> currentIterator;
45   
 
46  6 toggle LogTreeNodeIterator(Iterator<LogEvent> rootIterator)
47    {
48  6 this.rootIterator = rootIterator;
49  6 this.currentIterator = this.rootIterator;
50    }
51   
 
52  59 toggle @Override
53    public boolean hasNext()
54    {
55  59 return this.currentIterator.hasNext() || this.rootIterator.hasNext();
56    }
57   
 
58  115 toggle @Override
59    public LogEvent next()
60    {
61  115 if (!this.currentIterator.hasNext()) {
62  4 this.currentIterator = this.rootIterator;
63    }
64   
65  115 LogEvent logEvent = this.currentIterator.next();
66   
67  115 if (this.currentIterator == this.rootIterator && logEvent instanceof LogTreeNode) {
68  5 this.currentIterator = ((LogTreeNode) logEvent).iterator(true);
69    }
70   
71  115 return logEvent;
72    }
73   
 
74  0 toggle @Override
75    public void remove()
76    {
77  0 this.currentIterator.remove();
78    }
79    }
80   
81    /**
82    * Serialization identifier.
83    */
84    private static final long serialVersionUID = 1L;
85   
86    /**
87    * The children of this log event.
88    */
89    protected ConcurrentLinkedQueue<LogEvent> children;
90   
91    // Iterable
92   
 
93  3 toggle LogTreeNode()
94    {
95   
96    }
97   
98    /**
99    * @param logEvent the log event to copy
100    */
 
101  9 toggle public LogTreeNode(LogEvent logEvent)
102    {
103  9 super(logEvent);
104    }
105   
 
106  17 toggle @Override
107    public Iterator<LogEvent> iterator()
108    {
109  17 return this.children != null ? this.children.iterator() : Collections.<LogEvent>emptyList().iterator();
110    }
111   
112    /**
113    * @param recurse if true navigate through the whole tree, otherwise only the first level
114    * @return an iterator over a tree of logs
115    */
 
116  6 toggle public Iterator<LogEvent> iterator(boolean recurse)
117    {
118  6 if (!recurse) {
119  0 return iterator();
120    }
121   
122  6 return new LogTreeNodeIterator(iterator());
123    }
124   
125    /**
126    * The number of logs.
127    *
128    * @param recurse if true navigate through the whole tree, otherwise only the first level
129    * @return the number of log events
130    */
 
131  13 toggle public int size(boolean recurse)
132    {
133  13 if (!recurse) {
134  4 return this.children != null ? this.children.size() : 0;
135    }
136   
137  9 int size = 0;
138   
139  9 for (LogEvent logEvent : this) {
140  25 ++size;
141   
142  25 if (logEvent instanceof LogTreeNode) {
143  5 size += ((LogTreeNode) logEvent).size(true);
144    }
145    }
146   
147  9 return size;
148    }
149   
150    /**
151    * @param logEvent the log event to store
152    */
 
153  76 toggle void add(LogEvent logEvent)
154    {
155  76 if (this.children == null) {
156  12 this.children = new ConcurrentLinkedQueue<LogEvent>();
157    }
158   
159  76 this.children.add(logEvent);
160    }
161   
162    /**
163    * Filter logs of a specific level.
164    *
165    * @param level the level of the logs to return
166    * @param recurse if one of the {@link LogEvent} is a node look at its children too etc.
167    * @return the filtered logs
168    */
 
169  0 toggle public List<LogEvent> getLogs(LogLevel level, boolean recurse)
170    {
171  0 List<LogEvent> levelLogs = new LinkedList<LogEvent>();
172   
173  0 for (LogEvent log : this) {
174  0 if (log.getLevel() == level) {
175  0 levelLogs.add(log);
176    }
177   
178  0 if (recurse && log instanceof LogTreeNode) {
179  0 levelLogs.addAll(((LogTreeNode) log).getLogs(level, true));
180    }
181    }
182   
183  0 return levelLogs;
184    }
185   
186    /**
187    * Filter logs of a specific level.
188    *
189    * @param level the level of the logs to return
190    * @param recurse if one of the {@link LogEvent} is a node look at its children too etc.
191    * @return the filtered logs
192    */
 
193  0 toggle public List<LogEvent> getLogsFrom(LogLevel level, boolean recurse)
194    {
195  0 List<LogEvent> levelLogs = new LinkedList<LogEvent>();
196   
197  0 for (LogEvent log : this) {
198  0 if (log.getLevel().compareTo(level) <= 0) {
199  0 levelLogs.add(log);
200    }
201   
202  0 if (recurse && log instanceof LogTreeNode) {
203  0 levelLogs.addAll(((LogTreeNode) log).getLogsFrom(level, true));
204    }
205    }
206   
207  0 return levelLogs;
208    }
209    }