Removed apparent debugging code from org.simantics.utils.Development
[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
21         public static final boolean DEVELOPMENT = false;
22         
23         public static final String PRINT = "Development.print";
24
25         public interface DevelopmentListener {
26                 
27                 void handle(Object event);
28                 
29         }
30         
31         final static private HashMap<String, Variant> properties = new HashMap<>(); 
32         
33         final static private CopyOnWriteArrayList<DevelopmentListener> listeners = new CopyOnWriteArrayList<>();
34
35         static {
36                 
37                 if(DEVELOPMENT) {
38                         listeners.add(new DevelopmentListener() {
39         
40                                 @Override
41                                 public void handle(Object event) {
42                                         if((Boolean) getProperty(PRINT, Bindings.BOOLEAN))
43                                                 LOGGER.info(event.toString());
44                                 }
45         
46                         });
47                 }
48
49         }
50         
51         public static void addListener(DevelopmentListener listener) {
52                 listeners.add(listener);
53         }
54         
55         public static void removeListener(DevelopmentListener listener) {
56                 listeners.remove(listener);
57         }
58         
59         public static void dispatchEvent(Object event) {
60                 for(DevelopmentListener listener : listeners) {
61                         listener.handle(event);
62                 }
63         }
64         
65         
66         public static void setProperty(String name, Object value, Binding binding) {
67                 assert(name != null);
68                 assert(binding != null);
69                 try {
70                         binding.assertInstaceIsValid(value);
71                 } catch (BindingException e) {
72                         throw new RuntimeException(e);
73                 }
74                 properties.put(name, new Variant(binding, value));
75         }
76
77         @SuppressWarnings("unchecked")
78         public static <T> T getProperty(String name, Binding binding) {
79                 Variant value = properties.get(name);
80                 if(value == null) return null;
81                 try {
82                         return (T)value.getValue(binding);
83                 } catch (AdaptException e) {
84                         throw new RuntimeException(e);
85                 }
86         }
87         
88         public static boolean isTrue(String name) {
89                 Boolean value = getProperty(name, Bindings.BOOLEAN);
90                 return value != null && value;
91         }
92         
93         public static Map<String, Variant> getProperties() {
94                 return properties;
95         }
96         
97         public static void recordHistogram(Object o) {
98                 String key = o.toString();
99                 Integer i = histogram.get(key);
100                 histogram.put(key, i == null ? 1 : i+1);
101         }
102         
103 }