org.codehaus.gpars

groovyx.gpars.agent
[Java] Class AgentBase

java.lang.Object
  groovyx.gpars.agent.AgentCore
      groovyx.gpars.agent.AgentBase

abstract class AgentBase
extends AgentCore

Implements most of Agent's public method in Java

Authors:
Vaclav Pech


Nested Class Summary
private static class AgentBase.AwaitClosure

 
Field Summary
private static groovy.lang.Closure awaitClosure

private groovy.lang.Closure copy

protected java.lang.Object data

private java.util.Collection listeners

protected java.util.concurrent.locks.ReadWriteLock lock

private java.util.Collection validators

 
Fields inherited from class AgentCore
core, currentThread, errors
 
Constructor Summary
AgentBase(java.lang.Object data)

AgentBase(java.lang.Object data, groovy.lang.Closure copy)

Accepts a NullObject instance and sets the internal state to null

 
Method Summary
void addListener(groovy.lang.Closure listener)

Adds a validator checking the agent's state A listener should be a closure accepting the old and the new value in this order plus optionally the agent reference as the first argument.

void addValidator(groovy.lang.Closure validator)

Only two-argument closures are allowed

void await()

Blocks until all messages in the queue prior to call to await() complete.

private groovy.lang.Closure checkClosure(groovy.lang.Closure code)

Returns the current value of the Agent's state

java.lang.Object getInstantVal()

A shorthand method for safe message-based retrieval of the internal state.

java.lang.Object getVal()

A shorthand method for safe asynchronous message-based retrieval of the internal state.

void onMessage(org.codehaus.groovy.runtime.NullObject obj)

Accepts and invokes the closure

void onMessage(groovy.lang.Closure code)

Other messages than closures are accepted as new values for the internal state

void onMessage(java.lang.Object message)

Allows closures to set the new internal state as a whole Do not call this method directly from the outside.

java.lang.Object sendAndWait(groovy.lang.Closure message)

void updateValue(java.lang.Object newValue)

void valAsync(groovy.lang.Closure callback)

 
Methods inherited from class AgentCore
attachToThreadPool, call, getErrors, handleMessage, hasErrors, isFair, leftShift, makeFair, registerError, send, setPGroup
 

Field Detail

awaitClosure

private static final groovy.lang.Closure awaitClosure


copy

private final groovy.lang.Closure copy


data

protected java.lang.Object data


listeners

private final java.util.Collection listeners


lock

protected java.util.concurrent.locks.ReadWriteLock lock


validators

private final java.util.Collection validators


 
Constructor Detail

AgentBase

AgentBase(java.lang.Object data)


AgentBase

AgentBase(java.lang.Object data, groovy.lang.Closure copy)
Accepts a NullObject instance and sets the internal state to null
Parameters:
obj - The received message


 
Method Detail

addListener

public void addListener(groovy.lang.Closure listener)
Adds a validator checking the agent's state A listener should be a closure accepting the old and the new value in this order plus optionally the agent reference as the first argument.
Parameters:
validator - The closure with two or three arguments


addValidator

public void addValidator(groovy.lang.Closure validator)
Only two-argument closures are allowed
Parameters:
code - The passed-in closure
Returns:
Either the original closure or a two-argument closure after currying the first argument of the passed-in closure to self


await

public final void await()
Blocks until all messages in the queue prior to call to await() complete. Provides a means to synchronize with the Agent
throws:
InterruptedException If the thread gets interrupted while waiting for a value to be bound


checkClosure

private groovy.lang.Closure checkClosure(groovy.lang.Closure code)
Returns the current value of the Agent's state


getInstantVal

@java.lang.SuppressWarnings* @throws InterruptedException If the thread gets interrupted while waiting for a value to be bound
public final java.lang.Object getInstantVal()
A shorthand method for safe message-based retrieval of the internal state. Retrieves the internal state immediately by-passing the queue of tasks waiting to be processed.
Returns:
The current value of the actor's state


getVal

public final java.lang.Object getVal()
A shorthand method for safe asynchronous message-based retrieval of the internal state. The request to retrieve a value is put into the message queue, so will wait for all messages delivered earlier to complete.
Parameters:
callback - A closure to invoke with the internal state as a parameter


onMessage

final void onMessage(org.codehaus.groovy.runtime.NullObject obj)
Accepts and invokes the closure
Parameters:
code - The received message


onMessage

final void onMessage(groovy.lang.Closure code)
Other messages than closures are accepted as new values for the internal state
Parameters:
message - The received message


onMessage

final void onMessage(java.lang.Object message)
Allows closures to set the new internal state as a whole Do not call this method directly from the outside. It is designed to be used from within the submitted closures.
Parameters:
newValue - The value to set the internal state to


sendAndWait

@java.lang.SuppressWarnings});
public final java.lang.Object sendAndWait(groovy.lang.Closure message)


updateValue

@java.lang.SuppressWarningslistener.call(new Object[]{oldValue, newValue});
public final void updateValue(java.lang.Object newValue)


valAsync

public final void valAsync(groovy.lang.Closure callback)


 

Copyright © 2008–2013 Václav Pech. All Rights Reserved.