1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.binding.factory;
14 import java.util.HashMap;
16 import java.util.Map.Entry;
18 import org.simantics.databoard.binding.Binding;
19 import org.simantics.databoard.binding.reflection.BindingRequest;
21 public class BindingRepository {
24 * This map contains all the bindings
26 Map<BindingRequest, Binding> requestMap;
29 * This map contains bindings for non-generic classes.
31 Map<Class<?>, Binding> classMap;
33 public BindingRepository() {
34 requestMap = new HashMap<BindingRequest, Binding>();
35 classMap = new HashMap<Class<?>, Binding>();
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());
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)
54 public synchronized Binding get(Class<?> clazz) {
55 return classMap.get( clazz );
59 * Get binding for a binding request
62 * @return binding or null
64 public synchronized Binding get( BindingRequest request ) {
65 return requestMap.get( request );
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.
75 public synchronized void put( BindingRequest request, Binding binding ) {
76 if ( isClassRequest(request) ) {
77 classMap.put( request.getClazz(), binding );
79 requestMap.put( request, binding );
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());
90 for (Entry<Class<?>, Binding> e : classMap.entrySet()) {
91 if (e.getValue() == binding) {
92 classMap.remove(e.getValue());
98 public synchronized boolean containsRequest( BindingRequest request ) {
99 return requestMap.containsKey( request );
103 * Checks if repository contains a class without arguments
106 * @return true if contains class (without args)
108 public synchronized boolean containsClass( Class<?> clazz ) {
109 return classMap.containsKey( clazz );
112 public synchronized void clear() {
117 boolean isClassRequest( BindingRequest request )
119 return request.className != null && (request.annotations==null || request.annotations.length==0);