view gnv-artifacts/src/main/java/de/intevation/gnv/raster/PaletteManager.java @ 1145:dfe1ac687c7f tip

added tags
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:16:15 +0200
parents f953c9a559d8
children
line wrap: on
line source
/*
 * 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 :

http://dive4elements.wald.intevation.org