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: * @author Alexey A. Petrenko
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.util.Collection;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.Set;
027:
028: /**
029: * RenderingHints
030: *
031: */
032: public class RenderingHints implements Map<Object, Object>, Cloneable {
033: public static final Key KEY_ALPHA_INTERPOLATION = new KeyImpl(1);
034: public static final Object VALUE_ALPHA_INTERPOLATION_DEFAULT = new KeyValue(
035: KEY_ALPHA_INTERPOLATION);
036: public static final Object VALUE_ALPHA_INTERPOLATION_SPEED = new KeyValue(
037: KEY_ALPHA_INTERPOLATION);
038: public static final Object VALUE_ALPHA_INTERPOLATION_QUALITY = new KeyValue(
039: KEY_ALPHA_INTERPOLATION);
040:
041: public static final Key KEY_ANTIALIASING = new KeyImpl(2);
042: public static final Object VALUE_ANTIALIAS_DEFAULT = new KeyValue(
043: KEY_ANTIALIASING);
044: public static final Object VALUE_ANTIALIAS_ON = new KeyValue(
045: KEY_ANTIALIASING);
046: public static final Object VALUE_ANTIALIAS_OFF = new KeyValue(
047: KEY_ANTIALIASING);
048:
049: public static final Key KEY_COLOR_RENDERING = new KeyImpl(3);
050: public static final Object VALUE_COLOR_RENDER_DEFAULT = new KeyValue(
051: KEY_COLOR_RENDERING);
052: public static final Object VALUE_COLOR_RENDER_SPEED = new KeyValue(
053: KEY_COLOR_RENDERING);
054: public static final Object VALUE_COLOR_RENDER_QUALITY = new KeyValue(
055: KEY_COLOR_RENDERING);
056:
057: public static final Key KEY_DITHERING = new KeyImpl(4);
058: public static final Object VALUE_DITHER_DEFAULT = new KeyValue(
059: KEY_DITHERING);
060: public static final Object VALUE_DITHER_DISABLE = new KeyValue(
061: KEY_DITHERING);
062: public static final Object VALUE_DITHER_ENABLE = new KeyValue(
063: KEY_DITHERING);
064:
065: public static final Key KEY_FRACTIONALMETRICS = new KeyImpl(5);
066: public static final Object VALUE_FRACTIONALMETRICS_DEFAULT = new KeyValue(
067: KEY_FRACTIONALMETRICS);
068: public static final Object VALUE_FRACTIONALMETRICS_ON = new KeyValue(
069: KEY_FRACTIONALMETRICS);
070: public static final Object VALUE_FRACTIONALMETRICS_OFF = new KeyValue(
071: KEY_FRACTIONALMETRICS);
072:
073: public static final Key KEY_INTERPOLATION = new KeyImpl(6);
074: public static final Object VALUE_INTERPOLATION_BICUBIC = new KeyValue(
075: KEY_INTERPOLATION);
076: public static final Object VALUE_INTERPOLATION_BILINEAR = new KeyValue(
077: KEY_INTERPOLATION);
078: public static final Object VALUE_INTERPOLATION_NEAREST_NEIGHBOR = new KeyValue(
079: KEY_INTERPOLATION);
080:
081: public static final Key KEY_RENDERING = new KeyImpl(7);
082: public static final Object VALUE_RENDER_DEFAULT = new KeyValue(
083: KEY_RENDERING);
084: public static final Object VALUE_RENDER_SPEED = new KeyValue(
085: KEY_RENDERING);
086: public static final Object VALUE_RENDER_QUALITY = new KeyValue(
087: KEY_RENDERING);
088:
089: public static final Key KEY_STROKE_CONTROL = new KeyImpl(8);
090: public static final Object VALUE_STROKE_DEFAULT = new KeyValue(
091: KEY_STROKE_CONTROL);
092: public static final Object VALUE_STROKE_NORMALIZE = new KeyValue(
093: KEY_STROKE_CONTROL);
094: public static final Object VALUE_STROKE_PURE = new KeyValue(
095: KEY_STROKE_CONTROL);
096:
097: public static final Key KEY_TEXT_ANTIALIASING = new KeyImpl(9);
098: public static final Object VALUE_TEXT_ANTIALIAS_DEFAULT = new KeyValue(
099: KEY_TEXT_ANTIALIASING);
100: public static final Object VALUE_TEXT_ANTIALIAS_ON = new KeyValue(
101: KEY_TEXT_ANTIALIASING);
102: public static final Object VALUE_TEXT_ANTIALIAS_OFF = new KeyValue(
103: KEY_TEXT_ANTIALIASING);
104:
105: private HashMap<Object, Object> map = new HashMap<Object, Object>();
106:
107: public RenderingHints(Map<Key, ?> map) {
108: super ();
109: if (map != null) {
110: putAll(map);
111: }
112: }
113:
114: public RenderingHints(Key key, Object value) {
115: super ();
116: put(key, value);
117: }
118:
119: public void add(RenderingHints hints) {
120: map.putAll(hints.map);
121: }
122:
123: public Object put(Object key, Object value) {
124: if (!((Key) key).isCompatibleValue(value)) {
125: throw new IllegalArgumentException();
126: }
127:
128: return map.put(key, value);
129: }
130:
131: public Object remove(Object key) {
132: return map.remove(key);
133: }
134:
135: public Object get(Object key) {
136: return map.get(key);
137: }
138:
139: public Set<Object> keySet() {
140: return map.keySet();
141: }
142:
143: public Set<Map.Entry<Object, Object>> entrySet() {
144: return map.entrySet();
145: }
146:
147: public void putAll(Map<?, ?> m) {
148: if (m instanceof RenderingHints) {
149: map.putAll(((RenderingHints) m).map);
150: } else {
151: Set<?> entries = m.entrySet();
152:
153: if (entries != null) {
154: Iterator<?> it = entries.iterator();
155: while (it.hasNext()) {
156: Map.Entry<?, ?> entry = (Map.Entry<?, ?>) it.next();
157: Key key = (Key) entry.getKey();
158: Object val = entry.getValue();
159: put(key, val);
160: }
161: }
162: }
163: }
164:
165: public Collection<Object> values() {
166: return map.values();
167: }
168:
169: public boolean containsValue(Object value) {
170: return map.containsValue(value);
171: }
172:
173: public boolean containsKey(Object key) {
174: if (key == null) {
175: throw new NullPointerException();
176: }
177:
178: return map.containsKey(key);
179: }
180:
181: public boolean isEmpty() {
182: return map.isEmpty();
183: }
184:
185: public void clear() {
186: map.clear();
187: }
188:
189: public int size() {
190: return map.size();
191: }
192:
193: @Override
194: public boolean equals(Object o) {
195: if (!(o instanceof Map)) {
196: return false;
197: }
198:
199: Map<?, ?> m = (Map<?, ?>) o;
200: Set<?> keys = keySet();
201: if (!keys.equals(m.keySet())) {
202: return false;
203: }
204:
205: Iterator<?> it = keys.iterator();
206: while (it.hasNext()) {
207: Key key = (Key) it.next();
208: Object v1 = get(key);
209: Object v2 = m.get(key);
210: if (!(v1 == null ? v2 == null : v1.equals(v2))) {
211: return false;
212: }
213: }
214: return true;
215: }
216:
217: @Override
218: public int hashCode() {
219: return map.hashCode();
220: }
221:
222: @SuppressWarnings("unchecked")
223: @Override
224: public Object clone() {
225: RenderingHints clone = new RenderingHints(null);
226: clone.map = (HashMap<Object, Object>) this .map.clone();
227: return clone;
228: }
229:
230: @Override
231: public String toString() {
232: return "RenderingHints[" + map.toString() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
233: }
234:
235: /**
236: * Key
237: */
238: public abstract static class Key {
239: private final int key;
240:
241: protected Key(int key) {
242: this .key = key;
243: }
244:
245: @Override
246: public final boolean equals(Object o) {
247: return this == o;
248: }
249:
250: @Override
251: public final int hashCode() {
252: return System.identityHashCode(this );
253: }
254:
255: protected final int intKey() {
256: return key;
257: }
258:
259: public abstract boolean isCompatibleValue(Object val);
260: }
261:
262: /**
263: * Private implementation of Key class
264: */
265: private static class KeyImpl extends Key {
266:
267: protected KeyImpl(int key) {
268: super (key);
269: }
270:
271: @Override
272: public boolean isCompatibleValue(Object val) {
273: if (!(val instanceof KeyValue)) {
274: return false;
275: }
276:
277: return ((KeyValue) val).key == this ;
278: }
279: }
280:
281: /**
282: * Private class KeyValue is used as value for Key class instance.
283: */
284: private static class KeyValue {
285: private final Key key;
286:
287: protected KeyValue(Key key) {
288: this.key = key;
289: }
290: }
291: }
|