source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/StyleCache.java@ 12942

Last change on this file since 12942 was 12378, checked in by michael2402, 7 years ago

Document the gui.mappaint package

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import java.util.Arrays;
5import java.util.Optional;
6
7import org.openstreetmap.josm.data.osm.Storage;
8import org.openstreetmap.josm.tools.Pair;
9
10/**
11 * Caches styles for a single primitive.
12 * <p>
13 * This object is immutable.
14 */
15public 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 int hashCode() {
74 return Arrays.deepHashCode(this.states);
75 }
76
77 @Override
78 public boolean equals(Object obj) {
79 if (obj == null) {
80 return false;
81 }
82 if (getClass() != obj.getClass()) {
83 return false;
84 }
85 final StyleCache other = (StyleCache) obj;
86 return Arrays.deepEquals(this.states, other.states);
87 }
88
89 /**
90 * Like String.intern() (reduce memory consumption).
91 * StyleCache must not be changed after it has been added to the intern pool.
92 * @return style cache
93 */
94 private StyleCache intern() {
95 return internPool.putUnique(this);
96 }
97
98 /**
99 * Clears the style cache. This should only be used for testing.
100 * It may be removed some day and replaced by a WeakReference implementation that automatically forgets old entries.
101 */
102 static void clearStyleCachePool() {
103 internPool.clear();
104 }
105
106 /**
107 * Get the size of the intern pool. Only for tests!
108 * @return size of the intern pool
109 */
110 public static int getInternPoolSize() {
111 return internPool.size();
112 }
113}
Note: See TracBrowser for help on using the repository browser.