1 43 44 package org.jfree.xml.writer; 45 46 import java.util.Iterator ; 47 import java.util.List ; 48 49 55 public class AttributeList { 56 57 60 private static class AttributeEntry { 61 62 63 private String name; 64 65 66 private String value; 67 68 74 public AttributeEntry(final String name, final String value) { 75 if (name == null) { 76 throw new NullPointerException ("Name must not be null. [" 77 + name + ", " + value + "]"); 78 } 79 if (value == null) { 80 throw new NullPointerException ("Value must not be null. [" 81 + name + ", " + value + "]"); 82 } 83 this.name = name; 84 this.value = value; 85 } 86 87 92 public String getName() { 93 return this.name; 94 } 95 96 101 public String getValue() { 102 return this.value; 103 } 104 105 112 public boolean equals(final Object o) { 113 if (this == o) { 114 return true; 115 } 116 if (!(o instanceof AttributeEntry)) { 117 return false; 118 } 119 120 final AttributeEntry attributeEntry = (AttributeEntry) o; 121 if (!this.name.equals(attributeEntry.name)) { 122 return false; 123 } 124 return true; 125 } 126 127 132 public int hashCode() { 133 return this.name.hashCode(); 134 } 135 } 136 137 140 private static class AttributeIterator implements Iterator { 141 142 143 private Iterator backend; 144 145 150 public AttributeIterator(final Iterator backend) { 151 if (backend == null) { 152 throw new NullPointerException (); 153 } 154 this.backend = backend; 155 } 156 157 164 public boolean hasNext() { 165 return this.backend.hasNext(); 166 } 167 168 173 public Object next() { 174 final AttributeEntry entry = (AttributeEntry) this.backend.next(); 175 if (entry != null) { 176 return entry.getName(); 177 } 178 return entry; 179 } 180 181 189 public void remove() { 190 this.backend.remove(); 191 } 192 } 193 194 195 private List entryList; 196 197 200 public AttributeList() { 201 this.entryList = new java.util.ArrayList (); 202 } 203 204 210 public Iterator keys() { 211 return new AttributeIterator(this.entryList.iterator()); 212 } 213 214 220 public synchronized void setAttribute(final String name, final String value) { 221 final AttributeEntry entry = new AttributeEntry(name, value); 222 final int pos = this.entryList.indexOf(entry); 223 if (pos != -1) { 224 this.entryList.remove(pos); 225 } 226 this.entryList.add(entry); 227 } 228 229 236 public synchronized String getAttribute(final String name) { 237 return getAttribute(name, null); 238 } 239 240 249 public synchronized String getAttribute(final String name, final String defaultValue) { 250 for (int i = 0; i < this.entryList.size(); i++) { 251 final AttributeEntry ae = (AttributeEntry) this.entryList.get(i); 252 if (ae.getName().equals(name)) { 253 return ae.getValue(); 254 } 255 } 256 return defaultValue; 257 } 258 259 264 public synchronized void removeAttribute(final String name) { 265 for (int i = 0; i < this.entryList.size(); i++) { 266 final AttributeEntry ae = (AttributeEntry) this.entryList.get(i); 267 if (ae.getName().equals(name)) { 268 this.entryList.remove(ae); 269 return; 270 } 271 } 272 } 273 } 274 | Popular Tags |