view gnv-artifacts/src/main/java/de/intevation/gnv/raster/PaletteManager.java @ 1113:e7efbded1367

Summarized changes for RELEASE 1.1 gnv-artifacts/trunk@1251 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 30 Jun 2010 12:26:04 +0000
parents d766fe2d917a
children f953c9a559d8
line wrap: on
line source
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 :

http://dive4elements.wald.intevation.org