com.datatorrent.lib.math.Min 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.api.Operator.Unifier;
import com.datatorrent.lib.util.BaseNumberValueOperator;
/**
* This operator implements Unifier interface and emits at end of window minimum of all values sub-classed from Number in the incoming stream.
*
* StateFull :Yes, min value is computed over application windows.
* Partitions :Yes, operator is kin unifier operator.
*
* Ports:
* data: expects V extends Number
* min: emits V extends Number
*
*
* @displayName Minimum
* @category Math
* @tags minimum, numeric
* @since 0.3.2
*/
public class Min extends BaseNumberValueOperator implements Unifier
{
/**
* Computed low value.
*/
protected V low;
// transient field
protected boolean flag = false;
/**
* Input port that takes a number and compares to min and stores the new min.
*/
public final transient DefaultInputPort data = new DefaultInputPort()
{
/**
* Each tuple is compared to the min and a new min (if so) is stored.
*/
@Override
public void process(V tuple)
{
Min.this.process(tuple);
}
};
/**
* Unifier process function.
*/
@Override
public void process(V tuple)
{
if (!flag) {
low = tuple;
flag = true;
} else if (low.doubleValue() > tuple.doubleValue()) {
low = tuple;
}
}
/**
* Min output port.
*/
public final transient DefaultOutputPort min = new DefaultOutputPort()
{
@Override
public Unifier getUnifier()
{
return Min.this;
}
};
/**
* Emits the max. Override getValue if tuple type is mutable.
* Clears internal data. Node only works in windowed mode.
*/
@Override
public void endWindow()
{
if (flag) {
min.emit(low);
}
flag = false;
low = null;
}
}