0N/A/*
2362N/A * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.print.attribute;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.Serializable;
0N/Aimport java.util.HashMap;
0N/A
0N/A/**
0N/A * Class HashAttributeSet provides an <code>AttributeSet</code>
0N/A * implementation with characteristics of a hash map.
0N/A * <P>
0N/A *
0N/A * @author Alan Kaminsky
0N/A */
0N/Apublic class HashAttributeSet implements AttributeSet, Serializable {
0N/A
0N/A private static final long serialVersionUID = 5311560590283707917L;
0N/A
0N/A /**
0N/A * The interface of which all members of this attribute set must be an
0N/A * instance. It is assumed to be interface {@link Attribute Attribute}
0N/A * or a subinterface thereof.
0N/A * @serial
0N/A */
0N/A private Class myInterface;
0N/A
0N/A /*
0N/A * A HashMap used by the implementation.
0N/A * The serialised form doesn't include this instance variable.
0N/A */
0N/A private transient HashMap attrMap = new HashMap();
0N/A
0N/A /**
0N/A * Write the instance to a stream (ie serialize the object)
0N/A *
0N/A * @serialData
0N/A * The serialized form of an attribute set explicitly writes the
0N/A * number of attributes in the set, and each of the attributes.
0N/A * This does not guarantee equality of serialized forms since
0N/A * the order in which the attributes are written is not defined.
0N/A */
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
0N/A
0N/A s.defaultWriteObject();
0N/A Attribute [] attrs = toArray();
0N/A s.writeInt(attrs.length);
0N/A for (int i = 0; i < attrs.length; i++) {
0N/A s.writeObject(attrs[i]);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Reconstitute an instance from a stream that is, deserialize it).
0N/A */
0N/A private void readObject(ObjectInputStream s)
0N/A throws ClassNotFoundException, IOException {
0N/A
0N/A s.defaultReadObject();
0N/A attrMap = new HashMap();
0N/A int count = s.readInt();
0N/A Attribute attr;
0N/A for (int i = 0; i < count; i++) {
0N/A attr = (Attribute)s.readObject();
0N/A add(attr);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Construct a new, empty attribute set.
0N/A */
0N/A public HashAttributeSet() {
0N/A this(Attribute.class);
0N/A }
0N/A
0N/A /**
0N/A * Construct a new attribute set,
0N/A * initially populated with the given attribute.
0N/A *
0N/A * @param attribute Attribute value to add to the set.
0N/A *
0N/A * @exception NullPointerException
0N/A * (unchecked exception) Thrown if <CODE>attribute</CODE> is null.
0N/A */
0N/A public HashAttributeSet(Attribute attribute) {
0N/A this (attribute, Attribute.class);
0N/A }
0N/A
0N/A /**
0N/A * Construct a new attribute set,
0N/A * initially populated with the values from the
0N/A * given array. The new attribute set is populated by
0N/A * adding the elements of <CODE>attributes</CODE> array to the set in
0N/A * sequence, starting at index 0. Thus, later array elements may replace
0N/A * earlier array elements if the array contains duplicate attribute
0N/A * values or attribute categories.
0N/A *
0N/A * @param attributes Array of attribute values to add to the set.
0N/A * If null, an empty attribute set is constructed.
0N/A *
0N/A * @exception NullPointerException
0N/A * (unchecked exception) Thrown if any element of
0N/A * <CODE>attributes</CODE> is null.
0N/A */
0N/A public HashAttributeSet(Attribute[] attributes) {
0N/A this (attributes, Attribute.class);
0N/A }
0N/A
0N/A /**
0N/A * Construct a new attribute set,
0N/A * initially populated with the values from the given set.
0N/A *
0N/A * @param attributes Set of attributes from which to initialise this set.
0N/A * If null, an empty attribute set is constructed.
0N/A *
0N/A */
0N/A public HashAttributeSet(AttributeSet attributes) {
0N/A this (attributes, Attribute.class);
0N/A }
0N/A
0N/A /**
0N/A * Construct a new, empty attribute set, where the members of
0N/A * the attribute set are restricted to the given interface.
0N/A *
0N/A * @param interfaceName The interface of which all members of this
0N/A * attribute set must be an instance. It is assumed to
0N/A * be interface {@link Attribute Attribute} or a
0N/A * subinterface thereof.
0N/A * @exception NullPointerException if interfaceName is null.
0N/A */
0N/A protected HashAttributeSet(Class<?> interfaceName) {
0N/A if (interfaceName == null) {
0N/A throw new NullPointerException("null interface");
0N/A }
0N/A myInterface = interfaceName;
0N/A }
0N/A
0N/A /**
0N/A * Construct a new attribute set, initially populated with the given
0N/A * attribute, where the members of the attribute set are restricted to the
0N/A * given interface.
0N/A *
0N/A * @param attribute Attribute value to add to the set.
0N/A * @param interfaceName The interface of which all members of this
0N/A * attribute set must be an instance. It is assumed to
0N/A * be interface {@link Attribute Attribute} or a
0N/A * subinterface thereof.
0N/A *
0N/A * @exception NullPointerException
0N/A * (unchecked exception) Thrown if <CODE>attribute</CODE> is null.
0N/A * @exception NullPointerException if interfaceName is null.
0N/A * @exception ClassCastException
0N/A * (unchecked exception) Thrown if <CODE>attribute</CODE> is not an
0N/A * instance of <CODE>interfaceName</CODE>.
0N/A */
0N/A protected HashAttributeSet(Attribute attribute, Class<?> interfaceName) {
0N/A if (interfaceName == null) {
0N/A throw new NullPointerException("null interface");
0N/A }
0N/A myInterface = interfaceName;
0N/A add (attribute);
0N/A }
0N/A
0N/A /**
0N/A * Construct a new attribute set, where the members of the attribute
0N/A * set are restricted to the given interface.
0N/A * The new attribute set is populated
0N/A * by adding the elements of <CODE>attributes</CODE> array to the set in
0N/A * sequence, starting at index 0. Thus, later array elements may replace
0N/A * earlier array elements if the array contains duplicate attribute
0N/A * values or attribute categories.
0N/A *
0N/A * @param attributes Array of attribute values to add to the set. If
0N/A * null, an empty attribute set is constructed.
0N/A * @param interfaceName The interface of which all members of this
0N/A * attribute set must be an instance. It is assumed to
0N/A * be interface {@link Attribute Attribute} or a
0N/A * subinterface thereof.
0N/A *
0N/A * @exception NullPointerException
0N/A * (unchecked exception) Thrown if any element of
0N/A * <CODE>attributes</CODE> is null.
0N/A * @exception NullPointerException if interfaceName is null.
0N/A * @exception ClassCastException
0N/A * (unchecked exception) Thrown if any element of
0N/A * <CODE>attributes</CODE> is not an instance of
0N/A * <CODE>interfaceName</CODE>.
0N/A */
0N/A protected HashAttributeSet(Attribute[] attributes, Class<?> interfaceName) {
0N/A if (interfaceName == null) {
0N/A throw new NullPointerException("null interface");
0N/A }
0N/A myInterface = interfaceName;
0N/A int n = attributes == null ? 0 : attributes.length;
0N/A for (int i = 0; i < n; ++ i) {
0N/A add (attributes[i]);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Construct a new attribute set, initially populated with the
0N/A * values from the given set where the members of the attribute
0N/A * set are restricted to the given interface.
0N/A *
0N/A * @param attributes set of attribute values to initialise the set. If
0N/A * null, an empty attribute set is constructed.
0N/A * @param interfaceName The interface of which all members of this
0N/A * attribute set must be an instance. It is assumed to
0N/A * be interface {@link Attribute Attribute} or a
0N/A * subinterface thereof.
0N/A *
0N/A * @exception ClassCastException
0N/A * (unchecked exception) Thrown if any element of
0N/A * <CODE>attributes</CODE> is not an instance of
0N/A * <CODE>interfaceName</CODE>.
0N/A */
0N/A protected HashAttributeSet(AttributeSet attributes, Class<?> interfaceName) {
0N/A myInterface = interfaceName;
0N/A if (attributes != null) {
0N/A Attribute[] attribArray = attributes.toArray();
0N/A int n = attribArray == null ? 0 : attribArray.length;
0N/A for (int i = 0; i < n; ++ i) {
0N/A add (attribArray[i]);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the attribute value which this attribute set contains in the
0N/A * given attribute category. Returns <tt>null</tt> if this attribute set
0N/A * does not contain any attribute value in the given attribute category.
0N/A *
0N/A * @param category Attribute category whose associated attribute value
0N/A * is to be returned. It must be a
0N/A * {@link java.lang.Class Class}
0N/A * that implements interface {@link Attribute
0N/A * Attribute}.
0N/A *
0N/A * @return The attribute value in the given attribute category contained
0N/A * in this attribute set, or <tt>null</tt> if this attribute set
0N/A * does not contain any attribute value in the given attribute
0N/A * category.
0N/A *
0N/A * @throws NullPointerException
0N/A * (unchecked exception) Thrown if the <CODE>category</CODE> is null.
0N/A * @throws ClassCastException
0N/A * (unchecked exception) Thrown if the <CODE>category</CODE> is not a
0N/A * {@link java.lang.Class Class} that implements interface {@link
0N/A * Attribute Attribute}.
0N/A */
0N/A public Attribute get(Class<?> category) {
0N/A return (Attribute)
0N/A attrMap.get(AttributeSetUtilities.
0N/A verifyAttributeCategory(category,
0N/A Attribute.class));
0N/A }
0N/A
0N/A /**
0N/A * Adds the specified attribute to this attribute set if it is not
0N/A * already present, first removing any existing in the same
0N/A * attribute category as the specified attribute value.
0N/A *
0N/A * @param attribute Attribute value to be added to this attribute set.
0N/A *
0N/A * @return <tt>true</tt> if this attribute set changed as a result of the
0N/A * call, i.e., the given attribute value was not already a
0N/A * member of this attribute set.
0N/A *
0N/A * @throws NullPointerException
0N/A * (unchecked exception) Thrown if the <CODE>attribute</CODE> is null.
0N/A * @throws UnmodifiableSetException
0N/A * (unchecked exception) Thrown if this attribute set does not support
0N/A * the <CODE>add()</CODE> operation.
0N/A */
0N/A public boolean add(Attribute attribute) {
0N/A Object oldAttribute =
0N/A attrMap.put(attribute.getCategory(),
0N/A AttributeSetUtilities.
0N/A verifyAttributeValue(attribute, myInterface));
0N/A return (!attribute.equals(oldAttribute));
0N/A }
0N/A
0N/A /**
0N/A * Removes any attribute for this category from this attribute set if
0N/A * present. If <CODE>category</CODE> is null, then
0N/A * <CODE>remove()</CODE> does nothing and returns <tt>false</tt>.
0N/A *
0N/A * @param category Attribute category to be removed from this
0N/A * attribute set.
0N/A *
0N/A * @return <tt>true</tt> if this attribute set changed as a result of the
0N/A * call, i.e., the given attribute category had been a member of
0N/A * this attribute set.
0N/A *
0N/A * @throws UnmodifiableSetException
0N/A * (unchecked exception) Thrown if this attribute set does not
0N/A * support the <CODE>remove()</CODE> operation.
0N/A */
0N/A public boolean remove(Class<?> category) {
0N/A return
0N/A category != null &&
0N/A AttributeSetUtilities.
0N/A verifyAttributeCategory(category, Attribute.class) != null &&
0N/A attrMap.remove(category) != null;
0N/A }
0N/A
0N/A /**
0N/A * Removes the specified attribute from this attribute set if
0N/A * present. If <CODE>attribute</CODE> is null, then
0N/A * <CODE>remove()</CODE> does nothing and returns <tt>false</tt>.
0N/A *
0N/A * @param attribute Attribute value to be removed from this attribute set.
0N/A *
0N/A * @return <tt>true</tt> if this attribute set changed as a result of the
0N/A * call, i.e., the given attribute value had been a member of
0N/A * this attribute set.
0N/A *
0N/A * @throws UnmodifiableSetException
0N/A * (unchecked exception) Thrown if this attribute set does not
0N/A * support the <CODE>remove()</CODE> operation.
0N/A */
0N/A public boolean remove(Attribute attribute) {
0N/A return
0N/A attribute != null &&
0N/A attrMap.remove(attribute.getCategory()) != null;
0N/A }
0N/A
0N/A /**
0N/A * Returns <tt>true</tt> if this attribute set contains an
0N/A * attribute for the specified category.
0N/A *
0N/A * @param category whose presence in this attribute set is
0N/A * to be tested.
0N/A *
0N/A * @return <tt>true</tt> if this attribute set contains an attribute
0N/A * value for the specified category.
0N/A */
0N/A public boolean containsKey(Class<?> category) {
0N/A return
0N/A category != null &&
0N/A AttributeSetUtilities.
0N/A verifyAttributeCategory(category, Attribute.class) != null &&
0N/A attrMap.get(category) != null;
0N/A }
0N/A
0N/A /**
0N/A * Returns <tt>true</tt> if this attribute set contains the given
0N/A * attribute.
0N/A *
0N/A * @param attribute value whose presence in this attribute set is
0N/A * to be tested.
0N/A *
0N/A * @return <tt>true</tt> if this attribute set contains the given
0N/A * attribute value.
0N/A */
0N/A public boolean containsValue(Attribute attribute) {
0N/A return
0N/A attribute != null &&
0N/A attribute instanceof Attribute &&
0N/A attribute.equals(attrMap.get(((Attribute)attribute).getCategory()));
0N/A }
0N/A
0N/A /**
0N/A * Adds all of the elements in the specified set to this attribute.
0N/A * The outcome is the same as if the
0N/A * {@link #add(Attribute) <CODE>add(Attribute)</CODE>}
0N/A * operation had been applied to this attribute set successively with
0N/A * each element from the specified set.
0N/A * The behavior of the <CODE>addAll(AttributeSet)</CODE>
0N/A * operation is unspecified if the specified set is modified while
0N/A * the operation is in progress.
0N/A * <P>
0N/A * If the <CODE>addAll(AttributeSet)</CODE> operation throws an exception,
0N/A * the effect on this attribute set's state is implementation dependent;
0N/A * elements from the specified set before the point of the exception may
0N/A * or may not have been added to this attribute set.
0N/A *
0N/A * @param attributes whose elements are to be added to this attribute
0N/A * set.
0N/A *
0N/A * @return <tt>true</tt> if this attribute set changed as a result of the
0N/A * call.
0N/A *
0N/A * @throws UnmodifiableSetException
0N/A * (Unchecked exception) Thrown if this attribute set does not
0N/A * support the <tt>addAll(AttributeSet)</tt> method.
0N/A * @throws NullPointerException
0N/A * (Unchecked exception) Thrown if some element in the specified
0N/A * set is null, or the set is null.
0N/A *
0N/A * @see #add(Attribute)
0N/A */
0N/A public boolean addAll(AttributeSet attributes) {
0N/A
0N/A Attribute []attrs = attributes.toArray();
0N/A boolean result = false;
0N/A for (int i=0; i<attrs.length; i++) {
0N/A Attribute newValue =
0N/A AttributeSetUtilities.verifyAttributeValue(attrs[i],
0N/A myInterface);
0N/A Object oldValue = attrMap.put(newValue.getCategory(), newValue);
0N/A result = (! newValue.equals(oldValue)) || result;
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of attributes in this attribute set. If this
0N/A * attribute set contains more than <tt>Integer.MAX_VALUE</tt> elements,
0N/A * returns <tt>Integer.MAX_VALUE</tt>.
0N/A *
0N/A * @return The number of attributes in this attribute set.
0N/A */
0N/A public int size() {
0N/A return attrMap.size();
0N/A }
0N/A
0N/A /**
0N/A *
0N/A * @return the Attributes contained in this set as an array, zero length
0N/A * if the AttributeSet is empty.
0N/A */
0N/A public Attribute[] toArray() {
0N/A Attribute []attrs = new Attribute[size()];
0N/A attrMap.values().toArray(attrs);
0N/A return attrs;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Removes all attributes from this attribute set.
0N/A *
0N/A * @throws UnmodifiableSetException
0N/A * (unchecked exception) Thrown if this attribute set does not support
0N/A * the <CODE>clear()</CODE> operation.
0N/A */
0N/A public void clear() {
0N/A attrMap.clear();
0N/A }
0N/A
0N/A /**
0N/A * Returns true if this attribute set contains no attributes.
0N/A *
0N/A * @return true if this attribute set contains no attributes.
0N/A */
0N/A public boolean isEmpty() {
0N/A return attrMap.isEmpty();
0N/A }
0N/A
0N/A /**
0N/A * Compares the specified object with this attribute set for equality.
0N/A * Returns <tt>true</tt> if the given object is also an attribute set and
0N/A * the two attribute sets contain the same attribute category-attribute
0N/A * value mappings. This ensures that the
0N/A * <tt>equals()</tt> method works properly across different
0N/A * implementations of the AttributeSet interface.
0N/A *
0N/A * @param object to be compared for equality with this attribute set.
0N/A *
0N/A * @return <tt>true</tt> if the specified object is equal to this
0N/A * attribute set.
0N/A */
0N/A
0N/A public boolean equals(Object object) {
0N/A if (object == null || !(object instanceof AttributeSet)) {
0N/A return false;
0N/A }
0N/A
0N/A AttributeSet aset = (AttributeSet)object;
0N/A if (aset.size() != size()) {
0N/A return false;
0N/A }
0N/A
0N/A Attribute[] attrs = toArray();
0N/A for (int i=0;i<attrs.length; i++) {
0N/A if (!aset.containsValue(attrs[i])) {
0N/A return false;
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Returns the hash code value for this attribute set.
0N/A * The hash code of an attribute set is defined to be the sum
0N/A * of the hash codes of each entry in the AttributeSet.
0N/A * This ensures that <tt>t1.equals(t2)</tt> implies that
0N/A * <tt>t1.hashCode()==t2.hashCode()</tt> for any two attribute sets
0N/A * <tt>t1</tt> and <tt>t2</tt>, as required by the general contract of
0N/A * {@link java.lang.Object#hashCode() <CODE>Object.hashCode()</CODE>}.
0N/A *
0N/A * @return The hash code value for this attribute set.
0N/A */
0N/A public int hashCode() {
0N/A int hcode = 0;
0N/A Attribute[] attrs = toArray();
0N/A for (int i=0;i<attrs.length; i++) {
0N/A hcode += attrs[i].hashCode();
0N/A }
0N/A return hcode;
0N/A }
0N/A
0N/A}