sascha@438: package de.intevation.gnv.raster;
sascha@438:
sascha@779: import java.lang.ref.SoftReference;
sascha@779:
sascha@438: import java.util.HashMap;
sascha@438:
sascha@438: /**
sascha@801: * Manages palettes by their name. Provides different levels of
sascha@801: * subdivisions.
sascha@780: * @author Sascha L. Teichmann
sascha@438: */
sascha@438: public class PaletteManager
sascha@438: {
sascha@801: /**
sascha@801: * The base palette.
sascha@801: */
sascha@438: protected Palette base;
sascha@438:
sascha@801: /**
sascha@801: * The description of the palette.
sascha@801: */
sascha@438: protected String description;
sascha@801:
sascha@801: /**
sascha@801: * The name of the palette.
sascha@801: */
sascha@438: protected String name;
sascha@438:
sascha@801: /**
sascha@801: * Already created subdivisions of the palette.
sascha@801: */
sascha@438: protected HashMap> levels;
sascha@438:
sascha@801: /**
sascha@801: * Default constructor.
sascha@801: */
sascha@438: public PaletteManager() {
sascha@438: }
sascha@438:
sascha@801: /**
sascha@801: * Constructor to create a palette manager with a given name,
sascha@801: * description and base palette.
sascha@801: * @param name The name.
sascha@801: * @param description The description.
sascha@801: * @param base The base palette.
sascha@801: */
sascha@438: public PaletteManager(
sascha@438: String name,
sascha@438: String description,
sascha@438: Palette base
sascha@438: ) {
sascha@438: this.name = name;
sascha@438: this.description = description;
sascha@438: this.base = base;
sascha@438: levels = new HashMap>();
sascha@438: }
sascha@438:
sascha@801: /**
sascha@801: * Returns the description of the palette.
sascha@801: * @return The description.
sascha@801: */
sascha@438: public String getDescription() {
sascha@438: return description;
sascha@438: }
sascha@438:
sascha@801: /**
sascha@801: * The name of the palette.
sascha@801: * @return The name.
sascha@801: */
sascha@445: public String getName() {
sascha@445: return name;
sascha@445: }
sascha@445:
sascha@801: /**
sascha@801: * Returns the base palette.
sascha@801: * @return The base palette.
sascha@801: */
sascha@445: public Palette getBase() {
sascha@445: return base;
sascha@445: }
sascha@445:
sascha@801: /**
sascha@801: * Returns the subdivided palette of a given level.
sascha@801: * @param n The level of subdivision.
sascha@801: * @return The subdivided palette.
sascha@801: */
sascha@438: public Palette getLevel(int n) {
sascha@438: if (n < 2) {
sascha@438: return base;
sascha@438: }
sascha@438:
sascha@438: Integer N = Integer.valueOf(n);
sascha@438:
sascha@438: Palette palette;
sascha@438:
sascha@438: synchronized (levels) {
sascha@438: SoftReference ref = levels.get(N);
sascha@438:
sascha@438: if (ref != null && (palette = ref.get()) != null) {
sascha@438: return palette;
sascha@438: }
sascha@438:
sascha@438: palette = base.subdivide(n);
sascha@438:
sascha@438: ref = new SoftReference(palette);
sascha@438:
sascha@438: levels.put(N, ref);
sascha@438: }
sascha@438:
sascha@438: return palette;
sascha@438: }
sascha@438: }
sascha@798: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :