FileDocCategorySizeDatePackage
ValueListMap.javaAPI DocGlassfish v2 API9038Fri May 04 22:24:20 BST 2007com.sun.enterprise.admin.monitor.registry.spi

ValueListMap

public final class ValueListMap extends Object implements Map
A {@link Map} to maintain a map of listeners keyed on a particular MonitoredObjectType. It may so happen that multiple listeners are interested in listening for same type. This class is designed mainly to address this requirement. It is also possible that one listener is interested in various types such that it is existing in the mapping for multiple keys (MonitoredObjectType instances). This class also provides for this requirement.

Note the documentation of implementations of various {@link Map} interface methods in this class, because it has subtle connotations.

author
Kedar Mhaswade
since
S1AS8.0
version
$Revision: 1.5 $

Fields Summary
public static final Class
LISTENER_CLASS
private static final com.sun.enterprise.util.i18n.StringManager
sm
private final Class
valueClass
private final Map
map
Constructors Summary
public ValueListMap(Class valueClass)
Constructs the instance of this final class. It is a custom implementation of {@link Map} interface. It will not be possible to add mappings into this map for the values that denote objects that don't implement the class represented by the parameter.

param
valueClass denotes the class of the values that will be put into this map. May not be null. It is an assumption that the class of key and value is not the same.
throws
{@link IllegalArgumentException} if the class is null.

	
	                                            	                                         	  
	   
		if (valueClass == null) {
			final String msg = sm.getString("gen.illegal_arg");
			throw new IllegalArgumentException (msg);
		}
		this.map		= new HashMap();
		this.valueClass = valueClass;
	
public ValueListMap()
The default constructor to be used in most cases. The values to be added are of type {@link MonitoringLevelListener}.

		this(LISTENER_CLASS);
	
Methods Summary
private java.util.MapaddNewKey(java.lang.Object key)

		final Map v = new HashMap();
		map.put(key, v);
		return ( v );
	
public voidclear()

		map.clear();
	
public booleancontainsKey(java.lang.Object key)

		return ( map.containsKey(key) );
	
public booleancontainsValue(java.lang.Object value)
Throws {@link UnsupportedOperationException}, as there is no real application.

		throw new UnsupportedOperationException("ValueListMap:containsValue() - Not supported");
	
public java.util.SetentrySet()

		return ( map.entrySet() );
	
public java.lang.Objectget(java.lang.Object key)
Always returns a map or null if there is no mapping that exists for this key. Thus for unmapped keys, the behavior resembles the {@link HashMap}. In case there is a mapping, it returns a map such that the keys in returned map are the values that were put using the put call.

return
a Map of objects with keys that are put earlier.

		return ( map.get(key) );
	
private booleanimplementsValueClass(java.lang.Object value)

		boolean ivc = false;
		final Class[] ics = value.getClass().getInterfaces();
		for (int i = 0 ; i < ics.length ; i++) {
			if (valueClass.equals(ics[i])) {
				ivc = true;
				break;
			}
		}
		return ( ivc );
	
public booleanisEmpty()

		return ( map.isEmpty() );
	
public java.util.SetkeySet()

		return ( map.keySet() );
	
public java.lang.Objectput(java.lang.Object key, java.lang.Object value)
If the key already exists, a new mapping is created which will be put in an inner map for that key. If the key does not exist, a new key is created and then an inner map is created. The inner map is actually for the faster lookup. Thus the method renders the map like:
key1 -> Map1
key2 -> Map2

Map1 -> value1, value2, value3 ... -> 1 . This 1 is an arbitrary mapping.

		String msg = null;
		if (key == null || value == null) {
			msg = sm.getString("sm.illegal_arg");
			throw new IllegalArgumentException ("Null Argument");
		}
		if (! implementsValueClass(value)) {
			msg = sm.getString("sm.illegal_arg_class", valueClass.getName());
			throw new IllegalArgumentException(msg);
		}
		Map mm = (Map) map.get(key);  // the mapped map
		if (mm == null) {
			mm = addNewKey(key);
		}
		return mm.put(value, Integer.valueOf(1)); // the value can be mapped arbitrarily to anything!
	
public voidputAll(java.util.Map t)
Throws {@link UnsupportedOperationException}

		throw new UnsupportedOperationException("ValueListMap:putAll() - Not supported");
	
public java.lang.Objectremove(java.lang.Object keyOrValue)
Either removes the key (mapping) from the outer map or removes a key from inner mapping. Please see #put.

		Collection removed = null;
		if (valueClass.isAssignableFrom(keyOrValue.getClass())) {
			final Object value = keyOrValue; //indicating that we should try to remove this value
			removed = removeValues(value);
		}
		else {
			final Object key = keyOrValue; //indicating that we should try to remove this key
			removed = removeKeyedValues(key);
			map.remove(key);
		}
		return ( removed );
	
private java.util.CollectionremoveKeyedValues(java.lang.Object key)

		Collection list = new ArrayList();
		final Object value = this.get(key);
		if (value != null && value instanceof Map) {
			list = ((Map)value).keySet();
		}
		return ( list );
	
private java.util.CollectionremoveValues(java.lang.Object value)

		final Collection list = new ArrayList();
		final Iterator  iter  = map.keySet().iterator();
		while (iter.hasNext()) {
			final Map mm = (Map) map.get(iter.next()); //has to be map
			mm.remove(value);  // this will return the value Integer(1), which is ignored
			list.add(value);
		}
		return ( list );
	
public intsize()

		return ( map.size() );
	
public java.util.Collectionvalues()
Returns a Collection of ALL the values that were put by put calls till this point in time.

return
Collection (ArrayList) of all the values put

		//return all the mapped map's keys
		final Collection values = new ArrayList();
		final Iterator iter = this.keySet().iterator();
		while (iter.hasNext()) {
			final Map mm = (Map) map.get(iter.next());
			values.addAll(mm.keySet());
		}
		return ( values );