HashAttributeSetpublic class HashAttributeSet extends Object implements Serializable, AttributeSetClass HashAttributeSet provides an AttributeSet
implementation with characteristics of a hash map.
|
Fields Summary |
---|
private static final long | serialVersionUID | private Class | myInterfaceThe interface of which all members of this attribute set must be an
instance. It is assumed to be interface {@link Attribute Attribute}
or a subinterface thereof. | private transient HashMap | attrMap |
Constructors Summary |
---|
protected HashAttributeSet(AttributeSet attributes, Class interfaceName)Construct a new attribute set, initially populated with the
values from the given set where the members of the attribute
set are restricted to the given interface.
myInterface = interfaceName;
if (attributes != null) {
Attribute[] attribArray = attributes.toArray();
int n = attribArray == null ? 0 : attribArray.length;
for (int i = 0; i < n; ++ i) {
add (attribArray[i]);
}
}
| public HashAttributeSet()Construct a new, empty attribute set.
this(Attribute.class);
| public HashAttributeSet(Attribute attribute)Construct a new attribute set,
initially populated with the given attribute.
this (attribute, Attribute.class);
| public HashAttributeSet(Attribute[] attributes)Construct a new attribute set,
initially populated with the values from the
given array. The new attribute set is populated by
adding the elements of attributes array to the set in
sequence, starting at index 0. Thus, later array elements may replace
earlier array elements if the array contains duplicate attribute
values or attribute categories.
this (attributes, Attribute.class);
| public HashAttributeSet(AttributeSet attributes)Construct a new attribute set,
initially populated with the values from the given set.
this (attributes, Attribute.class);
| protected HashAttributeSet(Class interfaceName)Construct a new, empty attribute set, where the members of
the attribute set are restricted to the given interface.
if (interfaceName == null) {
throw new NullPointerException("null interface");
}
myInterface = interfaceName;
| protected HashAttributeSet(Attribute attribute, Class interfaceName)Construct a new attribute set, initially populated with the given
attribute, where the members of the attribute set are restricted to the
given interface.
if (interfaceName == null) {
throw new NullPointerException("null interface");
}
myInterface = interfaceName;
add (attribute);
| protected HashAttributeSet(Attribute[] attributes, Class interfaceName)Construct a new attribute set, where the members of the attribute
set are restricted to the given interface.
The new attribute set is populated
by adding the elements of attributes array to the set in
sequence, starting at index 0. Thus, later array elements may replace
earlier array elements if the array contains duplicate attribute
values or attribute categories.
if (interfaceName == null) {
throw new NullPointerException("null interface");
}
myInterface = interfaceName;
int n = attributes == null ? 0 : attributes.length;
for (int i = 0; i < n; ++ i) {
add (attributes[i]);
}
|
Methods Summary |
---|
public boolean | add(javax.print.attribute.Attribute attribute)Adds the specified attribute to this attribute set if it is not
already present, first removing any existing in the same
attribute category as the specified attribute value.
Object oldAttribute =
attrMap.put(attribute.getCategory(),
AttributeSetUtilities.
verifyAttributeValue(attribute, myInterface));
return (!attribute.equals(oldAttribute));
| public boolean | addAll(javax.print.attribute.AttributeSet attributes)Adds all of the elements in the specified set to this attribute.
The outcome is the same as if the
{@link #add(Attribute) add(Attribute) }
operation had been applied to this attribute set successively with
each element from the specified set.
The behavior of the addAll(AttributeSet)
operation is unspecified if the specified set is modified while
the operation is in progress.
If the addAll(AttributeSet) operation throws an exception,
the effect on this attribute set's state is implementation dependent;
elements from the specified set before the point of the exception may
or may not have been added to this attribute set.
Attribute []attrs = attributes.toArray();
boolean result = false;
for (int i=0; i<attrs.length; i++) {
Attribute newValue =
AttributeSetUtilities.verifyAttributeValue(attrs[i],
myInterface);
Object oldValue = attrMap.put(newValue.getCategory(), newValue);
result = (! newValue.equals(oldValue)) || result;
}
return result;
| public void | clear()Removes all attributes from this attribute set.
attrMap.clear();
| public boolean | containsKey(java.lang.Class category)Returns true if this attribute set contains an
attribute for the specified category.
return
category != null &&
AttributeSetUtilities.
verifyAttributeCategory(category, Attribute.class) != null &&
attrMap.get(category) != null;
| public boolean | containsValue(javax.print.attribute.Attribute attribute)Returns true if this attribute set contains the given
attribute.
return
attribute != null &&
attribute instanceof Attribute &&
attribute.equals(attrMap.get(((Attribute)attribute).getCategory()));
| public boolean | equals(java.lang.Object object)Compares the specified object with this attribute set for equality.
Returns true if the given object is also an attribute set and
the two attribute sets contain the same attribute category-attribute
value mappings. This ensures that the
equals() method works properly across different
implementations of the AttributeSet interface.
if (object == null || !(object instanceof AttributeSet)) {
return false;
}
AttributeSet aset = (AttributeSet)object;
if (aset.size() != size()) {
return false;
}
Attribute[] attrs = toArray();
for (int i=0;i<attrs.length; i++) {
if (!aset.containsValue(attrs[i])) {
return false;
}
}
return true;
| public javax.print.attribute.Attribute | get(java.lang.Class category)Returns the attribute value which this attribute set contains in the
given attribute category. Returns null if this attribute set
does not contain any attribute value in the given attribute category.
return (Attribute)
attrMap.get(AttributeSetUtilities.
verifyAttributeCategory(category,
Attribute.class));
| public int | hashCode()Returns the hash code value for this attribute set.
The hash code of an attribute set is defined to be the sum
of the hash codes of each entry in the AttributeSet.
This ensures that t1.equals(t2) implies that
t1.hashCode()==t2.hashCode() for any two attribute sets
t1 and t2, as required by the general contract of
{@link java.lang.Object#hashCode() Object.hashCode() }.
int hcode = 0;
Attribute[] attrs = toArray();
for (int i=0;i<attrs.length; i++) {
hcode += attrs[i].hashCode();
}
return hcode;
| public boolean | isEmpty()Returns true if this attribute set contains no attributes.
return attrMap.isEmpty();
| private void | readObject(java.io.ObjectInputStream s)Reconstitute an instance from a stream that is, deserialize it).
s.defaultReadObject();
attrMap = new HashMap();
int count = s.readInt();
Attribute attr;
for (int i = 0; i < count; i++) {
attr = (Attribute)s.readObject();
add(attr);
}
| public boolean | remove(java.lang.Class category)Removes any attribute for this category from this attribute set if
present. If category is null, then
remove() does nothing and returns false.
return
category != null &&
AttributeSetUtilities.
verifyAttributeCategory(category, Attribute.class) != null &&
attrMap.remove(category) != null;
| public boolean | remove(javax.print.attribute.Attribute attribute)Removes the specified attribute from this attribute set if
present. If attribute is null, then
remove() does nothing and returns false.
return
attribute != null &&
attrMap.remove(attribute.getCategory()) != null;
| public int | size()Returns the number of attributes in this attribute set. If this
attribute set contains more than Integer.MAX_VALUE elements,
returns Integer.MAX_VALUE.
return attrMap.size();
| public javax.print.attribute.Attribute[] | toArray()
Attribute []attrs = new Attribute[size()];
attrMap.values().toArray(attrs);
return attrs;
| private void | writeObject(java.io.ObjectOutputStream s)Write the instance to a stream (ie serialize the object)
s.defaultWriteObject();
Attribute [] attrs = toArray();
s.writeInt(attrs.length);
for (int i = 0; i < attrs.length; i++) {
s.writeObject(attrs[i]);
}
|
|