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

File AbstractContainerMarker.java

 

Coverage histogram

../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

46
66
13
1
231
160
36
0.55
5.08
13
2.77

Classes

Class Line # Actions
AbstractContainerMarker 36 66 0% 36 65
0.4848%
 

Contributing tests

This file is covered by 209 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.marker;
21   
22    import java.util.Collections;
23    import java.util.Iterator;
24    import java.util.LinkedHashSet;
25    import java.util.Set;
26   
27    import org.slf4j.Marker;
28   
29    /**
30    * Base class to use for custom {@link Marker}s which contains values (so which are a bit more than {@link Marker} as
31    * defined by SLF4J). For "real" marker use {@link org.slf4j.MarkerFactory#getMarker(String)}.
32    *
33    * @version $Id: ba00c7b0ffa031a62a432abb40296fc5f9a2ce84 $
34    * @since 5.4M1
35    */
 
36    public abstract class AbstractContainerMarker implements ContainerMarker
37    {
38    private static final long serialVersionUID = 1L;
39   
40    private static final String OPEN = "[ ";
41   
42    private static final String CLOSE = " ]";
43   
44    private static final String SEP = ", ";
45   
46    private final String name;
47   
48    private Set<Marker> references;
49   
50    /**
51    * @param name the name of the {@link Marker}
52    * @param references the other associated markers
53    */
 
54  223844 toggle public AbstractContainerMarker(String name, Marker... references)
55    {
56  223811 if (name == null) {
57  0 throw new IllegalArgumentException("A marker name cannot be null");
58    }
59   
60  223812 this.name = name;
61   
62  223801 for (Marker reference : references) {
63  744 add(reference);
64    }
65    }
66   
 
67  747 toggle @Override
68    public String getName()
69    {
70  747 return this.name;
71    }
72   
 
73  1 toggle @Override
74    public <M extends Marker> M get(String name)
75    {
76  1 if (this.name.equals(name)) {
77  1 return (M) this;
78    }
79   
80  0 if (this.references != null) {
81  0 for (Marker marker : this.references) {
82  0 if (marker.getName().equals(name)) {
83  0 return (M) marker;
84    }
85   
86  0 if (marker instanceof ContainerMarker) {
87  0 Marker targetMarker = ((ContainerMarker) marker).get(name);
88   
89  0 if (targetMarker != null) {
90  0 return (M) targetMarker;
91    }
92    }
93    }
94    }
95   
96  0 return null;
97    }
98   
 
99  745 toggle @Override
100    public synchronized void add(Marker reference)
101    {
102  745 if (reference == null) {
103  0 throw new IllegalArgumentException("A null value cannot be added to a Marker as reference.");
104    }
105   
106  745 if (!reference.contains(this)) {
107  745 if (this.references == null) {
108  745 this.references = new LinkedHashSet<Marker>();
109    }
110   
111  745 this.references.add(reference);
112    }
113   
114    }
115   
 
116  2 toggle @Override
117    public synchronized boolean hasReferences()
118    {
119  2 return this.references != null && this.references.size() > 0;
120    }
121   
 
122  1 toggle @Override
123    public boolean hasChildren()
124    {
125  1 return hasReferences();
126    }
127   
 
128  1 toggle @Override
129    public synchronized Iterator<Marker> iterator()
130    {
131  1 if (this.references != null) {
132  0 return this.references.iterator();
133    } else {
134  1 return Collections.<Marker>emptyList().iterator();
135    }
136    }
137   
 
138  1 toggle @Override
139    public synchronized boolean remove(Marker referenceToRemove)
140    {
141  1 if (this.references == null) {
142  1 return false;
143    }
144   
145  0 return this.references.remove(referenceToRemove);
146    }
147   
 
148  4799 toggle @Override
149    public boolean contains(Marker other)
150    {
151  4799 if (equals(other)) {
152  1 return true;
153    }
154   
155  4798 if (this.references != null) {
156  3313 for (Marker marker : this.references) {
157  3313 if (marker.contains(other)) {
158  2204 return true;
159    }
160    }
161    }
162   
163  2594 return false;
164    }
165   
 
166  6 toggle @Override
167    public boolean contains(String name)
168    {
169  6 if (this.name.equals(name)) {
170  1 return true;
171    }
172   
173  5 if (this.references != null) {
174  4 for (Marker marker : this.references) {
175  4 if (marker.contains(name)) {
176  4 return true;
177    }
178    }
179    }
180   
181  1 return false;
182    }
183   
 
184  0 toggle @Override
185    public boolean equals(Object obj)
186    {
187  0 if (this == obj) {
188  0 return true;
189    }
190   
191  0 if (obj == null) {
192  0 return false;
193    }
194   
195  0 if (!(obj instanceof Marker)) {
196  0 return false;
197    }
198   
199  0 final Marker other = (Marker) obj;
200  0 return this.name.equals(other.getName());
201    }
202   
 
203  0 toggle @Override
204    public int hashCode()
205    {
206  0 return this.name.hashCode();
207    }
208   
 
209  0 toggle @Override
210    public String toString()
211    {
212  0 if (!this.hasReferences()) {
213  0 return this.getName();
214    }
215   
216  0 StringBuilder sb = new StringBuilder(this.getName());
217  0 sb.append(' ').append(OPEN);
218   
219  0 Iterator<Marker> it = this.iterator();
220  0 while (it.hasNext()) {
221  0 Marker reference = it.next();
222  0 sb.append(reference.getName());
223  0 if (it.hasNext()) {
224  0 sb.append(SEP);
225    }
226    }
227  0 sb.append(CLOSE);
228   
229  0 return sb.toString();
230    }
231    }