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