comparison gnv-artifacts/src/main/java/de/intevation/gnv/raster/PaletteManager.java @ 875:5e9efdda6894

merged gnv-artifacts/1.0
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:56 +0200
parents d766fe2d917a
children f953c9a559d8
comparison
equal deleted inserted replaced
722:bb3ffe7d719e 875:5e9efdda6894
1 package de.intevation.gnv.raster;
2
3 import java.lang.ref.SoftReference;
4
5 import java.util.HashMap;
6
7 /**
8 * Manages palettes by their name. Provides different levels of
9 * subdivisions.
10 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
11 */
12 public class PaletteManager
13 {
14 /**
15 * The base palette.
16 */
17 protected Palette base;
18
19 /**
20 * The description of the palette.
21 */
22 protected String description;
23
24 /**
25 * The name of the palette.
26 */
27 protected String name;
28
29 /**
30 * Already created subdivisions of the palette.
31 */
32 protected HashMap<Integer, SoftReference<Palette>> levels;
33
34 /**
35 * Default constructor.
36 */
37 public PaletteManager() {
38 }
39
40 /**
41 * Constructor to create a palette manager with a given name,
42 * description and base palette.
43 * @param name The name.
44 * @param description The description.
45 * @param base The base palette.
46 */
47 public PaletteManager(
48 String name,
49 String description,
50 Palette base
51 ) {
52 this.name = name;
53 this.description = description;
54 this.base = base;
55 levels = new HashMap<Integer, SoftReference<Palette>>();
56 }
57
58 /**
59 * Returns the description of the palette.
60 * @return The description.
61 */
62 public String getDescription() {
63 return description;
64 }
65
66 /**
67 * The name of the palette.
68 * @return The name.
69 */
70 public String getName() {
71 return name;
72 }
73
74 /**
75 * Returns the base palette.
76 * @return The base palette.
77 */
78 public Palette getBase() {
79 return base;
80 }
81
82 /**
83 * Returns the subdivided palette of a given level.
84 * @param n The level of subdivision.
85 * @return The subdivided palette.
86 */
87 public Palette getLevel(int n) {
88 if (n < 2) {
89 return base;
90 }
91
92 Integer N = Integer.valueOf(n);
93
94 Palette palette;
95
96 synchronized (levels) {
97 SoftReference<Palette> ref = levels.get(N);
98
99 if (ref != null && (palette = ref.get()) != null) {
100 return palette;
101 }
102
103 palette = base.subdivide(n);
104
105 ref = new SoftReference(palette);
106
107 levels.put(N, ref);
108 }
109
110 return palette;
111 }
112 }
113 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org