]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/factory/BindingRepository.java
Improved Bindings.getBinding(Class) caching for Datatype.class
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / factory / BindingRepository.java
index f1472068f33e83ca402c63a407fb0a38c92a9e62..ba24353712c7f90f415b4b2652f7699dba5b27d0 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.databoard.binding.factory;\r
-\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-import java.util.Map.Entry;\r
-\r
-import org.simantics.databoard.binding.Binding;\r
-import org.simantics.databoard.binding.reflection.BindingRequest;\r
-\r
-public class BindingRepository {\r
-       \r
-       /**\r
-        * This map contains all the bindings\r
-        */\r
-       Map<BindingRequest, Binding> requestMap;\r
-       \r
-       /**\r
-        * This map contains bindings for non-generic classes. \r
-        */\r
-       Map<Class<?>, Binding> classMap;\r
-       \r
-       public BindingRepository() {            \r
-               requestMap = new HashMap<BindingRequest, Binding>();\r
-               classMap = new HashMap<Class<?>, Binding>();\r
-       }\r
-       \r
-       public BindingRepository(Map<BindingRequest, Binding> requests) {\r
-               this.requestMap = requests;\r
-               for (Entry<BindingRequest, Binding> e : requests.entrySet()) {\r
-                       if ( isClassRequest( e.getKey() ) ) {\r
-                               classMap.put(e.getKey().getClazz(), e.getValue());\r
-                       }\r
-               }\r
-       }\r
-       \r
-       /**\r
-        * Get binding for a class. May return the class if it is in the class map.\r
-        * If not, the user should try request map with some arguments (component bindings)\r
-        * \r
-        * @param clazz\r
-        * @return binding\r
-        */\r
-       public synchronized Binding get(Class<?> clazz) {\r
-               return classMap.get( clazz );\r
-       }\r
-       \r
-       /**\r
-        * Get binding for a binding request\r
-        * \r
-        * @param request\r
-        * @return binding or null\r
-        */\r
-       public synchronized Binding get( BindingRequest request ) {\r
-               return requestMap.get( request );\r
-       }\r
-       \r
-       /**\r
-        * Adds binding to the repository. If the request has no arguments, the \r
-        * class is added to classMap, and is available with class request.  \r
-        * \r
-        * @param request\r
-        * @param binding\r
-        */\r
-       public synchronized void put( BindingRequest request, Binding binding ) {\r
-               if ( isClassRequest(request) ) {\r
-                       classMap.put( request.getClazz(), binding );\r
-               }\r
-               requestMap.put( request, binding );\r
-       }\r
-       \r
-       public synchronized void remove(Binding binding) {\r
-               for (Entry<BindingRequest, Binding> e : requestMap.entrySet()) {\r
-                       if (e.getValue() == binding) {\r
-                               requestMap.remove(e.getValue());\r
-                               break;\r
-                       }\r
-               }\r
-               \r
-               for (Entry<Class<?>, Binding> e : classMap.entrySet()) {\r
-                       if (e.getValue() == binding) {\r
-                               classMap.remove(e.getValue());\r
-                               break;\r
-                       }\r
-               }\r
-}\r
-       \r
-       public synchronized boolean containsRequest( BindingRequest request ) {\r
-               return requestMap.containsKey( request );\r
-       }\r
-       \r
-       /**\r
-        * Checks if repository contains a class without arguments\r
-        * \r
-        * @param clazz \r
-        * @return true if contains class (without args)\r
-        */\r
-       public synchronized boolean containsClass( Class<?> clazz ) {\r
-               return classMap.containsKey( clazz );\r
-       }\r
-       \r
-       public synchronized void clear() {\r
-               requestMap.clear();\r
-               classMap.clear();\r
-       }\r
-\r
-       boolean isClassRequest( BindingRequest request )\r
-       {\r
-               return request.className != null && (request.annotations==null || request.annotations.length==0);\r
-       }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *     Semantum Oy - gitlab #82
+ *******************************************************************************/
+package org.simantics.databoard.binding.factory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.binding.reflection.BindingRequest;
+import org.simantics.databoard.type.Datatype;
+
+public class BindingRepository {
+       
+       /**
+        * This map contains all the bindings
+        */
+       Map<BindingRequest, Binding> requestMap;
+       
+       /**
+        * This map contains bindings for non-generic classes. 
+        */
+       Map<Class<?>, Binding> classMap;
+       
+       public BindingRepository() {            
+               requestMap = new HashMap<BindingRequest, Binding>();
+               classMap = new HashMap<Class<?>, Binding>();
+       }
+       
+       public BindingRepository(Map<BindingRequest, Binding> requests) {
+               this.requestMap = requests;
+               for (Entry<BindingRequest, Binding> e : requests.entrySet()) {
+                       if ( isClassRequest( e.getKey() ) ) {
+                               classMap.put(e.getKey().getClazz(), e.getValue());
+                       }
+               }
+       }
+       
+       /**
+        * Get binding for a class. May return the class if it is in the class map.
+        * If not, the user should try request map with some arguments (component bindings)
+        * 
+        * @param clazz
+        * @return binding
+        */
+       public synchronized Binding get(Class<?> clazz) {
+               return classMap.get( clazz );
+       }
+       
+       /**
+        * Get binding for a binding request
+        * 
+        * @param request
+        * @return binding or null
+        */
+       public synchronized Binding get( BindingRequest request ) {
+               return requestMap.get( request );
+       }
+       
+       /**
+        * Adds binding to the repository. If the request has no arguments, the 
+        * class is added to classMap, and is available with class request.  
+        * 
+        * @param request
+        * @param binding
+        */
+       public synchronized void put( BindingRequest request, Binding binding ) {
+               if ( isClassRequest(request) ) {
+                       classMap.put( request.getClazz(), binding );
+               }
+               requestMap.put( request, binding );
+       }
+       
+       public synchronized void remove(Binding binding) {
+               for (Entry<BindingRequest, Binding> e : requestMap.entrySet()) {
+                       if (e.getValue() == binding) {
+                               requestMap.remove(e.getValue());
+                               break;
+                       }
+               }
+               
+               for (Entry<Class<?>, Binding> e : classMap.entrySet()) {
+                       if (e.getValue() == binding) {
+                               classMap.remove(e.getValue());
+                               break;
+                       }
+               }
+}
+       
+       public synchronized boolean containsRequest( BindingRequest request ) {
+               return requestMap.containsKey( request );
+       }
+       
+       /**
+        * Checks if repository contains a class without arguments
+        * 
+        * @param clazz 
+        * @return true if contains class (without args)
+        */
+       public synchronized boolean containsClass( Class<?> clazz ) {
+               return classMap.containsKey( clazz );
+       }
+       
+       public synchronized void clear() {
+               requestMap.clear();
+               classMap.clear();
+       }
+
+       /**
+        * {@link Datatype} class has annotations but it can be considered a "class
+        * request" as it is a fundamental building block of Databoard and it has a
+        * fixed structure. Therefore {@link #classMap} is allowed to contain a cached
+        * Datatype.class -> Binding mapping.
+        */
+       private static final String DATATYPE_CLASS_NAME = Datatype.class.getName();
+
+       boolean isClassRequest( BindingRequest request )
+       {
+               return (request.className != null
+                               && ((request.annotations==null || request.annotations.length==0)
+                                               || DATATYPE_CLASS_NAME.equals(request.className))
+                               );
+       }
+
+}