KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > components > util > ServiceMap


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.components.util;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.ui.internal.components.Assert;
17 import org.eclipse.ui.internal.components.framework.ComponentException;
18 import org.eclipse.ui.internal.components.framework.IServiceProvider;
19
20 /**
21  * Basic service provider implementation that provides a set of explicitly
22  * registered services instances. Optionally delegates to another service
23  * provider for any services that aren't explicitly provided by this provider.
24  *
25  * <p>EXPERIMENTAL: The components framework is currently under active development. All
26  * aspects of this class including its existence, name, and public interface are likely
27  * to change during the development of Eclipse 3.1</p>
28  *
29  * @since 3.1
30  */

31 public final class ServiceMap implements IServiceProvider {
32
33     /**
34      * Parent provider (or null if none). This provider will look to its parent if
35      * a component can't be found locally.
36      */

37     private IServiceProvider parent = null;
38     
39     /**
40      * Map keys onto component instances (null if empty)
41      */

42     private Map JavaDoc overriddenInstances = null;
43     
44     /**
45      * Creates a new service provider that initially provides no services
46      */

47     public ServiceMap() {
48     }
49     
50     /**
51      * Creates a new service provider that modifies the given parent provider.
52      *
53      * @param parent parent context to modify
54      */

55     public ServiceMap(IServiceProvider parent) {
56         Assert.isNotNull(parent);
57         this.parent = parent;
58     }
59     
60     /* (non-javadoc)
61      *
62      */

63     public Object JavaDoc getService(Object JavaDoc key) throws ComponentException {
64         if (overriddenInstances != null && overriddenInstances.containsKey(key)) {
65             return overriddenInstances.get(key);
66         }
67      
68         if (parent != null) {
69             return parent.getService(key);
70         }
71         
72         return null;
73     }
74     
75     /**
76      * Maps the given key to the given service instance
77      *
78      * @param key service key
79      * @param instance service instance
80      * @return this
81      */

82     public ServiceMap map(Object JavaDoc key, Object JavaDoc instance) {
83         if (overriddenInstances == null) {
84             overriddenInstances = new HashMap JavaDoc();
85         }
86         
87         overriddenInstances.put(key, instance);
88         
89         return this;
90     }
91     
92     /**
93      * Removes a previous mapping for the given service key
94      *
95      * @param key key to unmap
96      * @return this
97      */

98     public ServiceMap unmap(Object JavaDoc key) {
99         if (overriddenInstances == null) {
100             return this;
101         }
102         
103         overriddenInstances.remove(key);
104         
105         if (overriddenInstances.isEmpty()) {
106             overriddenInstances = null;
107         }
108         
109         return this;
110     }
111
112     /* (non-Javadoc)
113      * @see org.eclipse.core.components.IComponentProvider#hasKey(java.lang.Object)
114      */

115     public boolean hasService(Object JavaDoc key) {
116         if (overriddenInstances != null && overriddenInstances.containsKey(key)) {
117             return overriddenInstances.get(key) != null;
118         }
119      
120         if (parent != null) {
121             return parent.hasService(key);
122         }
123         
124         return false;
125     }
126 }
127
Popular Tags