net.sf.staccatocommons.dynamic
Interface Dynamic

All Superinterfaces:
net.sf.staccatocommons.defs.Thunk<Object>
All Known Implementing Classes:
AbstractDynamic, NullDynamic, ReflectiveDynamic

public interface Dynamic
extends net.sf.staccatocommons.defs.Thunk<Object>

Dynamics are wrapper objects that are capable of receiving any message, and determine on runtime which method of the wrapped objecy will be actually evaluated. If there is no method matching the message, errors will not occur in compile time, but in runtime - a MessageNotUnderstoodException will be thrown

Author:
flbulgarelli

Method Summary
 Dynamic $(String selector, Object... args)
          Synonym for chainingSend(String, Object...)
<T> T
as(Class<T> clazz)
          "casts" this dynamic to the desired type, by returning the wrapped value, if it can be casted to the given class, or by returning a proxy that forwards messages to send(String, Object...), otherwise.
<T> T
chainingAs(Class<T> clazz)
          "casts" this dynamic to the desired type, by returning the wrapped value, if it can be casted to the given class, or by returning a proxy that forwards messages to chainingSend(String, Object...), casting with chainingAs(Class) the result to the desired method return type, otherwise.
 Dynamic chainingSend(String selector, Object... args)
          Sends a message to this Dynamic, as defined by send(String, Object...), and returns the message result wrapped as a dynamic, or Dynamics.null_(), if the result was null.
<T> net.sf.staccatocommons.defs.Thunk<T>
delayedSend(String selector, Object... args)
          Asynchronously sends a message to this dynamic object by returning a thunk that, when evaluated, performs the actual message passing
<T> T
send(String selector, Object... args)
          Sends a message to this Dynamic, and returns the message result If there is no method matching for the given selector and argument count and types, the message is said to not being understood and a MessageNotUnderstoodException is thrown If there exists more than one method matching the given selector and arguments count and types, Dynamic makes no assumptions about which one will be evaluated.
 Object value()
          The wrapped value
 

Method Detail

send

<T> T send(@NonNull
           String selector,
           @NonNull
           Object... args)
Sends a message to this Dynamic, and returns the message result If there is no method matching for the given selector and argument count and types, the message is said to not being understood and a MessageNotUnderstoodException is thrown If there exists more than one method matching the given selector and arguments count and types, Dynamic makes no assumptions about which one will be evaluated.

Type Parameters:
T -
Parameters:
selector - the message name
args - the message arguments
Returns:
the message result
Throws:
MessageNotUnderstoodException - if this dynamic object does not understand the given message
MethodEvaluationException - if a matching method exists, but its evaluation threw a an exception. The original exception is its cause

chainingSend

@NonNull
Dynamic chainingSend(@NonNull
                             String selector,
                             @NonNull
                             Object... args)
Sends a message to this Dynamic, as defined by send(String, Object...), and returns the message result wrapped as a dynamic, or Dynamics.null_(), if the result was null. If the message is not understood, instead of throwing an exception, this method returns the null dynamic. This method makes chaining dynamic methods easier. Example:
 Dynamics.from(anObject).$("getFoo").$("getBar").$("getBaz").value()
 
This will return the messages chain result, or null, if any message of the chain returned null or was not understood

Parameters:
selector -
args -
Returns:
the null dynamic if the message was not understood or if answered null. The message result, wrapped as a Dynamic, otherwise.
Throws:
MethodEvaluationException - if a matching method exists, but its evaluation threw a an exception. The original exception is its cause
See Also:
Dynamics.null_()

$

@NonNull
Dynamic $(@NonNull
                  String selector,
                  @NonNull
                  Object... args)
Synonym for chainingSend(String, Object...)

Parameters:
selector -
args -
Returns:
chainingSend(String, Object...)

delayedSend

@NonNull
<T> net.sf.staccatocommons.defs.Thunk<T> delayedSend(@NonNull
                                                             String selector,
                                                             @NonNull
                                                             Object... args)
Asynchronously sends a message to this dynamic object by returning a thunk that, when evaluated, performs the actual message passing

Type Parameters:
T -
Parameters:
selector - the message selector
args - the message arguments
Returns:
a thunk that, when evaluted, will send the given message

as

@NonNull
<T> T as(@NonNull
                 Class<T> clazz)
"casts" this dynamic to the desired type, by returning the wrapped value, if it can be casted to the given class, or by returning a proxy that forwards messages to send(String, Object...), otherwise. Methods must throw ClassCastException if the actual returned value of the dynamic can not be cast the the returned value of the method, and NullPointerException if null is returned when a primitive return type is expected

Type Parameters:
T -
Parameters:
clazz -
Returns:
a new proxy of the given type, that forwards messages to this Dynamic

chainingAs

@NonNull
<T> T chainingAs(@NonNull
                         Class<T> clazz)
"casts" this dynamic to the desired type, by returning the wrapped value, if it can be casted to the given class, or by returning a proxy that forwards messages to chainingSend(String, Object...), casting with chainingAs(Class) the result to the desired method return type, otherwise. Methods must throw NullPointerException if null is returned when a primitive return type is expected

Type Parameters:
T -
Parameters:
clazz -
Returns:
a new proxy of the given type, that forwards messages to this Dynamic

value

Object value()
The wrapped value

Specified by:
value in interface net.sf.staccatocommons.defs.Thunk<Object>


Copyright © 2010-2011 Staccato-Commons. All Rights Reserved.