KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > registry > spi > ValueListMap


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /* ValueListMap.java
25  * $Id: ValueListMap.java,v 1.3 2005/12/25 03:43:37 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:43:37 $
28  * Indentation Information:
29  * 0. Please (try to) preserve these settings.
30  * 1. Tabs are preferred over spaces.
31  * 2. In vi/vim -
32  * :set tabstop=4 :set shiftwidth=4 :set softtabstop=4
33  * 3. In S1 Studio -
34  * 1. Tools->Options->Editor Settings->Java Editor->Tab Size = 4
35  * 2. Tools->Options->Indentation Engines->Java Indentation Engine->Expand Tabs to Spaces = False.
36  * 3. Tools->Options->Indentation Engines->Java Indentation Engine->Number of Spaces per Tab = 4.
37  */

38
39 package com.sun.enterprise.admin.monitor.registry.spi;
40 import java.util.Map JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Set JavaDoc;
43 import java.util.HashSet JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.Collection JavaDoc;
46 import java.util.Iterator JavaDoc;
47
48 import com.sun.enterprise.util.i18n.StringManager;
49 /**
50  * A {@link Map} to maintain a map of listeners keyed on a particular MonitoredObjectType.
51  * It may so happen that multiple listeners are interested in listening for same
52  * type. This class is designed mainly to address this requirement. It is also
53  * possible that one listener is interested in various types such that it is
54  * existing in the mapping for multiple keys (MonitoredObjectType instances). This class
55  * also provides for this requirement.
56  * <P>
57  * Note the documentation of implementations of various {@link Map} interface methods in this class,
58  * because it has subtle connotations.
59  * @author <a HREF="mailto:Kedar.Mhaswade@sun.com">Kedar Mhaswade</a>
60  * @since S1AS8.0
61  * @version $Revision: 1.3 $
62  */

63 public final class ValueListMap implements Map JavaDoc {
64     
65     public final static Class JavaDoc LISTENER_CLASS = com.sun.enterprise.admin.monitor.registry.MonitoringLevelListener.class;
66     
67     private static final StringManager sm = StringManager.getManager(ValueListMap.class);
68     private final Class JavaDoc valueClass;
69     private final Map JavaDoc map;
70     
71     /**
72      * Constructs the instance of this final class. It is a custom implementation of
73      * {@link Map} interface. It will not be possible to add mappings into this map
74      * for the values that denote objects that don't implement the class represented by the parameter.
75      * @param valueClass denotes the class of the values that will be put into this map. May
76      * not be null. <b> It is an assumption that the class of key and value is not the same. </b>
77      * @throws {@link IllegalArgumentException} if the class is null.
78      */

79     public ValueListMap(Class JavaDoc valueClass) {
80         if (valueClass == null) {
81             final String JavaDoc msg = sm.getString("gen.illegal_arg");
82             throw new IllegalArgumentException JavaDoc (msg);
83         }
84         this.map = new HashMap JavaDoc();
85         this.valueClass = valueClass;
86     }
87     
88     /**
89      * The default constructor to be used in most cases. The values to be
90      * added are of type {@link MonitoringLevelListener}.
91      */

92     public ValueListMap () {
93         this(LISTENER_CLASS);
94     }
95     
96     public void clear() {
97         map.clear();
98     }
99     
100     public boolean containsKey(Object JavaDoc key) {
101         return ( map.containsKey(key) );
102     }
103     
104     /**
105      * Throws {@link UnsupportedOperationException}, as there is no real application.
106      */

107     public boolean containsValue(Object JavaDoc value){
108         throw new UnsupportedOperationException JavaDoc("ValueListMap:containsValue() - Not supported");
109     }
110     
111     public Set JavaDoc entrySet() {
112         return ( map.entrySet() );
113     }
114     
115     /**
116      * Always returns a map or null if there is no mapping that exists for this key. Thus
117      * for unmapped keys, the behavior resembles the {@link HashMap}. In case there is a
118      * mapping, it returns a map such that the keys in returned map are the values
119      * that were put using the put call.
120      * @return a Map of objects with keys that are put earlier.
121      */

122     public Object JavaDoc get(Object JavaDoc key) {
123         return ( map.get(key) );
124     }
125     
126     public boolean isEmpty() {
127         return ( map.isEmpty() );
128     }
129     
130     public Set JavaDoc keySet() {
131         return ( map.keySet() );
132     }
133     
134     /**
135      * If the key already exists, a new mapping is created which will be put in an
136      * inner map for that key. If the key does not exist, a new key is created and
137      * then an inner map is created. The inner map is actually for the faster lookup.
138      * Thus the method renders the map like:
139      * <pre>
140      * key1 -> Map1
141      * key2 -> Map2
142      *
143      * Map1 -> value1, value2, value3 ... -> 1 . This 1 is an arbitrary mapping.
144      * </pre>
145      */

146     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
147         String JavaDoc msg = null;
148         if (key == null || value == null) {
149             msg = sm.getString("sm.illegal_arg");
150             throw new IllegalArgumentException JavaDoc ("Null Argument");
151         }
152         if (! implementsValueClass(value)) {
153             msg = sm.getString("sm.illegal_arg_class", valueClass.getName());
154             throw new IllegalArgumentException JavaDoc(msg);
155         }
156         Map JavaDoc mm = (Map JavaDoc) map.get(key); // the mapped map
157
if (mm == null) {
158             mm = addNewKey(key);
159         }
160         return mm.put(value, new Integer JavaDoc(1)); // the value can be mapped arbitrarily to anything!
161
}
162     
163     /**
164      * Throws {@link UnsupportedOperationException}
165      */

166     public void putAll(Map JavaDoc t) {
167         throw new UnsupportedOperationException JavaDoc("ValueListMap:putAll() - Not supported");
168     }
169     
170     /**
171      * Either removes the key (mapping) from the outer map or removes a key
172      * from inner mapping. Please see #put.
173      */

174     public Object JavaDoc remove(Object JavaDoc keyOrValue) {
175         Collection JavaDoc removed = null;
176         if (valueClass.isAssignableFrom(keyOrValue.getClass())) {
177             final Object JavaDoc value = keyOrValue; //indicating that we should try to remove this value
178
removed = removeValues(value);
179         }
180         else {
181             final Object JavaDoc key = keyOrValue; //indicating that we should try to remove this key
182
removed = removeKeyedValues(key);
183             map.remove(key);
184         }
185         return ( removed );
186     }
187     
188     private Collection JavaDoc removeValues(Object JavaDoc value) {
189         final Collection JavaDoc list = new ArrayList JavaDoc();
190         final Iterator JavaDoc iter = map.keySet().iterator();
191         while (iter.hasNext()) {
192             final Map JavaDoc mm = (Map JavaDoc) map.get(iter.next()); //has to be map
193
mm.remove(value); // this will return the value Integer(1), which is ignored
194
list.add(value);
195         }
196         return ( list );
197     }
198     
199     private Collection JavaDoc removeKeyedValues(Object JavaDoc key) {
200         Collection JavaDoc list = new ArrayList JavaDoc();
201         final Object JavaDoc value = this.get(key);
202         if (value != null && value instanceof Map JavaDoc) {
203             list = ((Map JavaDoc)value).keySet();
204         }
205         return ( list );
206     }
207     
208     public int size() {
209         return ( map.size() );
210     }
211     
212     /**
213      * Returns a Collection of ALL the values that were put by put calls till
214      * this point in time.
215      * @return Collection (ArrayList) of all the values put
216      */

217     public Collection JavaDoc values() {
218         //return all the mapped map's keys
219
final Collection JavaDoc values = new ArrayList JavaDoc();
220         final Iterator JavaDoc iter = this.keySet().iterator();
221         while (iter.hasNext()) {
222             final Map JavaDoc mm = (Map JavaDoc) map.get(iter.next());
223             values.addAll(mm.keySet());
224         }
225         return ( values );
226     }
227     
228     private Map JavaDoc addNewKey(Object JavaDoc key) {
229         final Map JavaDoc v = new HashMap JavaDoc();
230         map.put(key, v);
231         return ( v );
232     }
233     
234     private boolean implementsValueClass(Object JavaDoc value) {
235         boolean ivc = false;
236         final Class JavaDoc[] ics = value.getClass().getInterfaces();
237         for (int i = 0 ; i < ics.length ; i++) {
238             if (valueClass.equals(ics[i])) {
239                 ivc = true;
240                 break;
241             }
242         }
243         return ( ivc );
244     }
245 }
246
Popular Tags