com.espertech.esper.view.stat
Class WeightedAverageView

java.lang.Object
  extended by com.espertech.esper.view.ViewSupport
      extended by com.espertech.esper.view.stat.WeightedAverageView
All Implemented Interfaces:
CloneableView, DerivedValueView, EventCollection, GroupableView, View, Viewable, java.lang.Iterable<EventBean>

public class WeightedAverageView
extends ViewSupport
implements CloneableView, DerivedValueView

View for computing a weighted average. The view uses 2 fields within the parent view to compute the weighted average. The X field and weight field. In a price-volume example it calculates the volume-weighted average price as (sum(price * volume) / sum(volume)). Example: weighted_avg("price", "volume")


Field Summary
protected  double currentValue
           
protected  java.lang.Object[] lastValuesEventNew
           
protected  double sumW
           
protected  double sumXtimesW
           
 
Fields inherited from class com.espertech.esper.view.ViewSupport
EMPTY_VIEW_ARRAY, parent
 
Constructor Summary
WeightedAverageView(WeightedAverageViewFactory viewFactory, AgentInstanceViewFactoryChainContext agentInstanceContext)
          Constructor requires the name of the field to use in the parent view to compute the weighted average on, as well as the name of the field in the parent view to get the weight from.
 
Method Summary
 View cloneView()
          Duplicates the view.
static EventType createEventType(StatementContext statementContext, StatViewAdditionalProps additionalProps, int streamNum)
          Creates the event type for this view.
 double getCurrentValue()
           
 EventType getEventType()
          Provides metadata information about the type of object the event collection contains.
 ExprNode getFieldNameWeight()
          Returns the expression supplying the weight values.
 ExprNode getFieldNameX()
          Returns the expression supplying the X values.
 java.lang.Object[] getLastValuesEventNew()
           
 double getSumW()
           
 double getSumXtimesW()
           
 ViewFactory getViewFactory()
           
 java.util.Iterator<EventBean> iterator()
          Allows iteration through all elements in this event collection.
 void setCurrentValue(double currentValue)
           
 void setLastValuesEventNew(java.lang.Object[] lastValuesEventNew)
           
 void setSumW(double sumW)
           
 void setSumXtimesW(double sumXtimesW)
           
 java.lang.String toString()
           
 void update(EventBean[] newData, EventBean[] oldData)
          Notify that data has been added or removed from the Viewable parent.
 
Methods inherited from class com.espertech.esper.view.ViewSupport
addView, addView, dumpChildViews, dumpUpdateParams, dumpUpdateParams, findDescendent, findViewIndex, getParent, getViews, hasViews, removeAllViews, removeView, removeView, setParent, updateChildren, updateChildren
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.espertech.esper.view.View
getParent, setParent
 
Methods inherited from interface com.espertech.esper.view.Viewable
addView, getViews, hasViews, removeAllViews, removeView
 

Field Detail

sumXtimesW

protected double sumXtimesW

sumW

protected double sumW

currentValue

protected double currentValue

lastValuesEventNew

protected java.lang.Object[] lastValuesEventNew
Constructor Detail

WeightedAverageView

public WeightedAverageView(WeightedAverageViewFactory viewFactory,
                           AgentInstanceViewFactoryChainContext agentInstanceContext)
Constructor requires the name of the field to use in the parent view to compute the weighted average on, as well as the name of the field in the parent view to get the weight from. compute the average for.

Method Detail

cloneView

public View cloneView()
Description copied from interface: CloneableView
Duplicates the view.

Expected to return a same view in initialized state for grouping.

Specified by:
cloneView in interface CloneableView

getFieldNameX

public final ExprNode getFieldNameX()
Returns the expression supplying the X values.

Returns:
expression supplying X data points

getFieldNameWeight

public final ExprNode getFieldNameWeight()
Returns the expression supplying the weight values.

Returns:
expression supplying weight

update

public void update(EventBean[] newData,
                   EventBean[] oldData)
Description copied from interface: View
Notify that data has been added or removed from the Viewable parent. The last object in the newData array of objects would be the newest object added to the parent view. The first object of the oldData array of objects would be the oldest object removed from the parent view. If the call to update contains new (inserted) data, then the first argument will be a non-empty list and the second will be empty. Similarly, if the call is a notification of deleted data, then the first argument will be empty and the second will be non-empty. Either the newData or oldData will be non-null. This method won't be called with both arguments being null, but either one could be null. The same is true for zero-length arrays. Either newData or oldData will be non-empty. If both are non-empty, then the update is a modification notification. When update() is called on a view by the parent object, the data in newData will be in the collection of the parent, and its data structures will be arranged to reflect that. The data in oldData will not be in the parent's data structures, and any access to the parent will indicate that that data is no longer there.

Specified by:
update in interface View
Parameters:
newData - is the new data that has been added to the parent view
oldData - is the old data that has been removed from the parent view

getEventType

public final EventType getEventType()
Description copied from interface: EventCollection
Provides metadata information about the type of object the event collection contains.

Specified by:
getEventType in interface EventCollection
Returns:
metadata for the objects in the collection

iterator

public final java.util.Iterator<EventBean> iterator()
Description copied from interface: EventCollection
Allows iteration through all elements in this event collection. The iterator will return the elements in the collection in their natural order, or, if there is no natural ordering, in some unpredictable order.

Specified by:
iterator in interface EventCollection
Specified by:
iterator in interface java.lang.Iterable<EventBean>
Returns:
an iterator which will go through all current elements in the collection.

toString

public final java.lang.String toString()
Overrides:
toString in class java.lang.Object

createEventType

public static EventType createEventType(StatementContext statementContext,
                                        StatViewAdditionalProps additionalProps,
                                        int streamNum)
Creates the event type for this view.

Parameters:
statementContext - is the event adapter service
Returns:
event type of view

getSumXtimesW

public double getSumXtimesW()

setSumXtimesW

public void setSumXtimesW(double sumXtimesW)

getSumW

public double getSumW()

setSumW

public void setSumW(double sumW)

getCurrentValue

public double getCurrentValue()

setCurrentValue

public void setCurrentValue(double currentValue)

getLastValuesEventNew

public java.lang.Object[] getLastValuesEventNew()

setLastValuesEventNew

public void setLastValuesEventNew(java.lang.Object[] lastValuesEventNew)

getViewFactory

public ViewFactory getViewFactory()
Specified by:
getViewFactory in interface GroupableView

© 2006-2015 EsperTech Inc.
All rights reserved.
Visit us at espertech.com