001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.mappaint;
003
004import org.openstreetmap.josm.data.osm.Storage;
005
006/**
007 * Caches styles for a single primitive.
008 */
009public final class StyleCache extends DividedScale<StyleElementList> {
010
011    // TODO: clean up the intern pool from time to time (after purge or layer removal)
012    private static final Storage<StyleCache> internPool = new Storage<>();
013
014    public static final StyleCache EMPTY_STYLECACHE = (new StyleCache()).intern();
015
016    private StyleCache(StyleCache sc) {
017        super(sc);
018    }
019
020    private StyleCache() {
021        super();
022    }
023
024    /**
025     * Add data object which is valid for the given range.
026     *
027     * This is only possible, if there is no data for the given range yet.
028     *
029     * @param o data object
030     * @param r the valid range
031     * @return a new, updated, <code>DividedScale</code> object
032     */
033    @Override
034    public StyleCache put(StyleElementList o, Range r) {
035        StyleCache s = new StyleCache(this);
036        s.putImpl(o, r.getLower(), r.getUpper());
037        s.consistencyTest();
038        s.intern();
039        return s;
040    }
041
042    /**
043     * Like String.intern() (reduce memory consumption).
044     * StyleCache must not be changed after it has been added to the intern pool.
045     * @return style cache
046     */
047    private StyleCache intern() {
048        return internPool.putUnique(this);
049    }
050}