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 | /**
|
---|
52 | * Get the style for a specific style. Returns the range as well.
|
---|
53 | * @param scale The current scale
|
---|
54 | * @param selected true to get the state for a selected element,
|
---|
55 | * @return The style and the range it is valid for.
|
---|
56 | */
|
---|
57 | public Pair<StyleElementList, Range> getWithRange(double scale, boolean selected) {
|
---|
58 | int idx = getIndex(selected);
|
---|
59 | if (states[idx] == null) {
|
---|
60 | return Pair.create(null, Range.ZERO_TO_INFINITY);
|
---|
61 | }
|
---|
62 | return states[idx].getWithRange(scale);
|
---|
63 | }
|
---|
64 |
|
---|
65 | private static int getIndex(boolean selected) {
|
---|
66 | return selected ? SELECTED : PLAIN;
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public int hashCode() {
|
---|
71 | return Arrays.deepHashCode(this.states);
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public boolean equals(Object obj) {
|
---|
76 | if (obj == null) {
|
---|
77 | return false;
|
---|
78 | }
|
---|
79 | if (getClass() != obj.getClass()) {
|
---|
80 | return false;
|
---|
81 | }
|
---|
82 | final StyleCache other = (StyleCache) obj;
|
---|
83 | return Arrays.deepEquals(this.states, other.states);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Like String.intern() (reduce memory consumption).
|
---|
88 | * StyleCache must not be changed after it has been added to the intern pool.
|
---|
89 | * @return style cache
|
---|
90 | */
|
---|
91 | private StyleCache intern() {
|
---|
92 | return internPool.putUnique(this);
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Clears the style cache. This should only be used for testing.
|
---|
97 | * It may be removed some day and replaced by a WeakReference implementation that automatically forgets old entries.
|
---|
98 | */
|
---|
99 | static void clearStyleCachePool() {
|
---|
100 | internPool.clear();
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Get the size of the intern pool. Only for tests!
|
---|
105 | * @return size of the intern pool
|
---|
106 | */
|
---|
107 | public static int getInternPoolSize() {
|
---|
108 | return internPool.size();
|
---|
109 | }
|
---|
110 | }
|
---|