]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/factory/BindingRepository.java
4680eee6a961b9d147fc5c0b3cb4478ae49a5e0e
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / factory / BindingRepository.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.binding.factory;
13
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Map.Entry;
17
18 import org.simantics.databoard.binding.Binding;
19 import org.simantics.databoard.binding.reflection.BindingRequest;
20
21 public class BindingRepository {
22         
23         /**
24          * This map contains all the bindings
25          */
26         Map<BindingRequest, Binding> requestMap;
27         
28         /**
29          * This map contains bindings for non-generic classes. 
30          */
31         Map<Class<?>, Binding> classMap;
32         
33         public BindingRepository() {            
34                 requestMap = new HashMap<BindingRequest, Binding>();
35                 classMap = new HashMap<Class<?>, Binding>();
36         }
37         
38         public BindingRepository(Map<BindingRequest, Binding> requests) {
39                 this.requestMap = requests;
40                 for (Entry<BindingRequest, Binding> e : requests.entrySet()) {
41                         if ( isClassRequest( e.getKey() ) ) {
42                                 classMap.put(e.getKey().getClazz(), e.getValue());
43                         }
44                 }
45         }
46         
47         /**
48          * Get binding for a class. May return the class if it is in the class map.
49          * If not, the user should try request map with some arguments (component bindings)
50          * 
51          * @param clazz
52          * @return binding
53          */
54         public synchronized Binding get(Class<?> clazz) {
55                 return classMap.get( clazz );
56         }
57         
58         /**
59          * Get binding for a binding request
60          * 
61          * @param request
62          * @return binding or null
63          */
64         public synchronized Binding get( BindingRequest request ) {
65                 return requestMap.get( request );
66         }
67         
68         /**
69          * Adds binding to the repository. If the request has no arguments, the 
70          * class is added to classMap, and is available with class request.  
71          * 
72          * @param request
73          * @param binding
74          */
75         public synchronized void put( BindingRequest request, Binding binding ) {
76                 if ( isClassRequest(request) ) {
77                         classMap.put( request.getClazz(), binding );
78                 }
79                 requestMap.put( request, binding );
80         }
81         
82         public synchronized void remove(Binding binding) {
83                 for (Entry<BindingRequest, Binding> e : requestMap.entrySet()) {
84                         if (e.getValue() == binding) {
85                                 requestMap.remove(e.getValue());
86                                 break;
87                         }
88                 }
89                 
90                 for (Entry<Class<?>, Binding> e : classMap.entrySet()) {
91                         if (e.getValue() == binding) {
92                                 classMap.remove(e.getValue());
93                                 break;
94                         }
95                 }
96 }
97         
98         public synchronized boolean containsRequest( BindingRequest request ) {
99                 return requestMap.containsKey( request );
100         }
101         
102         /**
103          * Checks if repository contains a class without arguments
104          * 
105          * @param clazz 
106          * @return true if contains class (without args)
107          */
108         public synchronized boolean containsClass( Class<?> clazz ) {
109                 return classMap.containsKey( clazz );
110         }
111         
112         public synchronized void clear() {
113                 requestMap.clear();
114                 classMap.clear();
115         }
116
117         boolean isClassRequest( BindingRequest request )
118         {
119                 return request.className != null && (request.annotations==null || request.annotations.length==0);
120         }
121
122 }