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<Marker> referenceList;
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 (referenceList == null) {
74          referenceList = new Vector<Marker>();
75        }
76        referenceList.add(reference);
77      }
78  
79    }
80  
81    public synchronized boolean hasReferences() {
82      return ((referenceList != null) && (referenceList.size() > 0));
83    }
84    
85    public boolean hasChildren() {
86      return hasReferences();
87    }
88  
89    public synchronized Iterator<Marker> iterator() {
90      if (referenceList != null) {
91        return referenceList.iterator();
92      } else {
93        List<Marker> emptyList = Collections.emptyList();
94        return emptyList.iterator();
95      }
96    }
97  
98    public synchronized boolean remove(Marker referenceToRemove) {
99      if (referenceList == null) {
100       return false;
101     }
102 
103     int size = referenceList.size();
104     for (int i = 0; i < size; i++) {
105       Marker m = (Marker) referenceList.get(i);
106       if (referenceToRemove.equals(m)) {
107         referenceList.remove(i);
108         return true;
109       }
110     }
111     return false;
112   }
113 
114   public boolean contains(Marker other) {
115     if (other == null) {
116       throw new IllegalArgumentException("Other cannot be null");
117     }
118 
119     if (this.equals(other)) {
120       return true;
121     }
122 
123     if (hasReferences()) {
124       for (int i = 0; i < referenceList.size(); i++) {
125         Marker ref = (Marker) referenceList.get(i);
126         if (ref.contains(other)) {
127           return true;
128         }
129       }
130     }
131     return false;
132   }
133 
134   /**
135    * This method is mainly used with Expression Evaluators.
136    */
137   public boolean contains(String name) {
138     if (name == null) {
139       throw new IllegalArgumentException("Other cannot be null");
140     }
141 
142     if (this.name.equals(name)) {
143       return true;
144     }
145 
146     if (hasReferences()) {
147       for (int i = 0; i < referenceList.size(); i++) {
148         Marker ref = (Marker) referenceList.get(i);
149         if (ref.contains(name)) {
150           return true;
151         }
152       }
153     }
154     return false;
155   }
156 
157   private static String OPEN = "[ ";
158   private static String CLOSE = " ]";
159   private static String SEP = ", ";
160 
161 
162   public boolean equals(Object obj) {
163     if (this == obj)
164       return true;
165     if (obj == null)
166       return false;
167     if (!(obj instanceof Marker))
168       return false;
169 
170     final Marker other = (Marker) obj;
171     return name.equals(other.getName());
172   }
173 
174   public int hashCode() {
175     return name.hashCode();
176   }
177 
178   public String toString() {
179     if (!this.hasReferences()) {
180       return this.getName();
181     }
182     Iterator<Marker> it = this.iterator();
183     Marker reference;
184     StringBuilder sb = new StringBuilder(this.getName());
185     sb.append(' ').append(OPEN);
186     while (it.hasNext()) {
187       reference = (Marker) it.next();
188       sb.append(reference.getName());
189       if (it.hasNext()) {
190         sb.append(SEP);
191       }
192     }
193     sb.append(CLOSE);
194 
195     return sb.toString();
196   }
197 }