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:
018: package javax.print.attribute;
019:
020: import java.io.Serializable;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023: import java.io.IOException;
024: import java.util.HashMap;
025:
026: public class HashAttributeSet implements Serializable, AttributeSet {
027: private static final long serialVersionUID = 5311560590283707917L;
028:
029: private Class<?> attributeInterfaceName;
030:
031: private transient HashMap<Class<?>, Attribute> attributesMap = new HashMap<Class<?>, Attribute>();
032:
033: public HashAttributeSet() {
034: this (Attribute.class);
035: }
036:
037: public HashAttributeSet(Attribute attribute) {
038: this (attribute, Attribute.class);
039: }
040:
041: public HashAttributeSet(Attribute[] attributes) {
042: this (attributes, Attribute.class);
043: }
044:
045: public HashAttributeSet(AttributeSet attributeSet) {
046: this (attributeSet, Attribute.class);
047: }
048:
049: protected HashAttributeSet(Class<?> interfaceName) {
050: if (interfaceName == null) {
051: throw new NullPointerException("Null attribute interface");
052: }
053: attributeInterfaceName = interfaceName;
054: }
055:
056: protected HashAttributeSet(Attribute attribute,
057: Class<?> interfaceName) {
058: if (interfaceName == null) {
059: throw new NullPointerException("Null attribute interface");
060: }
061: attributeInterfaceName = interfaceName;
062: add(attribute);
063: }
064:
065: protected HashAttributeSet(Attribute[] attributes,
066: Class<?> interfaceName) {
067: if (interfaceName == null) {
068: throw new NullPointerException("Null attribute interface");
069: }
070: attributeInterfaceName = interfaceName;
071: if (attributes != null) {
072: for (Attribute element : attributes) {
073: add(element);
074: }
075: }
076: }
077:
078: protected HashAttributeSet(AttributeSet attributeSet,
079: Class<?> interfaceName) {
080: attributeInterfaceName = interfaceName;
081: if (attributeSet != null) {
082: Attribute[] attributes = attributeSet.toArray();
083: for (Attribute element : attributes) {
084: add(element);
085: }
086: }
087: }
088:
089: private void readObject(ObjectInputStream ois)
090: throws ClassNotFoundException, IOException {
091: ois.defaultReadObject();
092: Attribute attribute;
093: attributesMap = new HashMap<Class<?>, Attribute>();
094: int n = ois.readInt();
095: for (int i = 0; i < n; i++) {
096: attribute = (Attribute) ois.readObject();
097: add(attribute);
098: }
099: }
100:
101: private void writeObject(ObjectOutputStream oos) throws IOException {
102: oos.defaultWriteObject();
103: Attribute[] attributes = toArray();
104: int n = attributes.length;
105: oos.writeInt(n);
106: for (int i = 0; i < n; i++) {
107: oos.writeObject(attributes[i]);
108: }
109: }
110:
111: public boolean add(Attribute attribute) {
112: Attribute newValue = AttributeSetUtilities
113: .verifyAttributeValue(attribute, attributeInterfaceName);
114: Object oldValue = attributesMap.put(attribute.getCategory(),
115: newValue);
116: return !attribute.equals(oldValue);
117: }
118:
119: public boolean addAll(AttributeSet attributeSet) {
120: boolean outcome = true;
121: Attribute[] attributes = attributeSet.toArray();
122: for (Attribute element : attributes) {
123: if (!add(element)) {
124: outcome = false;
125: }
126: }
127: return outcome;
128: }
129:
130: public void clear() {
131: attributesMap.clear();
132: }
133:
134: public boolean containsKey(Class<?> attributeCategory) {
135: if (attributeCategory == null) {
136: return false;
137: }
138: return attributesMap.containsKey(attributeCategory);
139: }
140:
141: public boolean containsValue(Attribute attribute) {
142: if (attribute == null) {
143: return false;
144: }
145: Object curValue = attributesMap.get(attribute.getCategory());
146: return attribute.equals(curValue);
147: }
148:
149: @Override
150: public boolean equals(Object object) {
151: if (!(object instanceof AttributeSet)
152: || ((AttributeSet) object).size() != size()) {
153: return false;
154: }
155: Attribute[] attributes = toArray();
156: for (Attribute element : attributes) {
157: if (!((AttributeSet) object).containsValue(element)) {
158: return false;
159: }
160: }
161: return true;
162: }
163:
164: public Attribute get(Class<?> attributeCategory) {
165: //1.5 support requires the following changes
166: //public Attribute get(Class<?> attributeCategory) {
167: AttributeSetUtilities.verifyAttributeCategory(
168: attributeCategory, Attribute.class);
169: return attributesMap.get(attributeCategory);
170: }
171:
172: @Override
173: public int hashCode() {
174: return attributesMap.hashCode();
175: }
176:
177: public boolean isEmpty() {
178: return attributesMap.isEmpty();
179: }
180:
181: public boolean remove(Attribute attribute) {
182: if ((attribute == null)
183: || (attributesMap.remove(attribute.getCategory()) == null)) {
184: return false;
185: }
186: return true;
187: }
188:
189: public boolean remove(Class<?> attributeCategory) {
190: if ((attributeCategory == null)
191: || (attributesMap.remove(attributeCategory) == null)) {
192: return false;
193: }
194: return true;
195: }
196:
197: public int size() {
198: return attributesMap.size();
199: }
200:
201: public Attribute[] toArray() {
202: Attribute[] attributes = new Attribute[size()];
203: attributesMap.values().toArray(attributes);
204: return attributes;
205: }
206: }
|