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 | /**
|
---|
21 | * An empty style cache entry
|
---|
22 | */
|
---|
23 | public static final StyleCache EMPTY_STYLECACHE = (new StyleCache()).intern();
|
---|
24 |
|
---|
25 | private static final int PLAIN = 0;
|
---|
26 | private static final int SELECTED = 1;
|
---|
27 |
|
---|
28 | @SuppressWarnings("unchecked")
|
---|
29 | private final DividedScale<StyleElementList>[] states = new DividedScale[2];
|
---|
30 |
|
---|
31 | private StyleCache(StyleCache sc) {
|
---|
32 | states[0] = sc.states[0];
|
---|
33 | states[1] = sc.states[1];
|
---|
34 | }
|
---|
35 |
|
---|
36 | private StyleCache() {
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Creates a new copy of this style cache with a new entry added.
|
---|
41 | * @param o The style to cache.
|
---|
42 | * @param r The range the style is for.
|
---|
43 | * @param selected The style list we should use (selected/unselected)
|
---|
44 | * @return The new object.
|
---|
45 | */
|
---|
46 | public StyleCache put(StyleElementList o, Range r, boolean selected) {
|
---|
47 | StyleCache s = new StyleCache(this);
|
---|
48 |
|
---|
49 | int idx = getIndex(selected);
|
---|
50 | s.states[idx] = Optional.ofNullable(s.states[idx]).orElseGet(DividedScale::new).put(o, r);
|
---|
51 | return s.intern();
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Get the style for a specific style. Returns the range as well.
|
---|
56 | * @param scale The current scale
|
---|
57 | * @param selected true to get the state for a selected element,
|
---|
58 | * @return The style and the range it is valid for.
|
---|
59 | */
|
---|
60 | public Pair<StyleElementList, Range> getWithRange(double scale, boolean selected) {
|
---|
61 | int idx = getIndex(selected);
|
---|
62 | if (states[idx] == null) {
|
---|
63 | return Pair.create(null, Range.ZERO_TO_INFINITY);
|
---|
64 | }
|
---|
65 | return states[idx].getWithRange(scale);
|
---|
66 | }
|
---|
67 |
|
---|
68 | private static int getIndex(boolean selected) {
|
---|
69 | return selected ? SELECTED : PLAIN;
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public String toString() {
|
---|
74 | return "StyleCache{PLAIN: " + this.states[PLAIN] + " SELECTED: " + this.states[SELECTED] + "}";
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public int hashCode() {
|
---|
79 | return Arrays.deepHashCode(this.states);
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public boolean equals(Object obj) {
|
---|
84 | if (obj == null) {
|
---|
85 | return false;
|
---|
86 | }
|
---|
87 | if (getClass() != obj.getClass()) {
|
---|
88 | return false;
|
---|
89 | }
|
---|
90 | final StyleCache other = (StyleCache) obj;
|
---|
91 | return Arrays.deepEquals(this.states, other.states);
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Like String.intern() (reduce memory consumption).
|
---|
96 | * StyleCache must not be changed after it has been added to the intern pool.
|
---|
97 | * @return style cache
|
---|
98 | */
|
---|
99 | private StyleCache intern() {
|
---|
100 | return internPool.putUnique(this);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Clears the style cache. This should only be used for testing.
|
---|
105 | * It may be removed some day and replaced by a WeakReference implementation that automatically forgets old entries.
|
---|
106 | */
|
---|
107 | static void clearStyleCachePool() {
|
---|
108 | internPool.clear();
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Get the size of the intern pool. Only for tests!
|
---|
113 | * @return size of the intern pool
|
---|
114 | */
|
---|
115 | public static int getInternPoolSize() {
|
---|
116 | return internPool.size();
|
---|
117 | }
|
---|
118 | }
|
---|