| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.mappaint;
|
|---|
| 3 |
|
|---|
| 4 | import java.util.Arrays;
|
|---|
| 5 | import java.util.Optional;
|
|---|
| 6 |
|
|---|
| 7 | import org.openstreetmap.josm.data.osm.Storage;
|
|---|
| 8 | import org.openstreetmap.josm.tools.Pair;
|
|---|
| 9 |
|
|---|
| 10 | /**
|
|---|
| 11 | * Caches styles for a single primitive.
|
|---|
| 12 | * <p>
|
|---|
| 13 | * This object is immutable.
|
|---|
| 14 | */
|
|---|
| 15 | public final class StyleCache {
|
|---|
| 16 |
|
|---|
| 17 | // TODO: clean up the intern pool from time to time (after purge or layer removal)
|
|---|
| 18 | private static final Storage<StyleCache> internPool = new Storage<>();
|
|---|
| 19 |
|
|---|
| 20 | public static final StyleCache EMPTY_STYLECACHE = (new StyleCache()).intern();
|
|---|
| 21 |
|
|---|
| 22 | private static final int PLAIN = 0;
|
|---|
| 23 | private static final int SELECTED = 1;
|
|---|
| 24 |
|
|---|
| 25 | @SuppressWarnings("unchecked")
|
|---|
| 26 | private final DividedScale<StyleElementList>[] states = new DividedScale[2];
|
|---|
| 27 |
|
|---|
| 28 | private StyleCache(StyleCache sc) {
|
|---|
| 29 | states[0] = sc.states[0];
|
|---|
| 30 | states[1] = sc.states[1];
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | private StyleCache() {
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * Creates a new copy of this style cache with a new entry added.
|
|---|
| 38 | * @param o The style to cache.
|
|---|
| 39 | * @param r The range the style is for.
|
|---|
| 40 | * @param selected The style list we should use (selected/unselected)
|
|---|
| 41 | * @return The new object.
|
|---|
| 42 | */
|
|---|
| 43 | public StyleCache put(StyleElementList o, Range r, boolean selected) {
|
|---|
| 44 | StyleCache s = new StyleCache(this);
|
|---|
| 45 |
|
|---|
| 46 | int idx = getIndex(selected);
|
|---|
| 47 | s.states[idx] = Optional.ofNullable(s.states[idx]).orElseGet(DividedScale::new).put(o, r);
|
|---|
| 48 | return s.intern();
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | public Pair<StyleElementList, Range> getWithRange(double scale, boolean selected) {
|
|---|
| 52 | int idx = getIndex(selected);
|
|---|
| 53 | if (states[idx] == null) {
|
|---|
| 54 | return Pair.create(null, Range.ZERO_TO_INFINITY);
|
|---|
| 55 | }
|
|---|
| 56 | return states[idx].getWithRange(scale);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | private static int getIndex(boolean selected) {
|
|---|
| 60 | return selected ? SELECTED : PLAIN;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | @Override
|
|---|
| 64 | public int hashCode() {
|
|---|
| 65 | return Arrays.deepHashCode(this.states);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | @Override
|
|---|
| 69 | public boolean equals(Object obj) {
|
|---|
| 70 | if (obj == null) {
|
|---|
| 71 | return false;
|
|---|
| 72 | }
|
|---|
| 73 | if (getClass() != obj.getClass()) {
|
|---|
| 74 | return false;
|
|---|
| 75 | }
|
|---|
| 76 | final StyleCache other = (StyleCache) obj;
|
|---|
| 77 | return Arrays.deepEquals(this.states, other.states);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * Like String.intern() (reduce memory consumption).
|
|---|
| 82 | * StyleCache must not be changed after it has been added to the intern pool.
|
|---|
| 83 | * @return style cache
|
|---|
| 84 | */
|
|---|
| 85 | private StyleCache intern() {
|
|---|
| 86 | return internPool.putUnique(this);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /**
|
|---|
| 90 | * Get the size of the intern pool. Only for tests!
|
|---|
| 91 | * @return size of the intern pool
|
|---|
| 92 | */
|
|---|
| 93 | public static int getInternPoolSize() {
|
|---|
| 94 | return internPool.size();
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|