]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/Development.java
Help investigation of query caching problems via query histogram data
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / Development.java
1 package org.simantics.utils;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.TreeMap;
6 import java.util.concurrent.CopyOnWriteArrayList;
7
8 import org.simantics.databoard.Bindings;
9 import org.simantics.databoard.adapter.AdaptException;
10 import org.simantics.databoard.binding.Binding;
11 import org.simantics.databoard.binding.error.BindingException;
12 import org.simantics.databoard.binding.mutable.Variant;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 public class Development {
17
18     private static final Logger LOGGER = LoggerFactory.getLogger(Development.class);
19         public static TreeMap<String,Integer> histogram = new TreeMap<>();
20         public static Map<String, Exception> histogramExceptions = new HashMap<>();
21
22         public static final boolean DEVELOPMENT = false;
23         private static final int histogramExceptionThreshold = 100;
24
25         public static final String PRINT = "Development.print";
26
27         public interface DevelopmentListener {
28                 
29                 void handle(Object event);
30                 
31         }
32         
33         final static private HashMap<String, Variant> properties = new HashMap<>(); 
34         
35         final static private CopyOnWriteArrayList<DevelopmentListener> listeners = new CopyOnWriteArrayList<>();
36
37         static {
38                 
39                 if(DEVELOPMENT) {
40                         listeners.add(new DevelopmentListener() {
41         
42                                 @Override
43                                 public void handle(Object event) {
44                                         if((Boolean) getProperty(PRINT, Bindings.BOOLEAN))
45                                                 LOGGER.info(event.toString());
46                                 }
47         
48                         });
49                 }
50
51         }
52         
53         public static void addListener(DevelopmentListener listener) {
54                 listeners.add(listener);
55         }
56         
57         public static void removeListener(DevelopmentListener listener) {
58                 listeners.remove(listener);
59         }
60         
61         public static void dispatchEvent(Object event) {
62                 for(DevelopmentListener listener : listeners) {
63                         listener.handle(event);
64                 }
65         }
66         
67         
68         public static void setProperty(String name, Object value, Binding binding) {
69                 assert(name != null);
70                 assert(binding != null);
71                 try {
72                         binding.assertInstaceIsValid(value);
73                 } catch (BindingException e) {
74                         throw new RuntimeException(e);
75                 }
76                 properties.put(name, new Variant(binding, value));
77         }
78
79         @SuppressWarnings("unchecked")
80         public static <T> T getProperty(String name, Binding binding) {
81                 Variant value = properties.get(name);
82                 if(value == null) return null;
83                 try {
84                         return (T)value.getValue(binding);
85                 } catch (AdaptException e) {
86                         throw new RuntimeException(e);
87                 }
88         }
89         
90         public static boolean isTrue(String name) {
91                 Boolean value = getProperty(name, Bindings.BOOLEAN);
92                 return value != null && value;
93         }
94         
95         public static Map<String, Variant> getProperties() {
96                 return properties;
97         }
98         
99         public static void recordHistogram(Object o) {
100                 String key = o.toString();
101                 Integer i = histogram.get(key);
102                 histogram.put(key, i == null ? 1 : i+1);
103                 if (i != null && i >= histogramExceptionThreshold) {
104                         histogramExceptions.computeIfAbsent(key, k -> new Exception());
105                 }
106         }
107         
108 }