com.datatorrent.lib.math.Range Maven / Gradle / Ivy
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.datatorrent.lib.math;
import com.datatorrent.api.DefaultInputPort;
import com.datatorrent.api.DefaultOutputPort;
import com.datatorrent.lib.util.BaseNumberValueOperator;
import com.datatorrent.lib.util.HighLow;
import com.datatorrent.lib.util.UnifierRange;
/**
* This operator emits the range of values at the end of window.
*
*
* StateFull : Yes, values are computed over application time window.
* Partitions : Yes , High/Low values are unified on output port.
*
* Ports:
* data: expects V extends Number
* range: emits HighLow<V>
*
*
* @displayName Range
* @category Math
* @tags range, numeric , comparison
* @since 0.3.3
*/
public class Range extends BaseNumberValueOperator
{
/**
* Highest value on input port.
*/
protected V high = null;
/**
* Lowest value on input port.
*/
protected V low = null;
/**
* Input data port.
*/
public final transient DefaultInputPort data = new DefaultInputPort()
{
/**
* Process each tuple to compute new high and low
*/
@Override
public void process(V tuple)
{
if ((low == null) || (low.doubleValue() > tuple.doubleValue())) {
low = tuple;
}
if ((high == null) || (high.doubleValue() < tuple.doubleValue())) {
high = tuple;
}
}
};
/**
* Output range port, which emits high low unifier operator.
*/
public final transient DefaultOutputPort> range = new DefaultOutputPort>()
{
@Override
public Unifier> getUnifier()
{
return new UnifierRange();
}
};
/**
* Emits the range. If no tuple was received in the window, no emit is done
* Clears the internal data before return
*/
@Override
public void endWindow()
{
if ((low != null) && (high != null)) {
HighLow tuple = new HighLow(getValue(high.doubleValue()),
getValue(low.doubleValue()));
range.emit(tuple);
}
high = null;
low = null;
}
}