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.Iterator;
28 import java.util.List;
29 import java.util.concurrent.CopyOnWriteArrayList;
30
31 import org.slf4j.Marker;
32
33
34
35
36
37
38
39 public class BasicMarker implements Marker {
40
41 private static final long serialVersionUID = -2849567615646933777L;
42 private final String name;
43 private List<Marker> referenceList = new CopyOnWriteArrayList<Marker>();
44
45 BasicMarker(String name) {
46 if (name == null) {
47 throw new IllegalArgumentException("A marker name cannot be null");
48 }
49 this.name = name;
50 }
51
52 public String getName() {
53 return name;
54 }
55
56 public void add(Marker reference) {
57 if (reference == null) {
58 throw new IllegalArgumentException("A null value cannot be added to a Marker as reference.");
59 }
60
61
62 if (this.contains(reference)) {
63 return;
64
65 } else if (reference.contains(this)) {
66
67 return;
68 } else {
69 referenceList.add(reference);
70 }
71 }
72
73 public boolean hasReferences() {
74 return (referenceList.size() > 0);
75 }
76
77 public boolean hasChildren() {
78 return hasReferences();
79 }
80
81 public Iterator<Marker> iterator() {
82 return referenceList.iterator();
83 }
84
85 public boolean remove(Marker referenceToRemove) {
86 return referenceList.remove(referenceToRemove);
87 }
88
89 public boolean contains(Marker other) {
90 if (other == null) {
91 throw new IllegalArgumentException("Other cannot be null");
92 }
93
94 if (this.equals(other)) {
95 return true;
96 }
97
98 if (hasReferences()) {
99 for (Marker ref : referenceList) {
100 if (ref.contains(other)) {
101 return true;
102 }
103 }
104 }
105 return false;
106 }
107
108
109
110
111 public boolean contains(String name) {
112 if (name == null) {
113 throw new IllegalArgumentException("Other cannot be null");
114 }
115
116 if (this.name.equals(name)) {
117 return true;
118 }
119
120 if (hasReferences()) {
121 for (Marker ref : referenceList) {
122 if (ref.contains(name)) {
123 return true;
124 }
125 }
126 }
127 return false;
128 }
129
130 private static String OPEN = "[ ";
131 private static String CLOSE = " ]";
132 private static String SEP = ", ";
133
134 public boolean equals(Object obj) {
135 if (this == obj)
136 return true;
137 if (obj == null)
138 return false;
139 if (!(obj instanceof Marker))
140 return false;
141
142 final Marker other = (Marker) obj;
143 return name.equals(other.getName());
144 }
145
146 public int hashCode() {
147 return name.hashCode();
148 }
149
150 public String toString() {
151 if (!this.hasReferences()) {
152 return this.getName();
153 }
154 Iterator<Marker> it = this.iterator();
155 Marker reference;
156 StringBuilder sb = new StringBuilder(this.getName());
157 sb.append(' ').append(OPEN);
158 while (it.hasNext()) {
159 reference = it.next();
160 sb.append(reference.getName());
161 if (it.hasNext()) {
162 sb.append(SEP);
163 }
164 }
165 sb.append(CLOSE);
166
167 return sb.toString();
168 }
169 }