26
27 import java.util.Hashtable;
28 import java.util.Enumeration;
29 import java.util.Collections;
30 import java.io.IOException;
31 import java.io.ObjectInputStream;
32 import java.io.Serializable;
33 import java.util.AbstractMap;
34 import java.util.LinkedHashMap;
35
36 /**
37 * A straightforward implementation of MutableAttributeSet using a
38 * hash table.
39 * <p>
40 * <strong>Warning:</strong>
41 * Serialized objects of this class will not be compatible with
42 * future Swing releases. The current serialization support is
43 * appropriate for short term storage or RMI between applications running
44 * the same version of Swing. As of 1.4, support for long term storage
45 * of all JavaBeans™
46 * has been added to the <code>java.beans</code> package.
47 * Please see {@link java.beans.XMLEncoder}.
48 *
49 * @author Tim Prinzing
50 */
51 @SuppressWarnings("serial") // Same-version serialization only
52 public class SimpleAttributeSet implements MutableAttributeSet, Serializable, Cloneable
53 {
54 private static final long serialVersionUID = -6631553454711782652L;
55
56 /**
57 * An empty attribute set.
58 */
59 public static final AttributeSet EMPTY = new EmptyAttributeSet();
60
61 private transient LinkedHashMap<Object, Object> table = new LinkedHashMap<>(3);
62
63 /**
64 * Creates a new attribute set.
65 */
66 public SimpleAttributeSet() {
110 * @param attr the second attribute set
111 * @return true if the sets are equal, false otherwise
112 */
113 public boolean isEqual(AttributeSet attr) {
114 return ((getAttributeCount() == attr.getAttributeCount()) &&
115 containsAttributes(attr));
116 }
117
118 /**
119 * Makes a copy of the attributes.
120 *
121 * @return the copy
122 */
123 public AttributeSet copyAttributes() {
124 return (AttributeSet) clone();
125 }
126
127 /**
128 * Gets the names of the attributes in the set.
129 *
130 * @return the names as an <code>Enumeration</code>
131 */
132 public Enumeration<?> getAttributeNames() {
133 return Collections.enumeration(table.keySet());
134 }
135
136 /**
137 * Gets the value of an attribute.
138 *
139 * @param name the attribute name
140 * @return the value
141 */
142 public Object getAttribute(Object name) {
143 Object value = table.get(name);
144 if (value == null) {
145 AttributeSet parent = getResolveParent();
146 if (parent != null) {
147 value = parent.getAttribute(name);
148 }
149 }
150 return value;
277 SimpleAttributeSet attr;
278 try {
279 attr = (SimpleAttributeSet) super.clone();
280 attr.table = (LinkedHashMap) table.clone();
281 } catch (CloneNotSupportedException cnse) {
282 attr = null;
283 }
284 return attr;
285 }
286
287 /**
288 * Returns a hashcode for this set of attributes.
289 * @return a hashcode value for this set of attributes.
290 */
291 public int hashCode() {
292 return table.hashCode();
293 }
294
295 /**
296 * Compares this object to the specified object.
297 * The result is <code>true</code> if the object is an equivalent
298 * set of attributes.
299 * @param obj the object to compare this attribute set with
300 * @return <code>true</code> if the objects are equal;
301 * <code>false</code> otherwise
302 */
303 public boolean equals(Object obj) {
304 if (this == obj) {
305 return true;
306 }
307 if (obj instanceof AttributeSet) {
308 AttributeSet attrs = (AttributeSet) obj;
309 return isEqual(attrs);
310 }
311 return false;
312 }
313
314 /**
315 * Converts the attribute set to a String.
316 *
317 * @return the string
318 */
319 public String toString() {
320 String s = "";
321 Enumeration<?> names = getAttributeNames();
|
26
27 import java.util.Hashtable;
28 import java.util.Enumeration;
29 import java.util.Collections;
30 import java.io.IOException;
31 import java.io.ObjectInputStream;
32 import java.io.Serializable;
33 import java.util.AbstractMap;
34 import java.util.LinkedHashMap;
35
36 /**
37 * A straightforward implementation of MutableAttributeSet using a
38 * hash table.
39 * <p>
40 * <strong>Warning:</strong>
41 * Serialized objects of this class will not be compatible with
42 * future Swing releases. The current serialization support is
43 * appropriate for short term storage or RMI between applications running
44 * the same version of Swing. As of 1.4, support for long term storage
45 * of all JavaBeans™
46 * has been added to the {@code java.beans} package.
47 * Please see {@link java.beans.XMLEncoder}.
48 *
49 * @author Tim Prinzing
50 */
51 @SuppressWarnings("serial") // Same-version serialization only
52 public class SimpleAttributeSet implements MutableAttributeSet, Serializable, Cloneable
53 {
54 private static final long serialVersionUID = -6631553454711782652L;
55
56 /**
57 * An empty attribute set.
58 */
59 public static final AttributeSet EMPTY = new EmptyAttributeSet();
60
61 private transient LinkedHashMap<Object, Object> table = new LinkedHashMap<>(3);
62
63 /**
64 * Creates a new attribute set.
65 */
66 public SimpleAttributeSet() {
110 * @param attr the second attribute set
111 * @return true if the sets are equal, false otherwise
112 */
113 public boolean isEqual(AttributeSet attr) {
114 return ((getAttributeCount() == attr.getAttributeCount()) &&
115 containsAttributes(attr));
116 }
117
118 /**
119 * Makes a copy of the attributes.
120 *
121 * @return the copy
122 */
123 public AttributeSet copyAttributes() {
124 return (AttributeSet) clone();
125 }
126
127 /**
128 * Gets the names of the attributes in the set.
129 *
130 * @return the names as an {@code Enumeration}
131 */
132 public Enumeration<?> getAttributeNames() {
133 return Collections.enumeration(table.keySet());
134 }
135
136 /**
137 * Gets the value of an attribute.
138 *
139 * @param name the attribute name
140 * @return the value
141 */
142 public Object getAttribute(Object name) {
143 Object value = table.get(name);
144 if (value == null) {
145 AttributeSet parent = getResolveParent();
146 if (parent != null) {
147 value = parent.getAttribute(name);
148 }
149 }
150 return value;
277 SimpleAttributeSet attr;
278 try {
279 attr = (SimpleAttributeSet) super.clone();
280 attr.table = (LinkedHashMap) table.clone();
281 } catch (CloneNotSupportedException cnse) {
282 attr = null;
283 }
284 return attr;
285 }
286
287 /**
288 * Returns a hashcode for this set of attributes.
289 * @return a hashcode value for this set of attributes.
290 */
291 public int hashCode() {
292 return table.hashCode();
293 }
294
295 /**
296 * Compares this object to the specified object.
297 * The result is {@code true} if the object is an equivalent
298 * set of attributes.
299 * @param obj the object to compare this attribute set with
300 * @return {@code true} if the objects are equal;
301 * {@code false} otherwise
302 */
303 public boolean equals(Object obj) {
304 if (this == obj) {
305 return true;
306 }
307 if (obj instanceof AttributeSet) {
308 AttributeSet attrs = (AttributeSet) obj;
309 return isEqual(attrs);
310 }
311 return false;
312 }
313
314 /**
315 * Converts the attribute set to a String.
316 *
317 * @return the string
318 */
319 public String toString() {
320 String s = "";
321 Enumeration<?> names = getAttributeNames();
|