KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > policy > BehaviourMap


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.policy;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25
26 /**
27  * Simple Map of Binding to Behaviour with observer support.
28  *
29  * @author David Caruana
30  *
31  * @param <B> the type of binding.
32  */

33 /*package*/ class BehaviourMap<B extends BehaviourBinding>
34 {
35     /**
36      * The map of bindings to behaviour
37      */

38     private Map JavaDoc<B, BehaviourDefinition<B>> index = new HashMap JavaDoc<B, BehaviourDefinition<B>>();
39     
40     /**
41      * The list of registered observers
42      */

43     private List JavaDoc<BehaviourChangeObserver<B>> observers = new ArrayList JavaDoc<BehaviourChangeObserver<B>>();
44     
45
46     /**
47      * Binds a Behaviour into the Map
48      *
49      * @param behaviourDefinition the behaviour definition to bind
50      */

51     public void put(BehaviourDefinition<B> behaviourDefinition)
52     {
53         B binding = behaviourDefinition.getBinding();
54         index.put(binding, behaviourDefinition);
55         for (BehaviourChangeObserver<B> listener : observers)
56         {
57             listener.addition(binding, behaviourDefinition.getBehaviour());
58         }
59     }
60     
61     
62     /**
63      * Gets a Behaviour from the Map
64      *
65      * @param binding the binding
66      * @return the behaviour
67      */

68     public BehaviourDefinition<B> get(B binding)
69     {
70         return index.get(binding);
71     }
72
73
74     /**
75      * Gets all bound Behaviours from the Map
76      *
77      * @return all bound behaviours
78      */

79     public Collection JavaDoc<BehaviourDefinition<B>> getAll()
80     {
81         return index.values();
82     }
83
84     
85     /**
86      * Gets the count of bound behaviours
87      *
88      * @return the count
89      */

90     public int size()
91     {
92         return index.size();
93     }
94
95     
96     /**
97      * Adds a Change Observer
98      *
99      * @param observer the change observer
100      */

101     public void addChangeObserver(BehaviourChangeObserver<B> observer)
102     {
103         observers.add(observer);
104     }
105     
106 }
107
Popular Tags