org.javasimon
Class AbstractSimon

java.lang.Object
  extended by org.javasimon.AbstractSimon
All Implemented Interfaces:
HasAttributes, Simon
Direct Known Subclasses:
CounterImpl, StopwatchImpl, UnknownSimon

abstract class AbstractSimon
extends Object
implements Simon

AbstractSimon implements basic enable/disable and hierarchy functionality. All Simon implementations extend this class.

Author:
Richard "Virgo" Richter

Field Summary
private  AttributesSupport attributesSupport
           
protected  boolean enabled
          Simon's effective state.
protected  long firstUsage
          Timestamp of the first usage.
private  List<Simon> children
           
protected  long lastUsage
          Timestamp of the last usage.
protected  Manager manager
          Owning manager of this Simon.
private  String name
           
private  String note
           
private  Simon parent
           
private  long resetTimestamp
           
private  SimonState state
           
 
Constructor Summary
AbstractSimon(String name, Manager manager)
          Constructor of the abstract Simon is used internally by subclasses.
 
Method Summary
(package private)  void addChild(AbstractSimon simon)
          Adds child to this Simon with setting the parent of the child.
 Object getAttribute(String name)
          Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.
<T> T
getAttribute(String name, Class<T> clazz)
          Returns the value of the named attribute typed to the specified class, or null if no attribute of the given name exists.
 Iterator<String> getAttributeNames()
          Returns an Iterator containing the names of the attributes available to this Simon.
 Map<String,Object> getCopyAsSortedMap()
          Returns copy of attributes as a sorted map, this can be used further for operations like toString.
 long getFirstUsage()
          Returns ms timestamp of the first usage of this Simon.
 List<Simon> getChildren()
          Returns list of children - direct sub-simons.
 long getLastReset()
          Returns ms timestamp of the last recent usage of the Simon.reset() method on the Simon.
 long getLastUsage()
          Returns ms timestamp of the last usage of this Simon.
 String getName()
          Returns Simon name.
 String getNote()
          Returns note for the Simon.
 Simon getParent()
          Returns parent Simon.
 SimonState getState()
          Returns state of the Simon that can be enabled, disabled or ihnerited.
private  boolean isAnonymousOrRootSimon()
           
 boolean isEnabled()
          Returns true, if the Simon is enabled or if the enabled state is inherited.
 void removeAttribute(String name)
          Removes an attribute from this Simon.
(package private)  void replaceChild(Simon simon, AbstractSimon newSimon)
          Replaces one of the children for a new one (unknown to concrete).
protected  void resetCommon()
          Saves the timestamp when the Simon was reset and calls Callback.onSimonReset(Simon).
(package private)  void sampleCommon(Sample sample)
           
 void setAttribute(String name, Object value)
          Stores an attribute in this Simon.
 void setNote(String note)
          Sets note for the Simon.
(package private)  void setParent(Simon parent)
          Sets parent for this Simon - used only internally.
 void setState(SimonState state, boolean overrule)
          Sets the state of the Simon.
private  boolean shouldBeEffectivlyEnabled()
           
 String toString()
          Returns name and state of the Simon as a human readable string.
private  void updateAndPropagateEffectiveState(boolean enabled, boolean overrule)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.javasimon.Simon
reset, sample, sampleAndReset
 

Field Detail

manager

protected Manager manager
Owning manager of this Simon.


enabled

protected volatile boolean enabled
Simon's effective state.


firstUsage

protected long firstUsage
Timestamp of the first usage.


lastUsage

protected long lastUsage
Timestamp of the last usage.


name

private final String name

state

private SimonState state

parent

private Simon parent

children

private final List<Simon> children

note

private String note

resetTimestamp

private long resetTimestamp

attributesSupport

private AttributesSupport attributesSupport
Constructor Detail

AbstractSimon

AbstractSimon(String name,
              Manager manager)
Constructor of the abstract Simon is used internally by subclasses.

Parameters:
name - Simon's name
manager - owning Manager
Method Detail

getParent

public final Simon getParent()
Description copied from interface: Simon
Returns parent Simon.

Specified by:
getParent in interface Simon
Returns:
parent Simon

getChildren

public final List<Simon> getChildren()
Description copied from interface: Simon
Returns list of children - direct sub-simons.

Specified by:
getChildren in interface Simon
Returns:
list of children

setParent

final void setParent(Simon parent)
Sets parent for this Simon - used only internally.

Parameters:
parent - Simon's parent

addChild

final void addChild(AbstractSimon simon)
Adds child to this Simon with setting the parent of the child.

Parameters:
simon - future child of this Simon

getName

public final String getName()
Description copied from interface: Simon
Returns Simon name. Simon name is always fully qualified and determines also position of the Simon in the monitor hierarchy. Simon name can be null for anonymous Simons.

Specified by:
getName in interface Simon
Returns:
name of the Simon

setState

public final void setState(SimonState state,
                           boolean overrule)
Sets the state of the Simon. It must be specified whether to propagate the change and overrule states of all sub-simons which effectively sets the same state to the whole subtree. If subtree is not overruled, some Simons (with their subtrees) may not be affected by this change.

Specified by:
setState in interface Simon
Parameters:
state - a new state.
overrule - specifies whether this change is forced to the whole subtree.
Throws:
IllegalArgumentException - if state is null.

isAnonymousOrRootSimon

private boolean isAnonymousOrRootSimon()

shouldBeEffectivlyEnabled

private boolean shouldBeEffectivlyEnabled()

updateAndPropagateEffectiveState

private void updateAndPropagateEffectiveState(boolean enabled,
                                              boolean overrule)

isEnabled

public boolean isEnabled()
Description copied from interface: Simon
Returns true, if the Simon is enabled or if the enabled state is inherited.

Specified by:
isEnabled in interface Simon
Returns:
true, if the Simon is effectively enabled

resetCommon

protected void resetCommon()
Saves the timestamp when the Simon was reset and calls Callback.onSimonReset(Simon). Called only from synchronized method Simon.reset().


getLastReset

public long getLastReset()
Description copied from interface: Simon
Returns ms timestamp of the last recent usage of the Simon.reset() method on the Simon. Returns 0 if reset was not called yet. This timestamp is useful for rate measuring when reset is called on a regular basis - likely via Simon.sampleAndReset(). While client code could store the timestamp too it is not necessary with this method.

Specified by:
getLastReset in interface Simon
Returns:
ms timestamp of the last reset or 0 if reset was not called yet

getState

public final SimonState getState()
Description copied from interface: Simon
Returns state of the Simon that can be enabled, disabled or ihnerited.

Specified by:
getState in interface Simon
Returns:
state of the Simon

getNote

public String getNote()
Description copied from interface: Simon
Returns note for the Simon. Note enables Simon with an additional information in human readable form.

Specified by:
getNote in interface Simon
Returns:
note for the Simon.

setNote

public void setNote(String note)
Description copied from interface: Simon
Sets note for the Simon. Note enables Simon with an additional information in human readable form.

Specified by:
setNote in interface Simon
Parameters:
note - note for the Simon.

getFirstUsage

public long getFirstUsage()
Description copied from interface: Simon
Returns ms timestamp of the first usage of this Simon. First and last usage are updated when monitor performs the measuring (start/stop/count/etc). They are not updated when values are obtained from the monitor.

Specified by:
getFirstUsage in interface Simon
Returns:
ms timestamp of the first usage

getLastUsage

public long getLastUsage()
Description copied from interface: Simon
Returns ms timestamp of the last usage of this Simon. First and last usage are updated when monitor performs the measuring (start/stop/count/etc). They are not updated when values are obtained from the monitor.

Specified by:
getLastUsage in interface Simon
Returns:
ms timestamp of the last usage

replaceChild

void replaceChild(Simon simon,
                  AbstractSimon newSimon)
Replaces one of the children for a new one (unknown to concrete). Used only internally.

Parameters:
simon - original Simon (unknown)
newSimon - new Simon

setAttribute

public void setAttribute(String name,
                         Object value)
Stores an attribute in this Simon. Attributes can be used to store any custom objects.

Specified by:
setAttribute in interface HasAttributes
Parameters:
name - a String specifying the name of the attribute
value - the Object to be stored
Since:
2.3

getAttribute

public Object getAttribute(String name)
Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.

Specified by:
getAttribute in interface HasAttributes
Parameters:
name - a String specifying the name of the attribute
Returns:
an Object containing the value of the attribute, or null if the attribute does not exist
Since:
2.3

getAttribute

public <T> T getAttribute(String name,
                          Class<T> clazz)
Returns the value of the named attribute typed to the specified class, or null if no attribute of the given name exists.

Specified by:
getAttribute in interface HasAttributes
Parameters:
name - a String specifying the name of the attribute
Returns:
the value of the attribute typed to T, or null if the attribute does not exist
Since:
3.4

removeAttribute

public void removeAttribute(String name)
Removes an attribute from this Simon.

Specified by:
removeAttribute in interface HasAttributes
Parameters:
name - a String specifying the name of the attribute to remove
Since:
2.3

getAttributeNames

public Iterator<String> getAttributeNames()
Returns an Iterator containing the names of the attributes available to this Simon. This method returns an empty Iterator if the Simon has no attributes available to it.

Specified by:
getAttributeNames in interface HasAttributes
Returns:
an Iterator of strings containing the names of the Simon's attributes
Since:
2.3

getCopyAsSortedMap

public Map<String,Object> getCopyAsSortedMap()
Returns copy of attributes as a sorted map, this can be used further for operations like toString.

Specified by:
getCopyAsSortedMap in interface HasAttributes
Returns:
copy of attributes as a sorted map
Since:
3.4

sampleCommon

void sampleCommon(Sample sample)

toString

public String toString()
Returns name and state of the Simon as a human readable string.

Overrides:
toString in class Object
Returns:
name and state of the Simon


Copyright © 2013. All Rights Reserved.