1 package org.simantics.utils;
3 import java.util.HashMap;
5 import java.util.TreeMap;
6 import java.util.concurrent.CopyOnWriteArrayList;
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;
16 public class Development {
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<>();
22 public static final boolean DEVELOPMENT = false;
23 private static final int histogramExceptionThreshold = 100;
25 public static final String PRINT = "Development.print";
27 public interface DevelopmentListener {
29 void handle(Object event);
33 final static private HashMap<String, Variant> properties = new HashMap<>();
35 final static private CopyOnWriteArrayList<DevelopmentListener> listeners = new CopyOnWriteArrayList<>();
40 listeners.add(new DevelopmentListener() {
43 public void handle(Object event) {
44 if((Boolean) getProperty(PRINT, Bindings.BOOLEAN))
45 LOGGER.info(event.toString());
53 public static void addListener(DevelopmentListener listener) {
54 listeners.add(listener);
57 public static void removeListener(DevelopmentListener listener) {
58 listeners.remove(listener);
61 public static void dispatchEvent(Object event) {
62 for(DevelopmentListener listener : listeners) {
63 listener.handle(event);
68 public static void setProperty(String name, Object value, Binding binding) {
70 assert(binding != null);
72 binding.assertInstaceIsValid(value);
73 } catch (BindingException e) {
74 throw new RuntimeException(e);
76 properties.put(name, new Variant(binding, value));
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;
84 return (T)value.getValue(binding);
85 } catch (AdaptException e) {
86 throw new RuntimeException(e);
90 public static boolean isTrue(String name) {
91 Boolean value = getProperty(name, Bindings.BOOLEAN);
92 return value != null && value;
95 public static Map<String, Variant> getProperties() {
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());