]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/profile/StyleBaseData.java
Fix warnings from newly added code
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / profile / StyleBaseData.java
1 package org.simantics.diagram.profile;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.concurrent.ConcurrentHashMap;
8
9 import org.simantics.db.Resource;
10 import org.simantics.scenegraph.profile.DataNodeMap;
11 import org.simantics.scenegraph.profile.EvaluationContext;
12 import org.simantics.scenegraph.profile.Style;
13 import org.simantics.scenegraph.profile.common.ObserverGroupListener;
14 import org.simantics.scl.runtime.tuple.Tuple;
15 import org.simantics.scl.runtime.tuple.Tuple3;
16
17 /**
18  * @author Antti Villberg
19  * @since 1.36.0
20  */
21 public class StyleBaseData {
22
23     private static StyleBaseData INSTANCE;
24
25     protected final Map<Tuple, Object> values   = new ConcurrentHashMap<>();
26
27     private Map<Tuple3, ObserverGroupListener> listeners = new HashMap<>();
28
29     private final Map<Style, List<Resource>> removals = new HashMap<>();
30
31     private StyleBaseData() {
32     }
33
34     public static StyleBaseData getInstance() {
35         if (INSTANCE == null) {
36             synchronized (StyleBaseData.class) {
37                 if (INSTANCE == null) {
38                     INSTANCE = new StyleBaseData();
39                 }
40             }
41         }
42         return INSTANCE;
43     }
44
45     public void removeValue(Tuple t) {
46         values.remove(t);
47     }
48
49     public void putValue(Tuple t, Object o) {
50         values.put(t, o);
51     }
52
53     @SuppressWarnings("unchecked")
54     public <T> T getValue(Tuple t) {
55         return (T) values.get(t);
56     }
57
58     public synchronized void removeItem(Style s, Resource r) {
59         List<Resource> l = removals.get(s);
60         if (l == null) {
61             l = new ArrayList<>();
62             removals.put(s, l);
63         }
64         l.add(r);
65     }
66
67     public void putListener(Tuple3 key, ObserverGroupListener listener) {
68         listeners.put(key, listener);
69     }
70
71     public void removeListener(Tuple3 key) {
72         listeners.remove(key);
73     }
74
75     public ObserverGroupListener getListener(Tuple3 key) {
76         return listeners.get(key);
77     }
78
79     public synchronized void applyRemovals(EvaluationContext evaluationContext, StyleBase<?> s) {
80         List<Resource> rs = removals.remove(s);
81         if (rs == null)
82             return;
83
84         DataNodeMap map = evaluationContext.getConstant(ProfileKeys.NODE_MAP);
85
86         for (Resource item : rs) {
87             s.cleanupStyleForItem(evaluationContext, map, item);
88         }
89     }
90
91 }