src/share/classes/javax/print/attribute/HashAttributeSet.java

Print this page




  31 import java.io.Serializable;
  32 import java.util.HashMap;
  33 
  34 /**
  35  * Class HashAttributeSet provides an <code>AttributeSet</code>
  36  * implementation with characteristics of a hash map.
  37  * <P>
  38  *
  39  * @author  Alan Kaminsky
  40  */
  41 public class HashAttributeSet implements AttributeSet, Serializable {
  42 
  43     private static final long serialVersionUID = 5311560590283707917L;
  44 
  45     /**
  46      * The interface of which all members of this attribute set must be an
  47      * instance. It is assumed to be interface {@link Attribute Attribute}
  48      * or a subinterface thereof.
  49      * @serial
  50      */
  51     private Class myInterface;
  52 
  53     /*
  54      * A HashMap used by the implementation.
  55      * The serialised form doesn't include this instance variable.
  56      */
  57     private transient HashMap attrMap = new HashMap();
  58 
  59     /**
  60      * Write the instance to a stream (ie serialize the object)
  61      *
  62      * @serialData
  63      * The serialized form of an attribute set explicitly writes the
  64      * number of attributes in the set, and each of the attributes.
  65      * This does not guarantee equality of serialized forms since
  66      * the order in which the attributes are written is not defined.
  67      */
  68     private void writeObject(ObjectOutputStream s) throws IOException {
  69 
  70         s.defaultWriteObject();
  71         Attribute [] attrs = toArray();
  72         s.writeInt(attrs.length);
  73         for (int i = 0; i < attrs.length; i++) {
  74             s.writeObject(attrs[i]);
  75         }
  76     }
  77 
  78     /**
  79      * Reconstitute an instance from a stream that is, deserialize it).
  80      */
  81     private void readObject(ObjectInputStream s)
  82         throws ClassNotFoundException, IOException {
  83 
  84         s.defaultReadObject();
  85         attrMap = new HashMap();
  86         int count = s.readInt();
  87         Attribute attr;
  88         for (int i = 0; i < count; i++) {
  89             attr = (Attribute)s.readObject();
  90             add(attr);
  91         }
  92     }
  93 
  94     /**
  95      * Construct a new, empty attribute set.
  96      */
  97     public HashAttributeSet() {
  98         this(Attribute.class);
  99     }
 100 
 101     /**
 102      * Construct a new attribute set,
 103      * initially populated with the given attribute.
 104      *
 105      * @param  attribute  Attribute value to add to the set.


 257      *
 258      * @param  category  Attribute category whose associated attribute value
 259      *                   is to be returned. It must be a
 260      *                   {@link java.lang.Class Class}
 261      *                   that implements interface {@link Attribute
 262      *                   Attribute}.
 263      *
 264      * @return  The attribute value in the given attribute category contained
 265      *          in this attribute set, or <tt>null</tt> if this attribute set
 266      *          does not contain any attribute value in the given attribute
 267      *          category.
 268      *
 269      * @throws  NullPointerException
 270      *     (unchecked exception) Thrown if the <CODE>category</CODE> is null.
 271      * @throws  ClassCastException
 272      *     (unchecked exception) Thrown if the <CODE>category</CODE> is not a
 273      *     {@link java.lang.Class Class} that implements interface {@link
 274      *     Attribute Attribute}.
 275      */
 276     public Attribute get(Class<?> category) {
 277         return (Attribute)
 278             attrMap.get(AttributeSetUtilities.
 279                         verifyAttributeCategory(category,
 280                                                 Attribute.class));
 281     }
 282 
 283     /**
 284      * Adds the specified attribute to this attribute set if it is not
 285      * already present, first removing any existing in the same
 286      * attribute category as the specified attribute value.
 287      *
 288      * @param  attribute  Attribute value to be added to this attribute set.
 289      *
 290      * @return  <tt>true</tt> if this attribute set changed as a result of the
 291      *          call, i.e., the given attribute value was not already a
 292      *          member of this attribute set.
 293      *
 294      * @throws  NullPointerException
 295      *    (unchecked exception) Thrown if the <CODE>attribute</CODE> is null.
 296      * @throws  UnmodifiableSetException
 297      *    (unchecked exception) Thrown if this attribute set does not support
 298      *     the <CODE>add()</CODE> operation.




  31 import java.io.Serializable;
  32 import java.util.HashMap;
  33 
  34 /**
  35  * Class HashAttributeSet provides an <code>AttributeSet</code>
  36  * implementation with characteristics of a hash map.
  37  * <P>
  38  *
  39  * @author  Alan Kaminsky
  40  */
  41 public class HashAttributeSet implements AttributeSet, Serializable {
  42 
  43     private static final long serialVersionUID = 5311560590283707917L;
  44 
  45     /**
  46      * The interface of which all members of this attribute set must be an
  47      * instance. It is assumed to be interface {@link Attribute Attribute}
  48      * or a subinterface thereof.
  49      * @serial
  50      */
  51     private Class<?> myInterface;
  52 
  53     /*
  54      * A HashMap used by the implementation.
  55      * The serialised form doesn't include this instance variable.
  56      */
  57     private transient HashMap<Class<?>, Attribute> attrMap = new HashMap<>();
  58 
  59     /**
  60      * Write the instance to a stream (ie serialize the object)
  61      *
  62      * @serialData
  63      * The serialized form of an attribute set explicitly writes the
  64      * number of attributes in the set, and each of the attributes.
  65      * This does not guarantee equality of serialized forms since
  66      * the order in which the attributes are written is not defined.
  67      */
  68     private void writeObject(ObjectOutputStream s) throws IOException {
  69 
  70         s.defaultWriteObject();
  71         Attribute [] attrs = toArray();
  72         s.writeInt(attrs.length);
  73         for (int i = 0; i < attrs.length; i++) {
  74             s.writeObject(attrs[i]);
  75         }
  76     }
  77 
  78     /**
  79      * Reconstitute an instance from a stream that is, deserialize it).
  80      */
  81     private void readObject(ObjectInputStream s)
  82         throws ClassNotFoundException, IOException {
  83 
  84         s.defaultReadObject();
  85         attrMap = new HashMap<>();
  86         int count = s.readInt();
  87         Attribute attr;
  88         for (int i = 0; i < count; i++) {
  89             attr = (Attribute)s.readObject();
  90             add(attr);
  91         }
  92     }
  93 
  94     /**
  95      * Construct a new, empty attribute set.
  96      */
  97     public HashAttributeSet() {
  98         this(Attribute.class);
  99     }
 100 
 101     /**
 102      * Construct a new attribute set,
 103      * initially populated with the given attribute.
 104      *
 105      * @param  attribute  Attribute value to add to the set.


 257      *
 258      * @param  category  Attribute category whose associated attribute value
 259      *                   is to be returned. It must be a
 260      *                   {@link java.lang.Class Class}
 261      *                   that implements interface {@link Attribute
 262      *                   Attribute}.
 263      *
 264      * @return  The attribute value in the given attribute category contained
 265      *          in this attribute set, or <tt>null</tt> if this attribute set
 266      *          does not contain any attribute value in the given attribute
 267      *          category.
 268      *
 269      * @throws  NullPointerException
 270      *     (unchecked exception) Thrown if the <CODE>category</CODE> is null.
 271      * @throws  ClassCastException
 272      *     (unchecked exception) Thrown if the <CODE>category</CODE> is not a
 273      *     {@link java.lang.Class Class} that implements interface {@link
 274      *     Attribute Attribute}.
 275      */
 276     public Attribute get(Class<?> category) {
 277         return attrMap.get(AttributeSetUtilities.

 278                            verifyAttributeCategory(category,
 279                                                    Attribute.class));
 280     }
 281 
 282     /**
 283      * Adds the specified attribute to this attribute set if it is not
 284      * already present, first removing any existing in the same
 285      * attribute category as the specified attribute value.
 286      *
 287      * @param  attribute  Attribute value to be added to this attribute set.
 288      *
 289      * @return  <tt>true</tt> if this attribute set changed as a result of the
 290      *          call, i.e., the given attribute value was not already a
 291      *          member of this attribute set.
 292      *
 293      * @throws  NullPointerException
 294      *    (unchecked exception) Thrown if the <CODE>attribute</CODE> is null.
 295      * @throws  UnmodifiableSetException
 296      *    (unchecked exception) Thrown if this attribute set does not support
 297      *     the <CODE>add()</CODE> operation.