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

Last change on this file since 10000 was 10000, checked in by Don-vip, 8 years ago

sonar - fix various issues

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