comparison gnv-artifacts/src/main/java/de/intevation/gnv/raster/PaletteManager.java @ 1119:7c4f81f74c47

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

http://dive4elements.wald.intevation.org