]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/factory/BindingRepository.java
Fixing several binding-related bugs
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / factory / BindingRepository.java
index ba24353712c7f90f415b4b2652f7699dba5b27d0..5a0239f26199a6a54174857298ffc4b0581b4422 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2018 Association for Decentralized Information Management in
+ * Copyright (c) 2007, 2019 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
@@ -8,7 +8,7 @@
  *
  * Contributors:
  *     VTT Technical Research Centre of Finland - initial API and implementation
- *     Semantum Oy - gitlab #82
+ *     Semantum Oy - gitlab #82, gitlab #313
  *******************************************************************************/
 package org.simantics.databoard.binding.factory;
 
@@ -18,10 +18,12 @@ import java.util.Map.Entry;
 
 import org.simantics.databoard.binding.Binding;
 import org.simantics.databoard.binding.reflection.BindingRequest;
-import org.simantics.databoard.type.Datatype;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class BindingRepository {
-       
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(BindingRepository.class);
        /**
         * This map contains all the bindings
         */
@@ -41,7 +43,7 @@ public class BindingRepository {
                this.requestMap = requests;
                for (Entry<BindingRequest, Binding> e : requests.entrySet()) {
                        if ( isClassRequest( e.getKey() ) ) {
-                               classMap.put(e.getKey().getClazz(), e.getValue());
+                               registerClassMapping(e.getKey().getClazz(), e.getValue());
                        }
                }
        }
@@ -76,11 +78,15 @@ public class BindingRepository {
         */
        public synchronized void put( BindingRequest request, Binding binding ) {
                if ( isClassRequest(request) ) {
-                       classMap.put( request.getClazz(), binding );
+                   registerClassMapping(request.getClazz(), binding);
+               }
+               Binding existing = requestMap.put( request, binding );
+               if (existing != null && !existing.equals(binding)) {
+                   LOGGER.error("Replacing existing binding with a different one! {} {} {}", request, binding, existing);
                }
-               requestMap.put( request, binding );
        }
-       
+
+       @SuppressWarnings("unlikely-arg-type")
        public synchronized void remove(Binding binding) {
                for (Entry<BindingRequest, Binding> e : requestMap.entrySet()) {
                        if (e.getValue() == binding) {
@@ -116,20 +122,15 @@ public class BindingRepository {
                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));
+       }
 
-       boolean isClassRequest( BindingRequest request )
-       {
-               return (request.className != null
-                               && ((request.annotations==null || request.annotations.length==0)
-                                               || DATATYPE_CLASS_NAME.equals(request.className))
-                               );
+       public void registerClassMapping(Class<?> clazz, Binding binding) {
+               Binding previous = classMap.putIfAbsent(clazz, binding);
+               if (previous != null) {
+                       LOGGER.warn("WARN: Can not put same key again to classMap! {} mapping {} not replaced by {}", clazz, previous, binding);
+               }
        }
 
 }