KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > slf4j > helpers > BasicMarker


1 /*
2  * Copyright (c) 2004-2007 QOS.ch
3  * All rights reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24
25 package org.slf4j.helpers;
26
27 import java.util.Collections JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import org.slf4j.Marker;
33
34
35 /**
36  * An almost trivial implementation of the {@link Marker} interface.
37  *
38  * <p><code>BasicMarker</code> lets users specify marker
39  * information. However, it does not offer any useful operations on
40  * that information.
41  *
42  * <p>Simple logging systems which ignore marker data, just return
43  * instances of this class in order to conform to the SLF4J API.
44  *
45  * @author Ceki G&uuml;lc&uuml;
46  */

47 public class BasicMarker implements Marker {
48
49   private static final long serialVersionUID = 1803952589649545191L;
50
51   final String JavaDoc name;
52   List JavaDoc children;
53   
54   BasicMarker(String JavaDoc name) {
55     this.name = name;
56   }
57
58   public String JavaDoc getName() {
59     return name;
60   }
61
62   public synchronized void add(Marker child) {
63     if (child == null) {
64       throw new NullPointerException JavaDoc(
65         "Null children cannot be added to a Marker.");
66     }
67     if (children == null) {
68       children = new Vector JavaDoc();
69     }
70     children.add(child);
71   }
72
73   public synchronized boolean hasChildren() {
74     return ((children != null) && (children.size() > 0));
75   }
76
77   public synchronized Iterator JavaDoc iterator() {
78     if (children != null) {
79       return children.iterator();
80     } else {
81       return Collections.EMPTY_LIST.iterator();
82     }
83   }
84
85   public synchronized boolean remove(Marker markerToRemove) {
86     if (children == null) {
87       return false;
88     }
89
90     int size = children.size();
91     for (int i = 0; i < size; i++) {
92       Marker m = (Marker) children.get(i);
93       if( m == markerToRemove) {
94           return false;
95       }
96     }
97     // could not find markerToRemove
98
return false;
99   }
100   
101   public boolean contains(Marker other) {
102     if(other == null) {
103       throw new IllegalArgumentException JavaDoc("Other cannot be null");
104     }
105     
106     if(this == other) {
107       return true;
108     }
109     
110     if (hasChildren()) {
111       for(int i = 0; i < children.size(); i++) {
112         Marker child = (Marker) children.get(i);
113         if(child.contains(other)) {
114           return true;
115         }
116       }
117     }
118     return false;
119   }
120   
121   /**
122    * This method is mainly used with Expression Evaluators.
123    */

124   public boolean contains(String JavaDoc name) {
125     if(name == null) {
126       throw new IllegalArgumentException JavaDoc("Other cannot be null");
127     }
128     
129     if (this.name.equals(name)) {
130       return true;
131     }
132     
133     if (hasChildren()) {
134       for(int i = 0; i < children.size(); i++) {
135         Marker child = (Marker) children.get(i);
136         if(child.contains(name)) {
137           return true;
138         }
139       }
140     }
141     return false;
142   }
143
144   private static String JavaDoc OPEN = "[ ";
145   private static String JavaDoc CLOSE = " ]";
146   private static String JavaDoc SEP = ", ";
147
148   public String JavaDoc toString() {
149       
150     if (!this.hasChildren()) {
151       return this.getName();
152     }
153     
154     Iterator JavaDoc it = this.iterator();
155     Marker child;
156     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(this.getName());
157     sb.append(' ').append(OPEN);
158     while(it.hasNext()) {
159       child = (Marker)it.next();
160       sb.append(child.getName());
161       if (it.hasNext()) {
162         sb.append(SEP);
163       }
164     }
165     sb.append(CLOSE);
166       
167     return sb.toString();
168   }
169 }
170
Popular Tags