com.datatorrent.lib.math.MultiplyByConstant 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.annotation.Stateless;
import com.datatorrent.common.util.BaseOperator;
/**
* Multiplies input tuple (Number) by the value of property "multiplier" and emits the result on respective ports.
*
* This operator emits the result as Long on port "longProduct", as Integer on port "integerProduct", as Double on port "doubleProduct", and as Float on port "floatProduct".
* Output is computed in current window.No state dependency among input tuples
* This is a pass through operator
*
* StateFull : No , output is computed in current window.
* Partitions : Yes , No state dependency among input tuples.
*
* Ports:
* input: expects Number
* longProduct: emits Long
* integerProduct: emits Integer
* doubleProduct: emits Double
* floatProduct: emits Float
*
* Properties:
* multiplier: Number to multiply input tuple with
*
* @displayName Multiply By Constant
* @category Math
* @tags multiplication, constant, numeric
* @since 0.3.2
*/
@Stateless
public class MultiplyByConstant extends BaseOperator
{
/**
* Input number port.
*/
public final transient DefaultInputPort input = new DefaultInputPort()
{
@Override
public void process(Number tuple)
{
Long lProduct = null;
if (longProduct.isConnected()) {
longProduct.emit(lProduct = multiplier.longValue() * tuple.longValue());
}
if (integerProduct.isConnected()) {
integerProduct.emit(lProduct == null ? (int)(multiplier.longValue() * tuple.longValue()) : lProduct.intValue());
}
Double dProduct = null;
if (doubleProduct.isConnected()) {
doubleProduct.emit(dProduct = multiplier.doubleValue() * tuple.doubleValue());
}
if (floatProduct.isConnected()) {
floatProduct.emit(dProduct == null ? (float)(multiplier.doubleValue() * tuple.doubleValue()) : dProduct.floatValue());
}
}
};
/**
* Long output port.
*/
public final transient DefaultOutputPort longProduct = new DefaultOutputPort();
/**
* Integer output port.
*/
public final transient DefaultOutputPort integerProduct = new DefaultOutputPort();
/**
* Double output port.
*/
public final transient DefaultOutputPort doubleProduct = new DefaultOutputPort();
/**
* Float output port.
*/
public final transient DefaultOutputPort floatProduct = new DefaultOutputPort();
/**
* @param multiplier the multiplier to set
*/
public void setMultiplier(Number multiplier)
{
this.multiplier = multiplier;
}
/**
*
*/
public Number getMultiplier()
{
return multiplier;
}
private Number multiplier;
}