/**************************************************************************************
* Copyright (C) 2008 EsperTech, Inc. All rights reserved. *
* http://esper.codehaus.org *
* http://www.espertech.com *
* ---------------------------------------------------------------------------------- *
* The software in this package is published under the terms of the GPL license *
* a copy of which has been included with this distribution in the license.txt file. *
**************************************************************************************/
package com.espertech.esper.view.window;
import com.espertech.esper.client.EventType;
import com.espertech.esper.core.context.util.AgentInstanceViewFactoryChainContext;
import com.espertech.esper.core.service.StatementContext;
import com.espertech.esper.epl.expression.ExprNode;
import com.espertech.esper.util.JavaClassHelper;
import com.espertech.esper.view.*;
import java.util.List;
/**
* Factory for {@link com.espertech.esper.view.window.TimeBatchView}.
*/
public class LengthBatchViewFactory implements DataWindowViewFactory, DataWindowViewWithPrevious, DataWindowBatchingViewFactory
{
/**
* The length window size.
*/
protected int size;
private EventType eventType;
public void setViewParameters(ViewFactoryContext viewFactoryContext, List<ExprNode> expressionParameters) throws ViewParameterException
{
List<Object> viewParameters = ViewFactorySupport.validateAndEvaluate("Length window view", viewFactoryContext.getStatementContext(), expressionParameters);
String errorMessage = "Length window view requires a single integer-type parameter";
if (viewParameters.size() != 1)
{
throw new ViewParameterException(errorMessage);
}
Object parameter = viewParameters.get(0);
if (!(parameter instanceof Number))
{
throw new ViewParameterException(errorMessage);
}
Number numParam = (Number) parameter;
if ( (JavaClassHelper.isFloatingPointNumber(numParam)) ||
(numParam instanceof Long))
{
throw new ViewParameterException(errorMessage);
}
size = numParam.intValue();
if (size <= 0)
{
throw new ViewParameterException("Length window requires a positive number");
}
}
public void attach(EventType parentEventType, StatementContext statementContext, ViewFactory optionalParentFactory, List<ViewFactory> parentViewFactories) throws ViewParameterException
{
this.eventType = parentEventType;
}
public Object makePreviousGetter() {
return new RelativeAccessByEventNIndexMap();
}
public View makeView(AgentInstanceViewFactoryChainContext agentInstanceViewFactoryContext)
{
IStreamRelativeAccess relativeAccessByEvent = ViewServiceHelper.getOptPreviousExprRelativeAccess(agentInstanceViewFactoryContext);
if (agentInstanceViewFactoryContext.isRemoveStream())
{
return new LengthBatchViewRStream(agentInstanceViewFactoryContext, this, size);
}
else
{
return new LengthBatchView(agentInstanceViewFactoryContext, this, size, relativeAccessByEvent);
}
}
public EventType getEventType()
{
return eventType;
}
public boolean canReuse(View view)
{
if (!(view instanceof LengthBatchView))
{
return false;
}
LengthBatchView myView = (LengthBatchView) view;
if (myView.getSize() != size)
{
return false;
}
return myView.isEmpty();
}
}