Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/print/attribute/AttributeSetUtilities.java
38918 views
/*1* Copyright (c) 2000, 2004, 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*/242526package javax.print.attribute;2728import java.io.Serializable;2930/**31* Class AttributeSetUtilities provides static methods for manipulating32* AttributeSets.33* <ul>34* <li>Methods for creating unmodifiable and synchronized views of attribute35* sets.36* <li>operations useful for building37* implementations of interface {@link AttributeSet AttributeSet}38* </ul>39* <P>40* An <B>unmodifiable view</B> <I>U</I> of an AttributeSet <I>S</I> provides a41* client with "read-only" access to <I>S</I>. Query operations on <I>U</I>42* "read through" to <I>S</I>; thus, changes in <I>S</I> are reflected in43* <I>U</I>. However, any attempt to modify <I>U</I>,44* results in an UnmodifiableSetException.45* The unmodifiable view object <I>U</I> will be serializable if the46* attribute set object <I>S</I> is serializable.47* <P>48* A <B>synchronized view</B> <I>V</I> of an attribute set <I>S</I> provides a49* client with synchronized (multiple thread safe) access to <I>S</I>. Each50* operation of <I>V</I> is synchronized using <I>V</I> itself as the lock51* object and then merely invokes the corresponding operation of <I>S</I>. In52* order to guarantee mutually exclusive access, it is critical that all53* access to <I>S</I> is accomplished through <I>V</I>. The synchronized view54* object <I>V</I> will be serializable if the attribute set object <I>S</I>55* is serializable.56* <P>57* As mentioned in the package description of javax.print, a null reference58* parameter to methods is59* incorrect unless explicitly documented on the method as having a meaningful60* interpretation. Usage to the contrary is incorrect coding and may result in61* a run time exception either immediately62* or at some later time. IllegalArgumentException and NullPointerException63* are examples of typical and acceptable run time exceptions for such cases.64*65* @author Alan Kaminsky66*/67public final class AttributeSetUtilities {6869/* Suppress default constructor, ensuring non-instantiability.70*/71private AttributeSetUtilities() {72}7374/**75* @serial include76*/77private static class UnmodifiableAttributeSet78implements AttributeSet, Serializable {7980private AttributeSet attrset;8182/* Unmodifiable view of the underlying attribute set.83*/84public UnmodifiableAttributeSet(AttributeSet attributeSet) {8586attrset = attributeSet;87}8889public Attribute get(Class<?> key) {90return attrset.get(key);91}9293public boolean add(Attribute attribute) {94throw new UnmodifiableSetException();95}9697public synchronized boolean remove(Class<?> category) {98throw new UnmodifiableSetException();99}100101public boolean remove(Attribute attribute) {102throw new UnmodifiableSetException();103}104105public boolean containsKey(Class<?> category) {106return attrset.containsKey(category);107}108109public boolean containsValue(Attribute attribute) {110return attrset.containsValue(attribute);111}112113public boolean addAll(AttributeSet attributes) {114throw new UnmodifiableSetException();115}116117public int size() {118return attrset.size();119}120121public Attribute[] toArray() {122return attrset.toArray();123}124125public void clear() {126throw new UnmodifiableSetException();127}128129public boolean isEmpty() {130return attrset.isEmpty();131}132133public boolean equals(Object o) {134return attrset.equals (o);135}136137public int hashCode() {138return attrset.hashCode();139}140141}142143/**144* @serial include145*/146private static class UnmodifiableDocAttributeSet147extends UnmodifiableAttributeSet148implements DocAttributeSet, Serializable {149150public UnmodifiableDocAttributeSet(DocAttributeSet attributeSet) {151152super (attributeSet);153}154}155156/**157* @serial include158*/159private static class UnmodifiablePrintRequestAttributeSet160extends UnmodifiableAttributeSet161implements PrintRequestAttributeSet, Serializable162{163public UnmodifiablePrintRequestAttributeSet164(PrintRequestAttributeSet attributeSet) {165166super (attributeSet);167}168}169170/**171* @serial include172*/173private static class UnmodifiablePrintJobAttributeSet174extends UnmodifiableAttributeSet175implements PrintJobAttributeSet, Serializable176{177public UnmodifiablePrintJobAttributeSet178(PrintJobAttributeSet attributeSet) {179180super (attributeSet);181}182}183184/**185* @serial include186*/187private static class UnmodifiablePrintServiceAttributeSet188extends UnmodifiableAttributeSet189implements PrintServiceAttributeSet, Serializable190{191public UnmodifiablePrintServiceAttributeSet192(PrintServiceAttributeSet attributeSet) {193194super (attributeSet);195}196}197198/**199* Creates an unmodifiable view of the given attribute set.200*201* @param attributeSet Underlying attribute set.202*203* @return Unmodifiable view of <CODE>attributeSet</CODE>.204*205* @exception NullPointerException206* Thrown if <CODE>attributeSet</CODE> is null. Null is never a207*/208public static AttributeSet unmodifiableView(AttributeSet attributeSet) {209if (attributeSet == null) {210throw new NullPointerException();211}212213return new UnmodifiableAttributeSet(attributeSet);214}215216/**217* Creates an unmodifiable view of the given doc attribute set.218*219* @param attributeSet Underlying doc attribute set.220*221* @return Unmodifiable view of <CODE>attributeSet</CODE>.222*223* @exception NullPointerException224* Thrown if <CODE>attributeSet</CODE> is null.225*/226public static DocAttributeSet unmodifiableView227(DocAttributeSet attributeSet) {228if (attributeSet == null) {229throw new NullPointerException();230}231return new UnmodifiableDocAttributeSet(attributeSet);232}233234/**235* Creates an unmodifiable view of the given print request attribute set.236*237* @param attributeSet Underlying print request attribute set.238*239* @return Unmodifiable view of <CODE>attributeSet</CODE>.240*241* @exception NullPointerException242* Thrown if <CODE>attributeSet</CODE> is null.243*/244public static PrintRequestAttributeSet245unmodifiableView(PrintRequestAttributeSet attributeSet) {246if (attributeSet == null) {247throw new NullPointerException();248}249return new UnmodifiablePrintRequestAttributeSet(attributeSet);250}251252/**253* Creates an unmodifiable view of the given print job attribute set.254*255* @param attributeSet Underlying print job attribute set.256*257* @return Unmodifiable view of <CODE>attributeSet</CODE>.258*259* @exception NullPointerException260* Thrown if <CODE>attributeSet</CODE> is null.261*/262public static PrintJobAttributeSet263unmodifiableView(PrintJobAttributeSet attributeSet) {264if (attributeSet == null) {265throw new NullPointerException();266}267return new UnmodifiablePrintJobAttributeSet(attributeSet);268}269270/**271* Creates an unmodifiable view of the given print service attribute set.272*273* @param attributeSet Underlying print service attribute set.274*275* @return Unmodifiable view of <CODE>attributeSet</CODE>.276*277* @exception NullPointerException278* Thrown if <CODE>attributeSet</CODE> is null.279*/280public static PrintServiceAttributeSet281unmodifiableView(PrintServiceAttributeSet attributeSet) {282if (attributeSet == null) {283throw new NullPointerException();284}285return new UnmodifiablePrintServiceAttributeSet (attributeSet);286}287288/**289* @serial include290*/291private static class SynchronizedAttributeSet292implements AttributeSet, Serializable {293294private AttributeSet attrset;295296public SynchronizedAttributeSet(AttributeSet attributeSet) {297attrset = attributeSet;298}299300public synchronized Attribute get(Class<?> category) {301return attrset.get(category);302}303304public synchronized boolean add(Attribute attribute) {305return attrset.add(attribute);306}307308public synchronized boolean remove(Class<?> category) {309return attrset.remove(category);310}311312public synchronized boolean remove(Attribute attribute) {313return attrset.remove(attribute);314}315316public synchronized boolean containsKey(Class<?> category) {317return attrset.containsKey(category);318}319320public synchronized boolean containsValue(Attribute attribute) {321return attrset.containsValue(attribute);322}323324public synchronized boolean addAll(AttributeSet attributes) {325return attrset.addAll(attributes);326}327328public synchronized int size() {329return attrset.size();330}331332public synchronized Attribute[] toArray() {333return attrset.toArray();334}335336public synchronized void clear() {337attrset.clear();338}339340public synchronized boolean isEmpty() {341return attrset.isEmpty();342}343344public synchronized boolean equals(Object o) {345return attrset.equals (o);346}347348public synchronized int hashCode() {349return attrset.hashCode();350}351}352353/**354* @serial include355*/356private static class SynchronizedDocAttributeSet357extends SynchronizedAttributeSet358implements DocAttributeSet, Serializable {359360public SynchronizedDocAttributeSet(DocAttributeSet attributeSet) {361super(attributeSet);362}363}364365/**366* @serial include367*/368private static class SynchronizedPrintRequestAttributeSet369extends SynchronizedAttributeSet370implements PrintRequestAttributeSet, Serializable {371372public SynchronizedPrintRequestAttributeSet373(PrintRequestAttributeSet attributeSet) {374super(attributeSet);375}376}377378/**379* @serial include380*/381private static class SynchronizedPrintJobAttributeSet382extends SynchronizedAttributeSet383implements PrintJobAttributeSet, Serializable {384385public SynchronizedPrintJobAttributeSet386(PrintJobAttributeSet attributeSet) {387super(attributeSet);388}389}390391/**392* @serial include393*/394private static class SynchronizedPrintServiceAttributeSet395extends SynchronizedAttributeSet396implements PrintServiceAttributeSet, Serializable {397public SynchronizedPrintServiceAttributeSet398(PrintServiceAttributeSet attributeSet) {399super(attributeSet);400}401}402403/**404* Creates a synchronized view of the given attribute set.405*406* @param attributeSet Underlying attribute set.407*408* @return Synchronized view of <CODE>attributeSet</CODE>.409*410* @exception NullPointerException411* Thrown if <CODE>attributeSet</CODE> is null.412*/413public static AttributeSet synchronizedView414(AttributeSet attributeSet) {415if (attributeSet == null) {416throw new NullPointerException();417}418return new SynchronizedAttributeSet(attributeSet);419}420421/**422* Creates a synchronized view of the given doc attribute set.423*424* @param attributeSet Underlying doc attribute set.425*426* @return Synchronized view of <CODE>attributeSet</CODE>.427*428* @exception NullPointerException429* Thrown if <CODE>attributeSet</CODE> is null.430*/431public static DocAttributeSet432synchronizedView(DocAttributeSet attributeSet) {433if (attributeSet == null) {434throw new NullPointerException();435}436return new SynchronizedDocAttributeSet(attributeSet);437}438439/**440* Creates a synchronized view of the given print request attribute set.441*442* @param attributeSet Underlying print request attribute set.443*444* @return Synchronized view of <CODE>attributeSet</CODE>.445*446* @exception NullPointerException447* Thrown if <CODE>attributeSet</CODE> is null.448*/449public static PrintRequestAttributeSet450synchronizedView(PrintRequestAttributeSet attributeSet) {451if (attributeSet == null) {452throw new NullPointerException();453}454return new SynchronizedPrintRequestAttributeSet(attributeSet);455}456457/**458* Creates a synchronized view of the given print job attribute set.459*460* @param attributeSet Underlying print job attribute set.461*462* @return Synchronized view of <CODE>attributeSet</CODE>.463*464* @exception NullPointerException465* Thrown if <CODE>attributeSet</CODE> is null.466*/467public static PrintJobAttributeSet468synchronizedView(PrintJobAttributeSet attributeSet) {469if (attributeSet == null) {470throw new NullPointerException();471}472return new SynchronizedPrintJobAttributeSet(attributeSet);473}474475/**476* Creates a synchronized view of the given print service attribute set.477*478* @param attributeSet Underlying print service attribute set.479*480* @return Synchronized view of <CODE>attributeSet</CODE>.481*/482public static PrintServiceAttributeSet483synchronizedView(PrintServiceAttributeSet attributeSet) {484if (attributeSet == null) {485throw new NullPointerException();486}487return new SynchronizedPrintServiceAttributeSet(attributeSet);488}489490491/**492* Verify that the given object is a {@link java.lang.Class Class} that493* implements the given interface, which is assumed to be interface {@link494* Attribute Attribute} or a subinterface thereof.495*496* @param object Object to test.497* @param interfaceName Interface the object must implement.498*499* @return If <CODE>object</CODE> is a {@link java.lang.Class Class}500* that implements <CODE>interfaceName</CODE>,501* <CODE>object</CODE> is returned downcast to type {@link502* java.lang.Class Class}; otherwise an exception is thrown.503*504* @exception NullPointerException505* (unchecked exception) Thrown if <CODE>object</CODE> is null.506* @exception ClassCastException507* (unchecked exception) Thrown if <CODE>object</CODE> is not a508* {@link java.lang.Class Class} that implements509* <CODE>interfaceName</CODE>.510*/511public static Class<?>512verifyAttributeCategory(Object object, Class<?> interfaceName) {513514Class result = (Class) object;515if (interfaceName.isAssignableFrom (result)) {516return result;517}518else {519throw new ClassCastException();520}521}522523/**524* Verify that the given object is an instance of the given interface, which525* is assumed to be interface {@link Attribute Attribute} or a subinterface526* thereof.527*528* @param object Object to test.529* @param interfaceName Interface of which the object must be an instance.530*531* @return If <CODE>object</CODE> is an instance of532* <CODE>interfaceName</CODE>, <CODE>object</CODE> is returned533* downcast to type {@link Attribute Attribute}; otherwise an534* exception is thrown.535*536* @exception NullPointerException537* (unchecked exception) Thrown if <CODE>object</CODE> is null.538* @exception ClassCastException539* (unchecked exception) Thrown if <CODE>object</CODE> is not an540* instance of <CODE>interfaceName</CODE>.541*/542public static Attribute543verifyAttributeValue(Object object, Class<?> interfaceName) {544545if (object == null) {546throw new NullPointerException();547}548else if (interfaceName.isInstance (object)) {549return (Attribute) object;550} else {551throw new ClassCastException();552}553}554555/**556* Verify that the given attribute category object is equal to the557* category of the given attribute value object. If so, this method558* returns doing nothing. If not, this method throws an exception.559*560* @param category Attribute category to test.561* @param attribute Attribute value to test.562*563* @exception NullPointerException564* (unchecked exception) Thrown if the <CODE>category</CODE> is565* null or if the <CODE>attribute</CODE> is null.566* @exception IllegalArgumentException567* (unchecked exception) Thrown if the <CODE>category</CODE> is not568* equal to the category of the <CODE>attribute</CODE>.569*/570public static void571verifyCategoryForValue(Class<?> category, Attribute attribute) {572573if (!category.equals (attribute.getCategory())) {574throw new IllegalArgumentException();575}576}577}578579580