View Javadoc

1   /**
2    * Copyright (c) 2004-2011 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;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Vector;
31  
32  import org.slf4j.Marker;
33  
34  /**
35   * A simple implementation of the {@link Marker} interface.
36   * 
37   * @author Ceki Gülcü
38   * @author Joern Huxhorn
39   */
40  public class BasicMarker implements Marker {
41  
42    private static final long serialVersionUID = 1803952589649545191L;
43  
44    private final String name;
45    private List refereceList;
46  
47    BasicMarker(String name) {
48      if (name == null) {
49        throw new IllegalArgumentException("A marker name cannot be null");
50      }
51      this.name = name;
52    }
53  
54    public String getName() {
55      return name;
56    }
57  
58    public synchronized void add(Marker reference) {
59      if (reference == null) {
60        throw new IllegalArgumentException(
61            "A null value cannot be added to a Marker as reference.");
62      }
63  
64      // no point in adding the reference multiple times
65      if (this.contains(reference)) {
66        return;
67  
68      } else if (reference.contains(this)) { // avoid recursion
69        // a potential reference should not its future "parent" as a reference
70        return;
71      } else {
72        // let's add the reference
73        if (refereceList == null) {
74          refereceList = new Vector();
75        }
76        refereceList.add(reference);
77      }
78  
79    }
80  
81    public synchronized boolean hasReferences() {
82      return ((refereceList != null) && (refereceList.size() > 0));
83    }
84    
85    public boolean hasChildren() {
86      return hasReferences();
87    }
88  
89    public synchronized Iterator iterator() {
90      if (refereceList != null) {
91        return refereceList.iterator();
92      } else {
93        return Collections.EMPTY_LIST.iterator();
94      }
95    }
96  
97    public synchronized boolean remove(Marker referenceToRemove) {
98      if (refereceList == null) {
99        return false;
100     }
101 
102     int size = refereceList.size();
103     for (int i = 0; i < size; i++) {
104       Marker m = (Marker) refereceList.get(i);
105       if (referenceToRemove.equals(m)) {
106         refereceList.remove(i);
107         return true;
108       }
109     }
110     return false;
111   }
112 
113   public boolean contains(Marker other) {
114     if (other == null) {
115       throw new IllegalArgumentException("Other cannot be null");
116     }
117 
118     if (this.equals(other)) {
119       return true;
120     }
121 
122     if (hasReferences()) {
123       for (int i = 0; i < refereceList.size(); i++) {
124         Marker ref = (Marker) refereceList.get(i);
125         if (ref.contains(other)) {
126           return true;
127         }
128       }
129     }
130     return false;
131   }
132 
133   /**
134    * This method is mainly used with Expression Evaluators.
135    */
136   public boolean contains(String name) {
137     if (name == null) {
138       throw new IllegalArgumentException("Other cannot be null");
139     }
140 
141     if (this.name.equals(name)) {
142       return true;
143     }
144 
145     if (hasReferences()) {
146       for (int i = 0; i < refereceList.size(); i++) {
147         Marker ref = (Marker) refereceList.get(i);
148         if (ref.contains(name)) {
149           return true;
150         }
151       }
152     }
153     return false;
154   }
155 
156   private static String OPEN = "[ ";
157   private static String CLOSE = " ]";
158   private static String SEP = ", ";
159 
160 
161   public boolean equals(Object obj) {
162     if (this == obj)
163       return true;
164     if (obj == null)
165       return false;
166     if (!(obj instanceof Marker))
167       return false;
168 
169     final Marker other = (Marker) obj;
170     return name.equals(other.getName());
171   }
172 
173   public int hashCode() {
174     return name.hashCode();
175   }
176 
177   public String toString() {
178     if (!this.hasReferences()) {
179       return this.getName();
180     }
181     Iterator it = this.iterator();
182     Marker reference;
183     StringBuffer sb = new StringBuffer(this.getName());
184     sb.append(' ').append(OPEN);
185     while (it.hasNext()) {
186       reference = (Marker) it.next();
187       sb.append(reference.getName());
188       if (it.hasNext()) {
189         sb.append(SEP);
190       }
191     }
192     sb.append(CLOSE);
193 
194     return sb.toString();
195   }
196 }