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