001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package javax.print.attribute;
018:
019: import java.io.Serializable;
020:
021: public final class AttributeSetUtilities {
022:
023: private static class SynchronizedAttributeSet implements
024: AttributeSet, Serializable {
025:
026: static final long serialVersionUID = 8365731020128564925L;
027:
028: private AttributeSet aset;
029:
030: public SynchronizedAttributeSet(AttributeSet attributeSet) {
031: aset = attributeSet;
032: }
033:
034: public synchronized boolean add(Attribute attribute) {
035: return aset.add(attribute);
036: }
037:
038: public synchronized boolean addAll(AttributeSet attributeSet) {
039: return aset.addAll(attributeSet);
040: }
041:
042: public synchronized void clear() {
043: aset.clear();
044: }
045:
046: public synchronized boolean containsKey(Class attributeCategory) {
047: return aset.containsKey(attributeCategory);
048: }
049:
050: public synchronized boolean containsValue(Attribute attribute) {
051: return aset.containsValue(attribute);
052: }
053:
054: public synchronized boolean equals(Object object) {
055: return aset.equals(object);
056: }
057:
058: public synchronized Attribute get(Class attributeCategory) {
059: return aset.get(attributeCategory);
060: }
061:
062: public synchronized int hashCode() {
063: return aset.hashCode();
064: }
065:
066: public synchronized boolean isEmpty() {
067: return aset.isEmpty();
068: }
069:
070: public synchronized boolean remove(Attribute attribute) {
071: return aset.remove(attribute);
072: }
073:
074: public synchronized boolean remove(Class attributeCategory) {
075: return aset.remove(attributeCategory);
076: }
077:
078: public synchronized int size() {
079: return aset.size();
080: }
081:
082: public synchronized Attribute[] toArray() {
083: return aset.toArray();
084: }
085:
086: }
087:
088: private static class SynchronizedDocAttributeSet extends
089: SynchronizedAttributeSet implements DocAttributeSet,
090: Serializable {
091:
092: static final long serialVersionUID = 6455869095246629354L;
093:
094: public SynchronizedDocAttributeSet(DocAttributeSet attributeSet) {
095: super (attributeSet);
096: }
097: }
098:
099: private static class SynchronizedPrintJobAttributeSet extends
100: SynchronizedAttributeSet implements PrintJobAttributeSet,
101: Serializable {
102:
103: static final long serialVersionUID = 2117188707856965749L;
104:
105: public SynchronizedPrintJobAttributeSet(
106: PrintJobAttributeSet attributeSet) {
107: super (attributeSet);
108: }
109: }
110:
111: private static class SynchronizedPrintRequestAttributeSet extends
112: SynchronizedAttributeSet implements
113: PrintRequestAttributeSet, Serializable {
114:
115: static final long serialVersionUID = 5671237023971169027L;
116:
117: public SynchronizedPrintRequestAttributeSet(
118: PrintRequestAttributeSet attributeSet) {
119: super (attributeSet);
120: }
121: }
122:
123: private static class SynchronizedPrintServiceAttributeSet extends
124: SynchronizedAttributeSet implements
125: PrintServiceAttributeSet, Serializable {
126:
127: static final long serialVersionUID = -2830705374001675073L;
128:
129: public SynchronizedPrintServiceAttributeSet(
130: PrintServiceAttributeSet attributeSet) {
131: super (attributeSet);
132: }
133: }
134:
135: private static class UnmodifiableAttributeSet implements
136: AttributeSet, Serializable {
137:
138: static final long serialVersionUID = -6131802583863447813L;
139:
140: private AttributeSet aset;
141:
142: public UnmodifiableAttributeSet(AttributeSet attributeSet) {
143: aset = attributeSet;
144: }
145:
146: public boolean add(Attribute attribute) {
147: throw new UnmodifiableSetException(
148: "Unmodifiable attribute set");
149: }
150:
151: public boolean addAll(AttributeSet attributeSet) {
152: throw new UnmodifiableSetException(
153: "Unmodifiable attribute set");
154: }
155:
156: public void clear() {
157: throw new UnmodifiableSetException(
158: "Unmodifiable attribute set");
159: }
160:
161: public boolean containsKey(Class attributeCategory) {
162: return aset.containsKey(attributeCategory);
163: }
164:
165: public boolean containsValue(Attribute attribute) {
166: return aset.containsValue(attribute);
167: }
168:
169: public boolean equals(Object object) {
170: return aset.equals(object);
171: }
172:
173: public Attribute get(Class attributeCategory) {
174: return aset.get(attributeCategory);
175: }
176:
177: public int hashCode() {
178: return aset.hashCode();
179: }
180:
181: public boolean isEmpty() {
182: return aset.isEmpty();
183: }
184:
185: public boolean remove(Attribute attribute) {
186: throw new UnmodifiableSetException(
187: "Unmodifiable attribute set");
188: }
189:
190: public synchronized boolean remove(Class attributeCategory) {
191: throw new UnmodifiableSetException(
192: "Unmodifiable attribute set");
193: }
194:
195: public int size() {
196: return aset.size();
197: }
198:
199: public Attribute[] toArray() {
200: return aset.toArray();
201: }
202:
203: }
204:
205: private static class UnmodifiableDocAttributeSet extends
206: UnmodifiableAttributeSet implements DocAttributeSet,
207: Serializable {
208:
209: static final long serialVersionUID = -6349408326066898956L;
210:
211: public UnmodifiableDocAttributeSet(DocAttributeSet attributeSet) {
212: super (attributeSet);
213: }
214: }
215:
216: private static class UnmodifiablePrintJobAttributeSet extends
217: UnmodifiableAttributeSet implements PrintJobAttributeSet,
218: Serializable {
219:
220: static final long serialVersionUID = -8002245296274522112L;
221:
222: public UnmodifiablePrintJobAttributeSet(
223: PrintJobAttributeSet attributeSet) {
224: super (attributeSet);
225: }
226: }
227:
228: private static class UnmodifiablePrintRequestAttributeSet extends
229: UnmodifiableAttributeSet implements
230: PrintRequestAttributeSet, Serializable {
231:
232: static final long serialVersionUID = 7799373532614825073L;
233:
234: public UnmodifiablePrintRequestAttributeSet(
235: PrintRequestAttributeSet attributeSet) {
236: super (attributeSet);
237: }
238: }
239:
240: private static class UnmodifiablePrintServiceAttributeSet extends
241: UnmodifiableAttributeSet implements
242: PrintServiceAttributeSet, Serializable {
243:
244: static final long serialVersionUID = -7112165137107826819L;
245:
246: public UnmodifiablePrintServiceAttributeSet(
247: PrintServiceAttributeSet attributeSet) {
248: super (attributeSet);
249: }
250: }
251:
252: private AttributeSetUtilities() {
253:
254: }
255:
256: public static AttributeSet synchronizedView(
257: AttributeSet attributeSet) {
258: if (attributeSet == null) {
259: throw new NullPointerException("Null attribute set");
260: }
261: return new SynchronizedAttributeSet(attributeSet);
262: }
263:
264: public static DocAttributeSet synchronizedView(
265: DocAttributeSet attributeSet) {
266: if (attributeSet == null) {
267: throw new NullPointerException("Null attribute set");
268: }
269: return new SynchronizedDocAttributeSet(attributeSet);
270: }
271:
272: public static PrintRequestAttributeSet synchronizedView(
273: PrintRequestAttributeSet attributeSet) {
274: if (attributeSet == null) {
275: throw new NullPointerException("Null attribute set");
276: }
277: return new SynchronizedPrintRequestAttributeSet(attributeSet);
278: }
279:
280: public static PrintJobAttributeSet synchronizedView(
281: PrintJobAttributeSet attributeSet) {
282: if (attributeSet == null) {
283: throw new NullPointerException("Null attribute set");
284: }
285: return new SynchronizedPrintJobAttributeSet(attributeSet);
286: }
287:
288: public static PrintServiceAttributeSet synchronizedView(
289: PrintServiceAttributeSet attributeSet) {
290: if (attributeSet == null) {
291: throw new NullPointerException("Null attribute set");
292: }
293: return new SynchronizedPrintServiceAttributeSet(attributeSet);
294: }
295:
296: public static AttributeSet unmodifiableView(
297: AttributeSet attributeSet) {
298: if (attributeSet == null) {
299: throw new NullPointerException("Null attribute set");
300: }
301: return new UnmodifiableAttributeSet(attributeSet);
302: }
303:
304: public static DocAttributeSet unmodifiableView(
305: DocAttributeSet attributeSet) {
306: if (attributeSet == null) {
307: throw new NullPointerException("Null attribute set");
308: }
309: return new UnmodifiableDocAttributeSet(attributeSet);
310: }
311:
312: public static PrintJobAttributeSet unmodifiableView(
313: PrintJobAttributeSet attributeSet) {
314: if (attributeSet == null) {
315: throw new NullPointerException("Null attribute set");
316: }
317: return new UnmodifiablePrintJobAttributeSet(attributeSet);
318: }
319:
320: public static PrintRequestAttributeSet unmodifiableView(
321: PrintRequestAttributeSet attributeSet) {
322: if (attributeSet == null) {
323: throw new NullPointerException("Null attribute set");
324: }
325: return new UnmodifiablePrintRequestAttributeSet(attributeSet);
326: }
327:
328: public static PrintServiceAttributeSet unmodifiableView(
329: PrintServiceAttributeSet attributeSet) {
330: if (attributeSet == null) {
331: throw new NullPointerException("Null attribute set");
332: }
333: return new UnmodifiablePrintServiceAttributeSet(attributeSet);
334: }
335:
336: public static Class<?> verifyAttributeCategory(Object object,
337: Class<?> interfaceName) {
338: if (!(Attribute.class).isAssignableFrom(interfaceName)) {
339: throw new ClassCastException(interfaceName.getName()
340: + " is not "
341: + "interface Attribute or it's subinterface");
342: } else if (interfaceName.isAssignableFrom((Class<?>) object)) {
343: return (Class<?>) object;
344: } else {
345: throw new ClassCastException(object.getClass().getName()
346: + "doesn't implement" + interfaceName.getName());
347: }
348: }
349:
350: public static Attribute verifyAttributeValue(Object attribute,
351: Class<?> interfaceName) {
352: if (attribute == null) {
353: throw new NullPointerException("Null attribute");
354: } else if (interfaceName.isInstance(attribute)) {
355: return (Attribute) attribute;
356: } else {
357: throw new ClassCastException(
358: "Object is not an instance of "
359: + interfaceName.getName());
360: }
361: }
362:
363: public static void verifyCategoryForValue(
364: Class<?> attributeCategory, Attribute attribute) {
365: if (!attributeCategory.equals(attribute.getCategory())) {
366: throw new IllegalArgumentException(attributeCategory
367: .getName()
368: + "is not equal to the category of the attribute"
369: + attribute.getCategory().getName());
370: }
371: }
372:
373: }
|