1 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 47 public class BasicMarker implements Marker { 48 49 private static final long serialVersionUID = 1803952589649545191L; 50 51 final String name; 52 List children; 53 54 BasicMarker(String name) { 55 this.name = name; 56 } 57 58 public String getName() { 59 return name; 60 } 61 62 public synchronized void add(Marker child) { 63 if (child == null) { 64 throw new NullPointerException ( 65 "Null children cannot be added to a Marker."); 66 } 67 if (children == null) { 68 children = new Vector (); 69 } 70 children.add(child); 71 } 72 73 public synchronized boolean hasChildren() { 74 return ((children != null) && (children.size() > 0)); 75 } 76 77 public synchronized Iterator iterator() { 78 if (children != null) { 79 return children.iterator(); 80 } else { 81 return Collections.EMPTY_LIST.iterator(); 82 } 83 } 84 85 public synchronized boolean remove(Marker markerToRemove) { 86 if (children == null) { 87 return false; 88 } 89 90 int size = children.size(); 91 for (int i = 0; i < size; i++) { 92 Marker m = (Marker) children.get(i); 93 if( m == markerToRemove) { 94 return false; 95 } 96 } 97 return false; 99 } 100 101 public boolean contains(Marker other) { 102 if(other == null) { 103 throw new IllegalArgumentException ("Other cannot be null"); 104 } 105 106 if(this == other) { 107 return true; 108 } 109 110 if (hasChildren()) { 111 for(int i = 0; i < children.size(); i++) { 112 Marker child = (Marker) children.get(i); 113 if(child.contains(other)) { 114 return true; 115 } 116 } 117 } 118 return false; 119 } 120 121 124 public boolean contains(String name) { 125 if(name == null) { 126 throw new IllegalArgumentException ("Other cannot be null"); 127 } 128 129 if (this.name.equals(name)) { 130 return true; 131 } 132 133 if (hasChildren()) { 134 for(int i = 0; i < children.size(); i++) { 135 Marker child = (Marker) children.get(i); 136 if(child.contains(name)) { 137 return true; 138 } 139 } 140 } 141 return false; 142 } 143 144 private static String OPEN = "[ "; 145 private static String CLOSE = " ]"; 146 private static String SEP = ", "; 147 148 public String toString() { 149 150 if (!this.hasChildren()) { 151 return this.getName(); 152 } 153 154 Iterator it = this.iterator(); 155 Marker child; 156 StringBuffer sb = new StringBuffer (this.getName()); 157 sb.append(' ').append(OPEN); 158 while(it.hasNext()) { 159 child = (Marker)it.next(); 160 sb.append(child.getName()); 161 if (it.hasNext()) { 162 sb.append(SEP); 163 } 164 } 165 sb.append(CLOSE); 166 167 return sb.toString(); 168 } 169 } 170 | Popular Tags |