org.dynalang.mop.impl
Class CompositeClassBasedMetaobjectProtocol

java.lang.Object
  extended by org.dynalang.mop.impl.CompositeClassBasedMetaobjectProtocol
All Implemented Interfaces:
java.io.Serializable, BaseMetaobjectProtocol, CallProtocol, ClassBasedMetaobjectProtocol, MetaobjectProtocol

public class CompositeClassBasedMetaobjectProtocol
extends java.lang.Object
implements ClassBasedMetaobjectProtocol, java.io.Serializable

A MOP that is composed of ClassBasedMetaobjectProtocol instances. Every operation is dispatched first to the the first member that claims authority over the class of the target object. Only if it returns a non-authoritative answer will the others be attempted.

Version:
$Id: $
Author:
Attila Szegedi
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.dynalang.mop.BaseMetaobjectProtocol
BaseMetaobjectProtocol.Results
 
Constructor Summary
CompositeClassBasedMetaobjectProtocol(ClassBasedMetaobjectProtocol[] members)
          Creates a new composite class-based metaobject protocol from the specified members.
 
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.
 java.lang.Object call(java.lang.Object target, java.lang.Object callableId, CallProtocol callProtocol, java.util.Map args)
          Calls a method on the target object with supplied named arguments.
 java.lang.Object call(java.lang.Object target, java.lang.Object callableId, CallProtocol callProtocol, java.lang.Object... args)
          Calls a method on the target object with supplied positional arguments.
 BaseMetaobjectProtocol.Results delete(java.lang.Object target, long propertyId)
          Behaves as BaseMetaobjectProtocol.delete(Object, Object) with an integer property ID.
 BaseMetaobjectProtocol.Results delete(java.lang.Object target, java.lang.Object propertyId)
          Deletes an association of a value with a property in the target object.
 java.lang.Object get(java.lang.Object target, long propertyId)
          Behaves as CallProtocol.get(Object, Object) with an integer property ID.
 java.lang.Object get(java.lang.Object target, java.lang.Object propertyId)
          Retrieves the property value from the target object.
 java.lang.Boolean has(java.lang.Object target, long propertyId)
          Behaves as BaseMetaobjectProtocol.has(Object, Object) with an integer property ID.
 java.lang.Boolean has(java.lang.Object target, java.lang.Object propertyId)
          Tells whether the target object has a particular property.
 boolean isAuthoritativeForClass(java.lang.Class clazz)
          Returns whether this metaobject protocol has authority over objects of the specified class.
static MetaobjectProtocol[] optimize(MetaobjectProtocol[] mops)
          Optimizes a list of MOPs.
 java.util.Iterator<java.util.Map.Entry> properties(java.lang.Object target)
          Returns an iterator over the property ID-value pairs in the target object this MOP knows about (and chooses to expose - not all properties must be exposed; hidden properties are allowed).
 java.util.Iterator<? extends java.lang.Object> propertyIds(java.lang.Object target)
          Returns an iterator over the property IDs in the target object this MOP knows about (and chooses to expose - not all properties must be exposed; hidden properties are allowed).
 BaseMetaobjectProtocol.Results put(java.lang.Object target, long propertyId, java.lang.Object value, CallProtocol callProtocol)
          Behaves as BaseMetaobjectProtocol.put(Object, Object, Object,CallProtocol) with an integer property ID.
 BaseMetaobjectProtocol.Results put(java.lang.Object target, java.lang.Object propertyId, java.lang.Object value, CallProtocol callProtocol)
          Associates a value with a property in the target object.
 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 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CompositeClassBasedMetaobjectProtocol

public CompositeClassBasedMetaobjectProtocol(ClassBasedMetaobjectProtocol[] members)
Creates a new composite class-based metaobject protocol from the specified members.

Parameters:
members - the member metaobject protocols.
Method Detail

optimize

public static MetaobjectProtocol[] optimize(MetaobjectProtocol[] mops)
Optimizes a list of MOPs. If a group of adjacent MOPs in the list all implement ClassBasedMetaobjectProtocol, they will be replaced with a single instance of CompositeClassBasedMetaobjectProtocol that contains them.

Parameters:
mops - the list of MOPs to optimize
Returns:
the optimized list

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:
the return value of the call, or a special result. BaseMetaobjectProtocol.Results.notCallable is returned if the protocol can authoritatively decide that the target is not a callable, or is not callable with named arguments. BaseMetaobjectProtocol.Results.doesNotExist is returned if the callable does not exist at all. BaseMetaobjectProtocol.Results.noAuthority is returned if the protocol can not authoritatively decide whether the target is callable.

call

public java.lang.Object call(java.lang.Object target,
                             java.lang.Object callableId,
                             CallProtocol callProtocol,
                             java.util.Map args)
Description copied from interface: MetaobjectProtocol
Calls a method on the target object with supplied named arguments. Must behave identically to:
 Object callable = get(target, callableId);
 if(callable instanceof Results) {
     return callable;
 }
 return call(callable, args);
 

Specified by:
call in interface MetaobjectProtocol
Parameters:
target - the target object
callableId - the ID of the method to call
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 arguments of the call
Returns:
the result of the invocation, or a special result. Can return any return value that CallProtocol.get(Object, Object) would return while looking up the callable. Can also return any return value that CallProtocol.call(Object, CallProtocol, Map) would return while calling.

call

public java.lang.Object call(java.lang.Object target,
                             java.lang.Object callableId,
                             CallProtocol callProtocol,
                             java.lang.Object... args)
Description copied from interface: MetaobjectProtocol
Calls a method on the target object with supplied positional arguments. Must behave identically to:
 Object callable = get(target, callableId);
 if(callable instanceof Results) {
     return callable;
 }
 return call(callable, args);
 

Specified by:
call in interface MetaobjectProtocol
Parameters:
target - the target object
callableId - the ID of the method to call
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 arguments of the call
Returns:
the result of the invocation, or a special result. Can return any return value that CallProtocol.get(Object, Object) would return while looking up the callable. Can also return any return value that CallProtocol.call(Object, CallProtocol, Object[]) would return while calling.

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:
the return value of the call, or a special result. BaseMetaobjectProtocol.Results.notCallable is returned if the protocol can authoritatively decide that the target is not a callable, or is not callable with positional arguments. BaseMetaobjectProtocol.Results.doesNotExist is returned if the callable does not exist at all. BaseMetaobjectProtocol.Results.noAuthority is returned if the protocol can not authoritatively decide whether the target is callable.

delete

public BaseMetaobjectProtocol.Results delete(java.lang.Object target,
                                             long propertyId)
Description copied from interface: MetaobjectProtocol
Behaves as BaseMetaobjectProtocol.delete(Object, Object) with an integer property ID.

Specified by:
delete in interface MetaobjectProtocol
Parameters:
target - the target object
propertyId - the ID of the property.
Returns:
see BaseMetaobjectProtocol.delete(Object, Object).

delete

public BaseMetaobjectProtocol.Results delete(java.lang.Object target,
                                             java.lang.Object propertyId)
Description copied from interface: BaseMetaobjectProtocol
Deletes an association of a value with a property in the target object.

Specified by:
delete in interface BaseMetaobjectProtocol
Parameters:
target - the target object
propertyId - the ID of the property. Usually a String or an Integer, but other property ID types can also be supported.
Returns:
the status of the operation. If the protocol decides that the property did not previously exist, it will return BaseMetaobjectProtocol.Results.doesNotExist. If the protocol authoritatively decides that the property can not be deleted, it will return BaseMetaobjectProtocol.Results.notDeleteable. If the protocol doesn't have the authority to delete the property, it will leave the object unchanged, and return BaseMetaobjectProtocol.Results.noAuthority. If the operation succeeds, BaseMetaobjectProtocol.Results.ok is returned.

get

public java.lang.Object get(java.lang.Object target,
                            long propertyId)
Description copied from interface: MetaobjectProtocol
Behaves as CallProtocol.get(Object, Object) with an integer property ID.

Specified by:
get in interface MetaobjectProtocol
Parameters:
target - the target object
propertyId - the ID of the property.
Returns:
see CallProtocol.get(Object, Object).

get

public java.lang.Object get(java.lang.Object target,
                            java.lang.Object propertyId)
Description copied from interface: CallProtocol
Retrieves the property value from the target object.

Specified by:
get in interface CallProtocol
Parameters:
target - the target object
propertyId - the ID of the property. Usually a String or an Integer, but other property ID types can also be supported.
Returns:
the property value for the target object. A null value means that the protocol authoritatively decided that the value of the property is null. If the protocol decides that the property does not exist, it will return BaseMetaobjectProtocol.Results.doesNotExist. If the protocol decides that the property exists, but is not readable, it will return BaseMetaobjectProtocol.Results.notReadable. If the protocol can not make an authoritative decision, it will return BaseMetaobjectProtocol.Results.noAuthority.

has

public java.lang.Boolean has(java.lang.Object target,
                             long propertyId)
Description copied from interface: MetaobjectProtocol
Behaves as BaseMetaobjectProtocol.has(Object, Object) with an integer property ID.

Specified by:
has in interface MetaobjectProtocol
Parameters:
target - the target object
propertyId - the ID of the property.
Returns:
see BaseMetaobjectProtocol.has(Object, Object).

has

public java.lang.Boolean has(java.lang.Object target,
                             java.lang.Object propertyId)
Description copied from interface: BaseMetaobjectProtocol
Tells whether the target object has a particular property.

Specified by:
has in interface BaseMetaobjectProtocol
Parameters:
target - the target object
propertyId - the ID of the property. Usually a String or an Integer, but other property ID types can also be supported.
Returns:
Boolean.TRUE or Boolean.FALSE if the protocol can authoritatively decide whether the target has or has not the property, or null if it does not have the authority to decide.

properties

public java.util.Iterator<java.util.Map.Entry> properties(java.lang.Object target)
Description copied from interface: BaseMetaobjectProtocol
Returns an iterator over the property ID-value pairs in the target object this MOP knows about (and chooses to expose - not all properties must be exposed; hidden properties are allowed).

Specified by:
properties in interface BaseMetaobjectProtocol
Parameters:
target - the target object
Returns:
an iterator over all the property ID-value pairs.

propertyIds

public java.util.Iterator<? extends java.lang.Object> propertyIds(java.lang.Object target)
Description copied from interface: MetaobjectProtocol
Returns an iterator over the property IDs in the target object this MOP knows about (and chooses to expose - not all properties must be exposed; hidden properties are allowed).

Specified by:
propertyIds in interface MetaobjectProtocol
Parameters:
target - the target object
Returns:
an iterator over the property IDs.

put

public BaseMetaobjectProtocol.Results put(java.lang.Object target,
                                          long propertyId,
                                          java.lang.Object value,
                                          CallProtocol callProtocol)
Description copied from interface: MetaobjectProtocol
Behaves as BaseMetaobjectProtocol.put(Object, Object, Object,CallProtocol) with an integer property ID. Associates a value with a property in the target object.

Specified by:
put in interface MetaobjectProtocol
Parameters:
target - the target object
propertyId - the ID of the property. Usually a String or an Integer, but other property ID types can also be supported.
value - the new value for the property
callProtocol - a marshaller that is used to convert the value in case the property can only accept values of certain types, and the metaobject protocol allows automatic coercing to those types.
Returns:
the previous value of the property, or a special value. A null value means that the protocol authoritatively decided that the previous value of the property is null. If the protocol decides that the property does not exist, and can not be created, it will return BaseMetaobjectProtocol.Results.doesNotExist. If the protocol can not set the new value, it will return BaseMetaobjectProtocol.Results.notWritable. If the protocol doesn't have the authority to put the new property value, it will leave the object unchanged, and return BaseMetaobjectProtocol.Results.noAuthority. If the operation succeeds, BaseMetaobjectProtocol.Results.ok is returned.

put

public BaseMetaobjectProtocol.Results put(java.lang.Object target,
                                          java.lang.Object propertyId,
                                          java.lang.Object value,
                                          CallProtocol callProtocol)
Description copied from interface: BaseMetaobjectProtocol
Associates a value with a property in the target object.

Specified by:
put in interface BaseMetaobjectProtocol
Parameters:
target - the target object
propertyId - the ID of the property. Usually a String or an Integer, but other property ID types can also be supported.
value - the new value for the property
callProtocol - a marshaller that is used to convert the value in case the property can only accept values of certain types, and the metaobject protocol allows automatic coercing to those types.
Returns:
a value indicating the outcome of the operation. If the protocol decides that the property does not exist, and can not be created, it will return BaseMetaobjectProtocol.Results.doesNotExist. If the protocol authoritatively decides that the property is read-only, it will return BaseMetaobjectProtocol.Results.notWritable. If the property has a limitation on types it can take as values, and the supplied marshaller is not able to provide a suitable representation, then the value of the property is not changed, and BaseMetaobjectProtocol.Results.noRepresentation is returned. Finally, if the protocol doesn't have the authority to put the new property value, it will leave the property value unchanged, and return BaseMetaobjectProtocol.Results.noAuthority. If the operation succeeds, BaseMetaobjectProtocol.Results.ok 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:
the representation that must be instance of the targetClass, or in case targetClass is primitive, of its adequate boxing class. It can also be a special value to indicate conversion failure: BaseMetaobjectProtocol.Results.noAuthority is returned if the marshaller can not authoritatively decide on a representation; BaseMetaobjectProtocol.Results.noRepresentation is returned if the marshaller can authoritatively decide that the requested representation is not possible. representation of null should be null, except when the target class is a primitive type in which case BaseMetaobjectProtocol.Results.noRepresentation should be returned.