summaryrefslogtreecommitdiffstats
path: root/awt/java/awt/RenderingHints.java
blob: acf6fa15de07d112850dcbfa33cf6fa6227500f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/**
 * @author Alexey A. Petrenko
 * @version $Revision$
 */

package java.awt;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * The RenderingHints class represents preferences for the rendering algorithms.
 * The preferences are arbitrary and can be specified by Map objects or by
 * key-value pairs.
 * 
 * @since Android 1.0
 */
public class RenderingHints implements Map<Object, Object>, Cloneable {

    /**
     * The Constant KEY_ALPHA_INTERPOLATION - alpha interpolation rendering hint
     * key.
     */
    public static final Key KEY_ALPHA_INTERPOLATION = new KeyImpl(1);

    /**
     * The Constant VALUE_ALPHA_INTERPOLATION_DEFAULT - alpha interpolation
     * rendering hint value.
     */
    public static final Object VALUE_ALPHA_INTERPOLATION_DEFAULT = new KeyValue(
            KEY_ALPHA_INTERPOLATION);

    /**
     * The Constant VALUE_ALPHA_INTERPOLATION_SPEED - alpha interpolation
     * rendering hint value.
     */
    public static final Object VALUE_ALPHA_INTERPOLATION_SPEED = new KeyValue(
            KEY_ALPHA_INTERPOLATION);

    /**
     * The Constant VALUE_ALPHA_INTERPOLATION_QUALITY - alpha interpolation
     * rendering hint value.
     */
    public static final Object VALUE_ALPHA_INTERPOLATION_QUALITY = new KeyValue(
            KEY_ALPHA_INTERPOLATION);

    /**
     * The Constant KEY_ANTIALIASING - antialiasing rendering hint key.
     */
    public static final Key KEY_ANTIALIASING = new KeyImpl(2);

    /**
     * The Constant VALUE_ANTIALIAS_DEFAULT - antialiasing rendering hint value.
     */
    public static final Object VALUE_ANTIALIAS_DEFAULT = new KeyValue(KEY_ANTIALIASING);

    /**
     * The Constant VALUE_ANTIALIAS_ON - antialiasing rendering hint value.
     */
    public static final Object VALUE_ANTIALIAS_ON = new KeyValue(KEY_ANTIALIASING);

    /**
     * The Constant VALUE_ANTIALIAS_OFF - antialiasing rendering hint value.
     */
    public static final Object VALUE_ANTIALIAS_OFF = new KeyValue(KEY_ANTIALIASING);

    /**
     * The Constant KEY_COLOR_RENDERING - color rendering hint key.
     */
    public static final Key KEY_COLOR_RENDERING = new KeyImpl(3);

    /**
     * The Constant VALUE_COLOR_RENDER_DEFAULT - color rendering hint value.
     */
    public static final Object VALUE_COLOR_RENDER_DEFAULT = new KeyValue(KEY_COLOR_RENDERING);

    /**
     * The Constant VALUE_COLOR_RENDER_SPEED - color rendering hint value.
     */
    public static final Object VALUE_COLOR_RENDER_SPEED = new KeyValue(KEY_COLOR_RENDERING);

    /**
     * The Constant VALUE_COLOR_RENDER_QUALITY - color rendering hint value.
     */
    public static final Object VALUE_COLOR_RENDER_QUALITY = new KeyValue(KEY_COLOR_RENDERING);

    /**
     * The Constant KEY_DITHERING - dithering rendering hint key.
     */
    public static final Key KEY_DITHERING = new KeyImpl(4);

    /**
     * The Constant VALUE_DITHER_DEFAULT - dithering rendering hint value.
     */
    public static final Object VALUE_DITHER_DEFAULT = new KeyValue(KEY_DITHERING);

    /**
     * The Constant VALUE_DITHER_DISABLE - dithering rendering hint value.
     */
    public static final Object VALUE_DITHER_DISABLE = new KeyValue(KEY_DITHERING);

    /**
     * The Constant VALUE_DITHER_DISABLE - dithering rendering hint value.
     */
    public static final Object VALUE_DITHER_ENABLE = new KeyValue(KEY_DITHERING);

    /**
     * The Constant KEY_FRACTIONALMETRICS - fractional metrics rendering hint
     * key.
     */
    public static final Key KEY_FRACTIONALMETRICS = new KeyImpl(5);

    /**
     * The Constant VALUE_FRACTIONALMETRICS_DEFAULT - fractional metrics
     * rendering hint value.
     */
    public static final Object VALUE_FRACTIONALMETRICS_DEFAULT = new KeyValue(KEY_FRACTIONALMETRICS);

    /**
     * The Constant VALUE_FRACTIONALMETRICS_ON - fractional metrics rendering
     * hint value.
     */
    public static final Object VALUE_FRACTIONALMETRICS_ON = new KeyValue(KEY_FRACTIONALMETRICS);

    /**
     * The Constant VALUE_FRACTIONALMETRICS_OFF - fractional metrics rendering
     * hint value.
     */
    public static final Object VALUE_FRACTIONALMETRICS_OFF = new KeyValue(KEY_FRACTIONALMETRICS);

    /**
     * The Constant KEY_INTERPOLATION - interpolation rendering hint key.
     */
    public static final Key KEY_INTERPOLATION = new KeyImpl(6);

    /**
     * The Constant VALUE_INTERPOLATION_BICUBIC - interpolation rendering hint
     * value.
     */
    public static final Object VALUE_INTERPOLATION_BICUBIC = new KeyValue(KEY_INTERPOLATION);

    /**
     * The Constant VALUE_INTERPOLATION_BILINEAR - interpolation rendering hint
     * value.
     */
    public static final Object VALUE_INTERPOLATION_BILINEAR = new KeyValue(KEY_INTERPOLATION);

    /**
     * The Constant VALUE_INTERPOLATION_NEAREST_NEIGHBOR - interpolation
     * rendering hint value.
     */
    public static final Object VALUE_INTERPOLATION_NEAREST_NEIGHBOR = new KeyValue(
            KEY_INTERPOLATION);

    /**
     * The Constant KEY_RENDERING - rendering hint key.
     */
    public static final Key KEY_RENDERING = new KeyImpl(7);

    /**
     * The Constant VALUE_RENDER_DEFAULT - rendering hint value.
     */
    public static final Object VALUE_RENDER_DEFAULT = new KeyValue(KEY_RENDERING);

    /**
     * The Constant VALUE_RENDER_SPEED - rendering hint value.
     */
    public static final Object VALUE_RENDER_SPEED = new KeyValue(KEY_RENDERING);

    /**
     * The Constant VALUE_RENDER_QUALITY - rendering hint value.
     */
    public static final Object VALUE_RENDER_QUALITY = new KeyValue(KEY_RENDERING);

    /**
     * The Constant KEY_STROKE_CONTROL - stroke control hint key.
     */
    public static final Key KEY_STROKE_CONTROL = new KeyImpl(8);

    /**
     * The Constant VALUE_STROKE_DEFAULT - stroke hint value.
     */
    public static final Object VALUE_STROKE_DEFAULT = new KeyValue(KEY_STROKE_CONTROL);

    /**
     * The Constant VALUE_STROKE_NORMALIZE - stroke hint value.
     */
    public static final Object VALUE_STROKE_NORMALIZE = new KeyValue(KEY_STROKE_CONTROL);

    /**
     * The Constant VALUE_STROKE_PURE - stroke hint value.
     */
    public static final Object VALUE_STROKE_PURE = new KeyValue(KEY_STROKE_CONTROL);

    /**
     * The Constant KEY_TEXT_ANTIALIASING - text antialiasing hint key.
     */
    public static final Key KEY_TEXT_ANTIALIASING = new KeyImpl(9);

    /**
     * The Constant VALUE_TEXT_ANTIALIAS_DEFAULT - text antialiasing hint key.
     */
    public static final Object VALUE_TEXT_ANTIALIAS_DEFAULT = new KeyValue(KEY_TEXT_ANTIALIASING);

    /**
     * The Constant VALUE_TEXT_ANTIALIAS_ON - text antialiasing hint key.
     */
    public static final Object VALUE_TEXT_ANTIALIAS_ON = new KeyValue(KEY_TEXT_ANTIALIASING);

    /**
     * The Constant VALUE_TEXT_ANTIALIAS_OFF - text antialiasing hint key.
     */
    public static final Object VALUE_TEXT_ANTIALIAS_OFF = new KeyValue(KEY_TEXT_ANTIALIASING);

    /** The map. */
    private HashMap<Object, Object> map = new HashMap<Object, Object>();

    /**
     * Instantiates a new rendering hints object from specified Map object with
     * defined key/value pairs or null for empty RenderingHints.
     * 
     * @param map
     *            the Map object with defined key/value pairs or null for empty
     *            RenderingHints.
     */
    public RenderingHints(Map<Key, ?> map) {
        super();
        if (map != null) {
            putAll(map);
        }
    }

    /**
     * Instantiates a new rendering hints object with the specified key/value
     * pair.
     * 
     * @param key
     *            the key of hint property.
     * @param value
     *            the value of hint property.
     */
    public RenderingHints(Key key, Object value) {
        super();
        put(key, value);
    }

    /**
     * Adds the properties represented by key/value pairs from the specified
     * RenderingHints object to current object.
     * 
     * @param hints
     *            the RenderingHints to be added.
     */
    public void add(RenderingHints hints) {
        map.putAll(hints.map);
    }

    /**
     * Puts the specified value to the specified key. Neither the key nor the
     * value can be null.
     * 
     * @param key
     *            the rendering hint key.
     * @param value
     *            the rendering hint value.
     * @return the previous rendering hint value assigned to the key or null.
     */
    public Object put(Object key, Object value) {
        if (!((Key)key).isCompatibleValue(value)) {
            throw new IllegalArgumentException();
        }

        return map.put(key, value);
    }

    /**
     * Removes the specified key and corresponding value from the RenderingHints
     * object.
     * 
     * @param key
     *            the specified hint key to be removed.
     * @return the object of previous rendering hint value which is assigned to
     *         the specified key, or null.
     */
    public Object remove(Object key) {
        return map.remove(key);
    }

    /**
     * Gets the value assigned to the specified key.
     * 
     * @param key
     *            the rendering hint key.
     * @return the object assigned to the specified key.
     */
    public Object get(Object key) {
        return map.get(key);
    }

    /**
     * Returns a set of rendering hints keys for current RenderingHints object.
     * 
     * @return the set of rendering hints keys.
     */
    public Set<Object> keySet() {
        return map.keySet();
    }

    /**
     * Returns a set of Map.Entry objects which contain current RenderingHint
     * key/value pairs.
     * 
     * @return the a set of mapped RenderingHint key/value pairs.
     */
    public Set<Map.Entry<Object, Object>> entrySet() {
        return map.entrySet();
    }

    /**
     * Puts all of the preferences from the specified Map into the current
     * RenderingHints object. These mappings replace all existing preferences.
     * 
     * @param m
     *            the specified Map of preferences.
     */
    public void putAll(Map<?, ?> m) {
        if (m instanceof RenderingHints) {
            map.putAll(((RenderingHints)m).map);
        } else {
            Set<?> entries = m.entrySet();

            if (entries != null) {
                Iterator<?> it = entries.iterator();
                while (it.hasNext()) {
                    Map.Entry<?, ?> entry = (Map.Entry<?, ?>)it.next();
                    Key key = (Key)entry.getKey();
                    Object val = entry.getValue();
                    put(key, val);
                }
            }
        }
    }

    /**
     * Returns a Collection of values contained in current RenderingHints
     * object.
     * 
     * @return the Collection of RenderingHints's values.
     */
    public Collection<Object> values() {
        return map.values();
    }

    /**
     * Checks whether or not current RenderingHints object contains at least one
     * the value which is equal to the specified Object.
     * 
     * @param value
     *            the specified Object.
     * @return true, if the specified object is assigned to at least one
     *         RenderingHint's key, false otherwise.
     */
    public boolean containsValue(Object value) {
        return map.containsValue(value);
    }

    /**
     * Checks whether or not current RenderingHints object contains the key
     * which is equal to the specified Object.
     * 
     * @param key
     *            the specified Object.
     * @return true, if the RenderingHints object contains the specified Object
     *         as a key, false otherwise.
     */
    public boolean containsKey(Object key) {
        if (key == null) {
            throw new NullPointerException();
        }

        return map.containsKey(key);
    }

    /**
     * Checks whether or not the RenderingHints object contains any key/value
     * pairs.
     * 
     * @return true, if the RenderingHints object is empty, false otherwise.
     */
    public boolean isEmpty() {
        return map.isEmpty();
    }

    /**
     * Clears the RenderingHints of all key/value pairs.
     */
    public void clear() {
        map.clear();
    }

    /**
     * Returns the number of key/value pairs in the RenderingHints.
     * 
     * @return the number of key/value pairs.
     */
    public int size() {
        return map.size();
    }

    /**
     * Compares the RenderingHints object with the specified object.
     * 
     * @param o
     *            the specified Object to be compared.
     * @return true, if the Object is a Map whose key/value pairs match this
     *         RenderingHints' key/value pairs, false otherwise.
     */
    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Map)) {
            return false;
        }

        Map<?, ?> m = (Map<?, ?>)o;
        Set<?> keys = keySet();
        if (!keys.equals(m.keySet())) {
            return false;
        }

        Iterator<?> it = keys.iterator();
        while (it.hasNext()) {
            Key key = (Key)it.next();
            Object v1 = get(key);
            Object v2 = m.get(key);
            if (!(v1 == null ? v2 == null : v1.equals(v2))) {
                return false;
            }
        }
        return true;
    }

    /**
     * Returns the hash code for this RenderingHints object.
     * 
     * @return the hash code for this RenderingHints object.
     */
    @Override
    public int hashCode() {
        return map.hashCode();
    }

    /**
     * Returns the clone of the RenderingHints object with the same contents.
     * 
     * @return the clone of the RenderingHints instance.
     */
    @SuppressWarnings("unchecked")
    @Override
    public Object clone() {
        RenderingHints clone = new RenderingHints(null);
        clone.map = (HashMap<Object, Object>)this.map.clone();
        return clone;
    }

    /**
     * Returns the string representation of the RenderingHints object.
     * 
     * @return the String object which represents RenderingHints object.
     */
    @Override
    public String toString() {
        return "RenderingHints[" + map.toString() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
    }

    /**
     * The RenderingHints.Key class is abstract and defines a base type for all
     * RenderingHints keys.
     * 
     * @since Android 1.0
     */
    public abstract static class Key {

        /** The key. */
        private final int key;

        /**
         * Instantiates a new key with unique integer identifier. No two objects
         * of the same subclass with the same integer key can be instantiated.
         * 
         * @param key
         *            the unique key.
         */
        protected Key(int key) {
            this.key = key;
        }

        /**
         * Compares the Key object with the specified object.
         * 
         * @param o
         *            the specified Object to be compared.
         * @return true, if the Key is equal to the specified object, false
         *         otherwise.
         */
        @Override
        public final boolean equals(Object o) {
            return this == o;
        }

        /**
         * Returns the hash code for this Key object.
         * 
         * @return the hash code for this Key object.
         */
        @Override
        public final int hashCode() {
            return System.identityHashCode(this);
        }

        /**
         * Returns integer unique key with which this Key object has been
         * instantiated.
         * 
         * @return the integer unique key with which this Key object has been
         *         instantiated.
         */
        protected final int intKey() {
            return key;
        }

        /**
         * Checks whether or not specified value is compatible with the Key.
         * 
         * @param val
         *            the Object.
         * @return true, if the specified value is compatible with the Key,
         *         false otherwise.
         */
        public abstract boolean isCompatibleValue(Object val);
    }

    /**
     * Private implementation of Key class.
     */
    private static class KeyImpl extends Key {

        /**
         * Instantiates a new key implementation.
         * 
         * @param key
         *            the key.
         */
        protected KeyImpl(int key) {
            super(key);
        }

        @Override
        public boolean isCompatibleValue(Object val) {
            if (!(val instanceof KeyValue)) {
                return false;
            }

            return ((KeyValue)val).key == this;
        }
    }

    /**
     * Private class KeyValue is used as value for Key class instance.
     */
    private static class KeyValue {

        /**
         * The key.
         */
        private final Key key;

        /**
         * Instantiates a new key value.
         * 
         * @param key
         *            the key.
         */
        protected KeyValue(Key key) {
            this.key = key;
        }
    }
}