1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
36
37
38
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("A null value cannot be added to a Marker as reference.");
61 }
62
63
64 if (this.contains(reference)) {
65 return;
66
67 } else if (reference.contains(this)) {
68
69 return;
70 } else {
71
72 if (referenceList == null) {
73 referenceList = new Vector<Marker>();
74 }
75 referenceList.add(reference);
76 }
77
78 }
79
80 public synchronized boolean hasReferences() {
81 return ((referenceList != null) && (referenceList.size() > 0));
82 }
83
84 public boolean hasChildren() {
85 return hasReferences();
86 }
87
88 public synchronized Iterator<Marker> iterator() {
89 if (referenceList != null) {
90 return referenceList.iterator();
91 } else {
92 List<Marker> emptyList = Collections.emptyList();
93 return emptyList.iterator();
94 }
95 }
96
97 public synchronized boolean remove(Marker referenceToRemove) {
98 if (referenceList == null) {
99 return false;
100 }
101
102 int size = referenceList.size();
103 for (int i = 0; i < size; i++) {
104 Marker m = referenceList.get(i);
105 if (referenceToRemove.equals(m)) {
106 referenceList.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 (Marker ref : referenceList) {
124 if (ref.contains(other)) {
125 return true;
126 }
127 }
128 }
129 return false;
130 }
131
132
133
134
135 public boolean contains(String name) {
136 if (name == null) {
137 throw new IllegalArgumentException("Other cannot be null");
138 }
139
140 if (this.name.equals(name)) {
141 return true;
142 }
143
144 if (hasReferences()) {
145 for (Marker ref : referenceList) {
146 if (ref.contains(name)) {
147 return true;
148 }
149 }
150 }
151 return false;
152 }
153
154 private static String OPEN = "[ ";
155 private static String CLOSE = " ]";
156 private static String SEP = ", ";
157
158 public boolean equals(Object obj) {
159 if (this == obj)
160 return true;
161 if (obj == null)
162 return false;
163 if (!(obj instanceof Marker))
164 return false;
165
166 final Marker other = (Marker) obj;
167 return name.equals(other.getName());
168 }
169
170 public int hashCode() {
171 return name.hashCode();
172 }
173
174 public String toString() {
175 if (!this.hasReferences()) {
176 return this.getName();
177 }
178 Iterator<Marker> it = this.iterator();
179 Marker reference;
180 StringBuilder sb = new StringBuilder(this.getName());
181 sb.append(' ').append(OPEN);
182 while (it.hasNext()) {
183 reference = it.next();
184 sb.append(reference.getName());
185 if (it.hasNext()) {
186 sb.append(SEP);
187 }
188 }
189 sb.append(CLOSE);
190
191 return sb.toString();
192 }
193 }