blob: 3b411bb437ad9160fab8bb968d75dc35b5f8089c [file] [log] [blame]
/*
* Copyright (c) 2013 DataTorrent, Inc. ALL Rights Reserved.
*
* Licensed 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.api.annotation.InputPortFieldAnnotation;
import com.datatorrent.api.annotation.OutputPortFieldAnnotation;
import com.datatorrent.lib.util.BaseNumberValueOperator;
/**
*<p>
* This operator implements Unifier interface and emits maximum of all values sub-classed from Number at end of window in the incoming stream. <br>
* <b>StateFull :</b> Yes, max value is determined during application window, can be more than 1. <br>
* <b>Partitions : </b>Yes, operator itself is used as unifier at output port. <br>
* <br>
* <b>Ports</b>:<br>
* <b>data</b>: expects V extends Number<br>
* <b>max</b>: emits V extends Number<br>
* <br>
* @displayName Maximum
* @category Math
* @tags maximum, numeric
* @since 0.3.2
*/
public class Max<V extends Number> extends BaseNumberValueOperator<V> implements Unifier<V>
{
/**
* Input port that takes a number and compares to max and stores the new max.
*/
public final transient DefaultInputPort<V> data = new DefaultInputPort<V>()
{
/**
* Each tuple is compared to the max and a new max (if so) is stored.
*/
@Override
public void process(V tuple)
{
Max.this.process(tuple);
}
};
/**
* Unifier process function.
*/
@Override
public void process(V tuple)
{
if (!flag) {
high = tuple;
flag = true;
}
else if (high.doubleValue() < tuple.doubleValue()) {
high = tuple;
}
}
/**
* Max value output port.
*/
public final transient DefaultOutputPort<V> max = new DefaultOutputPort<V>()
{
@Override
public Unifier<V> getUnifier()
{
return Max.this;
}
};
protected V high;
protected boolean flag = false;
/**
* Node only works in windowed mode. Emits the max. Override getValue if tuple type is mutable
* Clears internal data
*/
@Override
public void endWindow()
{
if (flag) {
max.emit(high);
}
flag = false;
high = null;
}
}