Mercurial > dive4elements > gnv-client
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/raster/PaletteManager.java Fri Sep 28 12:14:00 2012 +0200 @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2010 by Intevation GmbH + * + * This program is free software under the LGPL (>=v2.1) + * Read the file LGPL.txt coming with the software for details + * or visit http://www.gnu.org/licenses/ if it does not exist. + */ + +package de.intevation.gnv.raster; + +import java.lang.ref.SoftReference; + +import java.util.HashMap; + +/** + * Manages palettes by their name. Provides different levels of + * subdivisions. + * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> + */ +public class PaletteManager +{ + /** + * The base palette. + */ + protected Palette base; + + /** + * The description of the palette. + */ + protected String description; + + /** + * The name of the palette. + */ + protected String name; + + /** + * Already created subdivisions of the palette. + */ + protected HashMap<Integer, SoftReference<Palette>> levels; + + /** + * Default constructor. + */ + public PaletteManager() { + } + + /** + * Constructor to create a palette manager with a given name, + * description and base palette. + * @param name The name. + * @param description The description. + * @param base The base palette. + */ + public PaletteManager( + String name, + String description, + Palette base + ) { + this.name = name; + this.description = description; + this.base = base; + levels = new HashMap<Integer, SoftReference<Palette>>(); + } + + /** + * Returns the description of the palette. + * @return The description. + */ + public String getDescription() { + return description; + } + + /** + * The name of the palette. + * @return The name. + */ + public String getName() { + return name; + } + + /** + * Returns the base palette. + * @return The base palette. + */ + public Palette getBase() { + return base; + } + + /** + * Returns the subdivided palette of a given level. + * @param n The level of subdivision. + * @return The subdivided palette. + */ + public Palette getLevel(int n) { + if (n < 2) { + return base; + } + + Integer N = Integer.valueOf(n); + + Palette palette; + + synchronized (levels) { + SoftReference<Palette> ref = levels.get(N); + + if (ref != null && (palette = ref.get()) != null) { + return palette; + } + + palette = base.subdivide(n); + + ref = new SoftReference(palette); + + levels.put(N, ref); + } + + return palette; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :