Path: blob/master/src/java.desktop/share/classes/javax/print/attribute/AttributeSetUtilities.java
41159 views
/*1* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.print.attribute;2627import java.io.Serial;28import java.io.Serializable;2930/**31* Class {@code AttributeSetUtilities} provides static methods for manipulating32* {@code AttributeSets}.33* <ul>34* <li>Methods for creating unmodifiable and synchronized views of attribute35* sets.36* <li>operations useful for building implementations of interface37* {@link AttributeSet AttributeSet}38* </ul>39* An <b>unmodifiable view</b> <i>U</i> of an {@code AttributeSet} <i>S</i>40* provides a client with "read-only" access to <i>S</i>. Query operations on41* <i>U</i> "read through" to <i>S</i>; thus, changes in <i>S</i> are reflected42* in <i>U</i>. However, any attempt to modify <i>U</i>, results in an43* {@code UnmodifiableSetException}. The unmodifiable view object <i>U</i> will44* be serializable if the attribute set object <i>S</i> is serializable.45* <p>46* A <b>synchronized view</b> <i>V</i> of an attribute set <i>S</i> provides a47* client with synchronized (multiple thread safe) access to <i>S</i>. Each48* operation of <i>V</i> is synchronized using <i>V</i> itself as the lock49* object and then merely invokes the corresponding operation of <i>S</i>. In50* order to guarantee mutually exclusive access, it is critical that all access51* to <i>S</i> is accomplished through <i>V</i>. The synchronized view object52* <i>V</i> will be serializable if the attribute set object <i>S</i> is53* serializable.54* <p>55* As mentioned in the package description of {@code javax.print}, a56* {@code null} reference parameter to methods is incorrect unless explicitly57* documented on the method as having a meaningful interpretation. Usage to the58* contrary is incorrect coding and may result in a run time exception either59* immediately or at some later time. {@code IllegalArgumentException} and60* {@code NullPointerException} are examples of typical and acceptable run time61* exceptions for such cases.62*63* @author Alan Kaminsky64*/65public final class AttributeSetUtilities {6667/**68* Suppress default constructor, ensuring non-instantiability.69*/70private AttributeSetUtilities() {71}7273/**74* Unmodifiable view of {@code AttributeSet}.75*76* @serial include77*/78private static class UnmodifiableAttributeSet79implements AttributeSet, Serializable {8081/**82* Use serialVersionUID from JDK 1.4 for interoperability.83*/84@Serial85private static final long serialVersionUID = -6131802583863447813L;8687/**88* The attribute set.89*/90@SuppressWarnings("serial") // Not statically typed as Serializable91private AttributeSet attrset;9293/**94* Constructs unmodifiable view of the underlying attribute set.95*96* @param attributeSet the attribute set97*/98public UnmodifiableAttributeSet(AttributeSet attributeSet) {99100attrset = attributeSet;101}102103public Attribute get(Class<?> key) {104return attrset.get(key);105}106107public boolean add(Attribute attribute) {108throw new UnmodifiableSetException();109}110111public synchronized boolean remove(Class<?> category) {112throw new UnmodifiableSetException();113}114115public boolean remove(Attribute attribute) {116throw new UnmodifiableSetException();117}118119public boolean containsKey(Class<?> category) {120return attrset.containsKey(category);121}122123public boolean containsValue(Attribute attribute) {124return attrset.containsValue(attribute);125}126127public boolean addAll(AttributeSet attributes) {128throw new UnmodifiableSetException();129}130131public int size() {132return attrset.size();133}134135public Attribute[] toArray() {136return attrset.toArray();137}138139public void clear() {140throw new UnmodifiableSetException();141}142143public boolean isEmpty() {144return attrset.isEmpty();145}146147public boolean equals(Object o) {148return attrset.equals (o);149}150151public int hashCode() {152return attrset.hashCode();153}154}155156/**157* Unmodifiable view of {@code DocAttributeSet}.158*159* @serial include160*/161private static class UnmodifiableDocAttributeSet162extends UnmodifiableAttributeSet163implements DocAttributeSet, Serializable {164165/**166* Use serialVersionUID from JDK 1.4 for interoperability.167*/168@Serial169private static final long serialVersionUID = -6349408326066898956L;170171/**172* Constructs a new unmodifiable doc attribute set.173*174* @param attributeSet the doc attribute set175*/176public UnmodifiableDocAttributeSet(DocAttributeSet attributeSet) {177178super (attributeSet);179}180}181182/**183* Unmodifiable view of {@code PrintRequestAttributeSet}.184*185* @serial include186*/187private static class UnmodifiablePrintRequestAttributeSet188extends UnmodifiableAttributeSet189implements PrintRequestAttributeSet, Serializable190{191192/**193* Use serialVersionUID from JDK 1.4 for interoperability.194*/195@Serial196private static final long serialVersionUID = 7799373532614825073L;197198/**199* Constructs a new unmodifiable print request attribute set.200*201* @param attributeSet the print request attribute set202*/203public UnmodifiablePrintRequestAttributeSet204(PrintRequestAttributeSet attributeSet) {205206super (attributeSet);207}208}209210/**211* Unmodifiable view of {@code PrintJobAttributeSet}.212*213* @serial include214*/215private static class UnmodifiablePrintJobAttributeSet216extends UnmodifiableAttributeSet217implements PrintJobAttributeSet, Serializable218{219/**220* Use serialVersionUID from JDK 1.4 for interoperability.221*/222@Serial223private static final long serialVersionUID = -8002245296274522112L;224225/**226* Constructs a new unmodifiable print job attribute set.227*228* @param attributeSet the print job attribute set229*/230public UnmodifiablePrintJobAttributeSet231(PrintJobAttributeSet attributeSet) {232233super (attributeSet);234}235}236237/**238* Unmodifiable view of {@code PrintServiceAttributeSet}.239*240* @serial include241*/242private static class UnmodifiablePrintServiceAttributeSet243extends UnmodifiableAttributeSet244implements PrintServiceAttributeSet, Serializable245{246/**247* Use serialVersionUID from JDK 1.4 for interoperability.248*/249@Serial250private static final long serialVersionUID = -7112165137107826819L;251252/**253* Constructs a new unmodifiable print service attribute set.254*255* @param attributeSet the print service attribute set256*/257public UnmodifiablePrintServiceAttributeSet258(PrintServiceAttributeSet attributeSet) {259260super (attributeSet);261}262}263264/**265* Creates an unmodifiable view of the given attribute set.266*267* @param attributeSet underlying attribute set268* @return unmodifiable view of {@code attributeSet}269* @throws NullPointerException if {@code attributeSet} is {@code null}270*/271public static AttributeSet unmodifiableView(AttributeSet attributeSet) {272if (attributeSet == null) {273throw new NullPointerException();274}275276return new UnmodifiableAttributeSet(attributeSet);277}278279/**280* Creates an unmodifiable view of the given doc attribute set.281*282* @param attributeSet underlying doc attribute set283* @return unmodifiable view of {@code attributeSet}284* @throws NullPointerException if {@code attributeSet} is {@code null}285*/286public static DocAttributeSet unmodifiableView287(DocAttributeSet attributeSet) {288if (attributeSet == null) {289throw new NullPointerException();290}291return new UnmodifiableDocAttributeSet(attributeSet);292}293294/**295* Creates an unmodifiable view of the given print request attribute set.296*297* @param attributeSet underlying print request attribute set298* @return unmodifiable view of {@code attributeSet}299* @throws NullPointerException if {@code attributeSet} is {@code null}300*/301public static PrintRequestAttributeSet302unmodifiableView(PrintRequestAttributeSet attributeSet) {303if (attributeSet == null) {304throw new NullPointerException();305}306return new UnmodifiablePrintRequestAttributeSet(attributeSet);307}308309/**310* Creates an unmodifiable view of the given print job attribute set.311*312* @param attributeSet underlying print job attribute set313* @return unmodifiable view of {@code attributeSet}314* @throws NullPointerException if {@code attributeSet} is {@code null}315*/316public static PrintJobAttributeSet317unmodifiableView(PrintJobAttributeSet attributeSet) {318if (attributeSet == null) {319throw new NullPointerException();320}321return new UnmodifiablePrintJobAttributeSet(attributeSet);322}323324/**325* Creates an unmodifiable view of the given print service attribute set.326*327* @param attributeSet underlying print service attribute set328* @return unmodifiable view of {@code attributeSet}329* @throws NullPointerException if {@code attributeSet} is {@code null}330*/331public static PrintServiceAttributeSet332unmodifiableView(PrintServiceAttributeSet attributeSet) {333if (attributeSet == null) {334throw new NullPointerException();335}336return new UnmodifiablePrintServiceAttributeSet (attributeSet);337}338339/**340* Synchronized view of {@code AttributeSet}.341*342* @serial include343*/344private static class SynchronizedAttributeSet345implements AttributeSet, Serializable {346347/**348* Use serialVersionUID from JDK 1.4 for interoperability.349*/350@Serial351private static final long serialVersionUID = 8365731020128564925L;352353/**354* The attribute set.355*/356@SuppressWarnings("serial") // Not statically typed as Serializable357private AttributeSet attrset;358359/**360* Constructs a new synchronized attribute set.361*362* @param attributeSet the attribute set363*/364public SynchronizedAttributeSet(AttributeSet attributeSet) {365attrset = attributeSet;366}367368public synchronized Attribute get(Class<?> category) {369return attrset.get(category);370}371372public synchronized boolean add(Attribute attribute) {373return attrset.add(attribute);374}375376public synchronized boolean remove(Class<?> category) {377return attrset.remove(category);378}379380public synchronized boolean remove(Attribute attribute) {381return attrset.remove(attribute);382}383384public synchronized boolean containsKey(Class<?> category) {385return attrset.containsKey(category);386}387388public synchronized boolean containsValue(Attribute attribute) {389return attrset.containsValue(attribute);390}391392public synchronized boolean addAll(AttributeSet attributes) {393return attrset.addAll(attributes);394}395396public synchronized int size() {397return attrset.size();398}399400public synchronized Attribute[] toArray() {401return attrset.toArray();402}403404public synchronized void clear() {405attrset.clear();406}407408public synchronized boolean isEmpty() {409return attrset.isEmpty();410}411412public synchronized boolean equals(Object o) {413return attrset.equals (o);414}415416public synchronized int hashCode() {417return attrset.hashCode();418}419}420421/**422* Synchronized view of {@code DocAttributeSet}.423*424* @serial include425*/426private static class SynchronizedDocAttributeSet427extends SynchronizedAttributeSet428implements DocAttributeSet, Serializable {429430/**431* Use serialVersionUID from JDK 1.4 for interoperability.432*/433@Serial434private static final long serialVersionUID = 6455869095246629354L;435436/**437* Constructs a new synchronized doc attribute set.438*439* @param attributeSet the doc attribute set440*/441public SynchronizedDocAttributeSet(DocAttributeSet attributeSet) {442super(attributeSet);443}444}445446/**447* Synchronized view of {@code PrintRequestAttributeSet}.448*449* @serial include450*/451private static class SynchronizedPrintRequestAttributeSet452extends SynchronizedAttributeSet453implements PrintRequestAttributeSet, Serializable {454455/**456* Use serialVersionUID from JDK 1.4 for interoperability.457*/458@Serial459private static final long serialVersionUID = 5671237023971169027L;460461/**462* Constructs a new synchronized print request attribute set.463*464* @param attributeSet the print request attribute set465*/466public SynchronizedPrintRequestAttributeSet467(PrintRequestAttributeSet attributeSet) {468super(attributeSet);469}470}471472/**473* Synchronized view of {@code PrintJobAttributeSet}.474*475* @serial include476*/477private static class SynchronizedPrintJobAttributeSet478extends SynchronizedAttributeSet479implements PrintJobAttributeSet, Serializable {480481/**482* Use serialVersionUID from JDK 1.4 for interoperability.483*/484@Serial485private static final long serialVersionUID = 2117188707856965749L;486487/**488* Constructs a new synchronized print job attribute set.489*490* @param attributeSet the print job attribute set491*/492public SynchronizedPrintJobAttributeSet493(PrintJobAttributeSet attributeSet) {494super(attributeSet);495}496}497498/**499* Synchronized view of {@code PrintServiceAttributeSet}.500*501* @serial include502*/503private static class SynchronizedPrintServiceAttributeSet504extends SynchronizedAttributeSet505implements PrintServiceAttributeSet, Serializable {506507/**508* Use serialVersionUID from JDK 1.4 for interoperability.509*/510@Serial511private static final long serialVersionUID = -2830705374001675073L;512513/**514* Constructs a new synchronized print service attribute set.515*516* @param attributeSet the print service attribute set517*/518public SynchronizedPrintServiceAttributeSet519(PrintServiceAttributeSet attributeSet) {520super(attributeSet);521}522}523524/**525* Creates a synchronized view of the given attribute set.526*527* @param attributeSet underlying attribute set528* @return synchronized view of {@code attributeSet}529* @throws NullPointerException if {@code attributeSet} is {@code null}530*/531public static AttributeSet synchronizedView532(AttributeSet attributeSet) {533if (attributeSet == null) {534throw new NullPointerException();535}536return new SynchronizedAttributeSet(attributeSet);537}538539/**540* Creates a synchronized view of the given doc attribute set.541*542* @param attributeSet underlying doc attribute set543* @return synchronized view of {@code attributeSet}544* @throws NullPointerException if {@code attributeSet} is {@code null}545*/546public static DocAttributeSet547synchronizedView(DocAttributeSet attributeSet) {548if (attributeSet == null) {549throw new NullPointerException();550}551return new SynchronizedDocAttributeSet(attributeSet);552}553554/**555* Creates a synchronized view of the given print request attribute set.556*557* @param attributeSet underlying print request attribute set558* @return synchronized view of {@code attributeSet}559* @throws NullPointerException if {@code attributeSet} is {@code null}560*/561public static PrintRequestAttributeSet562synchronizedView(PrintRequestAttributeSet attributeSet) {563if (attributeSet == null) {564throw new NullPointerException();565}566return new SynchronizedPrintRequestAttributeSet(attributeSet);567}568569/**570* Creates a synchronized view of the given print job attribute set.571*572* @param attributeSet underlying print job attribute set573* @return synchronized view of {@code attributeSet}574* @throws NullPointerException if {@code attributeSet} is {@code null}575*/576public static PrintJobAttributeSet577synchronizedView(PrintJobAttributeSet attributeSet) {578if (attributeSet == null) {579throw new NullPointerException();580}581return new SynchronizedPrintJobAttributeSet(attributeSet);582}583584/**585* Creates a synchronized view of the given print service attribute set.586*587* @param attributeSet underlying print service attribute set588* @return synchronized view of {@code attributeSet}589* @throws NullPointerException if {@code attributeSet} is {@code null}590*/591public static PrintServiceAttributeSet592synchronizedView(PrintServiceAttributeSet attributeSet) {593if (attributeSet == null) {594throw new NullPointerException();595}596return new SynchronizedPrintServiceAttributeSet(attributeSet);597}598599/**600* Verify that the given object is a {@link Class Class} that implements the601* given interface, which is assumed to be interface602* {@link Attribute Attribute} or a subinterface thereof.603*604* @param object {@code Object} to test605* @param interfaceName interface the object must implement606* @return if {@code object} is a {@link Class Class} that implements607* {@code interfaceName}, {@code object} is returned downcast to608* type {@link Class Class}; otherwise an exception is thrown609* @throws NullPointerException if {@code object} is {@code null}610* @throws ClassCastException if {@code object} is not a611* {@link Class Class} that implements {@code interfaceName}612*/613public static Class<?>614verifyAttributeCategory(Object object, Class<?> interfaceName) {615616Class<?> result = (Class<?>) object;617if (interfaceName.isAssignableFrom (result)) {618return result;619}620else {621throw new ClassCastException();622}623}624625/**626* Verify that the given object is an instance of the given interface, which627* is assumed to be interface {@link Attribute Attribute} or a subinterface628* thereof.629*630* @param object {@code Object} to test631* @param interfaceName interface of which the object must be an instance632* @return if {@code object} is an instance of {@code interfaceName},633* {@code object} is returned downcast to type634* {@link Attribute Attribute}; otherwise an exception is thrown635* @throws NullPointerException if {@code object} is {@code null}636* @throws ClassCastException if {@code object} is not an instance of637* {@code interfaceName}638*/639public static Attribute640verifyAttributeValue(Object object, Class<?> interfaceName) {641642if (object == null) {643throw new NullPointerException();644}645else if (interfaceName.isInstance (object)) {646return (Attribute) object;647} else {648throw new ClassCastException();649}650}651652/**653* Verify that the given attribute category object is equal to the category654* of the given attribute value object. If so, this method returns doing655* nothing. If not, this method throws an exception.656*657* @param category attribute category to test658* @param attribute attribute value to test659* @throws NullPointerException if the {@code category} or {@code attribute}660* are {@code null}661* @throws IllegalArgumentException if the {@code category} is not equal to662* the category of the {@code attribute}663*/664public static void665verifyCategoryForValue(Class<?> category, Attribute attribute) {666667if (!category.equals (attribute.getCategory())) {668throw new IllegalArgumentException();669}670}671}672673674