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 final List<Marker> referenceList = new CopyOnWriteArrayList<>();
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 @Deprecated
78 public boolean hasChildren() {
79 return hasReferences();
80 }
81
82 public Iterator<Marker> iterator() {
83 return referenceList.iterator();
84 }
85
86 public boolean remove(Marker referenceToRemove) {
87 return referenceList.remove(referenceToRemove);
88 }
89
90 public boolean contains(Marker other) {
91 if (other == null) {
92 throw new IllegalArgumentException("Other cannot be null");
93 }
94
95 if (this.equals(other)) {
96 return true;
97 }
98
99 if (hasReferences()) {
100 for (Marker ref : referenceList) {
101 if (ref.contains(other)) {
102 return true;
103 }
104 }
105 }
106 return false;
107 }
108
109
110
111
112 public boolean contains(String name) {
113 if (name == null) {
114 throw new IllegalArgumentException("Other cannot be null");
115 }
116
117 if (this.name.equals(name)) {
118 return true;
119 }
120
121 if (hasReferences()) {
122 for (Marker ref : referenceList) {
123 if (ref.contains(name)) {
124 return true;
125 }
126 }
127 }
128 return false;
129 }
130
131 private static final String OPEN = "[ ";
132 private static final String CLOSE = " ]";
133 private static final String SEP = ", ";
134
135 public boolean equals(Object obj) {
136 if (this == obj)
137 return true;
138 if (obj == null)
139 return false;
140 if (!(obj instanceof Marker))
141 return false;
142
143 final Marker other = (Marker) obj;
144 return name.equals(other.getName());
145 }
146
147 public int hashCode() {
148 return name.hashCode();
149 }
150
151 public String toString() {
152 if (!this.hasReferences()) {
153 return this.getName();
154 }
155 Iterator<Marker> it = this.iterator();
156 Marker reference;
157 StringBuilder sb = new StringBuilder(this.getName());
158 sb.append(' ').append(OPEN);
159 while (it.hasNext()) {
160 reference = it.next();
161 sb.append(reference.getName());
162 if (it.hasNext()) {
163 sb.append(SEP);
164 }
165 }
166 sb.append(CLOSE);
167
168 return sb.toString();
169 }
170 }