org.dynalang.mop.collections
Class MapMetaobjectProtocol

java.lang.Object
  extended by org.dynalang.mop.impl.MetaobjectProtocolBase
      extended by org.dynalang.mop.collections.MapMetaobjectProtocol
All Implemented Interfaces:
BaseMetaobjectProtocol, CallProtocol, ClassBasedMetaobjectProtocol, MetaobjectProtocol

public class MapMetaobjectProtocol
extends MetaobjectProtocolBase
implements ClassBasedMetaobjectProtocol

A metaobject protocol that knows how to manipulate Java maps. Operates on all keys present in the map, and returns BaseMetaobjectProtocol.Results#noAuthority for keys not present (thus allowing fallback to other navigators below it).

Version:
$Id: $
Author:
Attila Szegedi

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.dynalang.mop.BaseMetaobjectProtocol
BaseMetaobjectProtocol.Results
 
Constructor Summary
MapMetaobjectProtocol()
           
 
Method Summary
 java.lang.Object call(java.lang.Object callable, CallProtocol callProtocol, java.util.Map args)
          Calls a callable object with named arguments.
 java.lang.Object call(java.lang.Object callable, CallProtocol callProtocol, java.lang.Object... args)
          Calls a callable object with positional arguments.
 BaseMetaobjectProtocol.Results delete(java.lang.Object target, java.lang.Object propertyId)
          Removes a key-value mapping from the map.
 java.lang.Object get(java.lang.Object target, java.lang.Object propertyId)
          Retrieves a value from the map.
 java.lang.Boolean has(java.lang.Object target, java.lang.Object propertyId)
          Determines whether a key is present in the map.
 boolean isAuthoritativeForClass(java.lang.Class clazz)
          Returns whether this metaobject protocol has authority over objects of the specified class.
 java.util.Iterator<java.util.Map.Entry> properties(java.lang.Object target)
          Retrieves the mappings in the map.
 BaseMetaobjectProtocol.Results put(java.lang.Object target, java.lang.Object propertyId, java.lang.Object value, CallProtocol callProtocol)
          Binds a key-value pair into the map.
 java.lang.Object representAs(java.lang.Object object, java.lang.Class targetClass)
          Returns a representation of the specified target object as an object of the specified target class.
 
Methods inherited from class org.dynalang.mop.impl.MetaobjectProtocolBase
call, call, delete, get, has, propertyIds, put
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.dynalang.mop.MetaobjectProtocol
call, call, delete, get, has, propertyIds, put
 

Constructor Detail

MapMetaobjectProtocol

public MapMetaobjectProtocol()
Method Detail

isAuthoritativeForClass

public boolean isAuthoritativeForClass(java.lang.Class clazz)
Description copied from interface: ClassBasedMetaobjectProtocol
Returns whether this metaobject protocol has authority over objects of the specified class. Note that if a MOP claims that it has authority over objects of a certain class, it can still return BaseMetaobjectProtocol.Results.noAuthority for certain objects. In that case - when used in CompositeClassBasedMetaobjectProtocol - the other participating MOPs will also be given the chance to handle the object after this MOP was given the chance first.

Specified by:
isAuthoritativeForClass in interface ClassBasedMetaobjectProtocol
Parameters:
clazz - the class of the handled object
Returns:
true if this metaobject protocol has authority over objects of the specified class, false otherwise.

call

public java.lang.Object call(java.lang.Object callable,
                             CallProtocol callProtocol,
                             java.util.Map args)
Description copied from interface: CallProtocol
Calls a callable object with named arguments.

Specified by:
call in interface CallProtocol
Parameters:
callable - the callable object
callProtocol - a marshaller that should be used by this metaobject protocol to convert the arguments to conform to expected argument types for the call.
args - the named arguments for the callable object. null must be treated as empty map. Usually, the map keys are strings, but it is possible that some protocols support non-string keys.
Returns:
BaseMetaobjectProtocol.Results#noAuthority as this MOP has no concept of callables

call

public java.lang.Object call(java.lang.Object callable,
                             CallProtocol callProtocol,
                             java.lang.Object... args)
Description copied from interface: CallProtocol
Calls a callable object with positional arguments.

Specified by:
call in interface CallProtocol
Parameters:
callable - the callable object
callProtocol - a marshaller that should be used by this metaobject protocol to convert the arguments to conform to expected argument types for the call.
args - the positional arguments for the callable object. null must be treated as empty array.
Returns:
BaseMetaobjectProtocol.Results#noAuthority as this MOP has no concept of callables

delete

public BaseMetaobjectProtocol.Results delete(java.lang.Object target,
                                             java.lang.Object propertyId)
Removes a key-value mapping from the map.

Specified by:
delete in interface BaseMetaobjectProtocol
Parameters:
target - the map to remove element from
propertyId - the map key
Returns:
BaseMetaobjectProtocol.Results#ok if the removal was successful. If the map is read-only (throws a UnsupportedOperationException on removal attempt), BaseMetaobjectProtocol.Results#notDeleteable is returned. If the map does not contain the key, or it throws a NullPointerException for a null key, or the target is not a map, BaseMetaobjectProtocol.Results#noAuthority is returned.

get

public java.lang.Object get(java.lang.Object target,
                            java.lang.Object propertyId)
Retrieves a value from the map.

Specified by:
get in interface CallProtocol
Parameters:
target - the map to retrieve from
propertyId - the key for retrieval
Returns:
If the map contains the key, returns the associated value. If the map does not contain the key, or it throws a NullPointerException for a null key, or the target is not a map, BaseMetaobjectProtocol.Results#noAuthority is returned.

has

public java.lang.Boolean has(java.lang.Object target,
                             java.lang.Object propertyId)
Determines whether a key is present in the map.

Specified by:
has in interface BaseMetaobjectProtocol
Parameters:
target - the map to test
propertyId - the key to test for
Returns:
If the map contains the key, returns Boolean.TRUE. If the map does not contain the key, or it throws a NullPointerException for a null key, or the target is not a map, BaseMetaobjectProtocol.Results#noAuthority is returned.

properties

public java.util.Iterator<java.util.Map.Entry> properties(java.lang.Object target)
Retrieves the mappings in the map.

Specified by:
properties in interface BaseMetaobjectProtocol
Parameters:
target - the map whose mappings are retrieved
Returns:
the iterator over the entry set of the map, or empty iterator if the target is not a map.

put

public BaseMetaobjectProtocol.Results put(java.lang.Object target,
                                          java.lang.Object propertyId,
                                          java.lang.Object value,
                                          CallProtocol callProtocol)
Binds a key-value pair into the map.

Specified by:
put in interface BaseMetaobjectProtocol
Parameters:
target - the map where to bind the new mapping
propertyId - the mapping key
value - the mapped value
callProtocol - not used
Returns:
BaseMetaobjectProtocol.Results#ok if the binding was successful. If the map is read-only (throws a UnsupportedOperationException on put attempt), BaseMetaobjectProtocol.Results#notWritable is returned. If the map does not contain the key, or it throws a NullPointerException for a null key or null value, or the target is not a map, BaseMetaobjectProtocol.Results#noAuthority is returned. If the map throws ClassCastException (because it is limited in types of values it can accept), BaseMetaobjectProtocol.Results#noRepresentation is returned.

representAs

public java.lang.Object representAs(java.lang.Object object,
                                    java.lang.Class targetClass)
Description copied from interface: CallProtocol
Returns a representation of the specified target object as an object of the specified target class. Typical supported target classes would be wrappers for Java primitive types that map to native types of the higher level languages. I.e. if an object is used as a boolean expression, then the language would ask the protocol to interpret it as boolean by calling representAs(Boolean.TYPE).

Specified by:
representAs in interface CallProtocol
Parameters:
object - the object to represent
targetClass - the target class for representation.
Returns:
BaseMetaobjectProtocol.Results#noAuthority as this MOP has no concept of type conversion