All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datatorrent.lib.math.Max 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 maximum of all values sub-classed from Number at end of window in the incoming stream.
* StateFull : Yes, max value is determined during application window, can be more than 1.
* Partitions : Yes, operator itself is used as unifier at output port.
*
* Ports:
* data: expects V extends Number
* max: emits V extends Number
*
* @displayName Maximum * @category Math * @tags maximum, numeric * @since 0.3.2 */ public class Max extends BaseNumberValueOperator implements Unifier { /** * Input port that takes a number and compares to max and stores the new max. */ public final transient DefaultInputPort data = new DefaultInputPort() { /** * 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 max = new DefaultOutputPort() { @Override public Unifier 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; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy