com.datatorrent.lib.math.LogicalCompareToConstant 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;
/**
* This operator does a logical comparison of a constant with a tuple.
*
*
* @see If the constant is equal to tuple, then the pair is
* emitted on equalTo, greaterThanEqualTo, and lessThanEqualTo ports. If
* the constant is less than tuple, then the pair is emitted on notEqualTo,
* lessThan and lessThanEqualTo ports. If the constant is greater than
* tuple, then the pair is emitted on notEqualTo, greaterThan and
* greaterThanEqualTo ports. This is a pass through operator
*
*
* StateFull : No, comparison is done in current window.
* Partitions : Yes, no state dependency among input tuples.
*
* Ports:
* input: expects T
* equalTo: emits T
* notEqualTo: emits T
* greaterThanEqualTo: emits T
* greaterThan: emits T
* lessThanEqualTo: emits T
* lessThan: emits T
*
* @displayName Logical Compare To Constant
* @category Math
* @tags comparison, logical, key value, constant
* @since 0.3.3
*/
@Stateless
public class LogicalCompareToConstant> extends
BaseOperator
{
/**
* Compare constant, set by application.
*/
private T constant;
/**
* Input port that takes a comparable to compare it with a constant.
*/
public final transient DefaultInputPort input = new DefaultInputPort()
{
@Override
public void process(T tuple)
{
int i = constant.compareTo(tuple);
if (i > 0) {
greaterThan.emit(tuple);
greaterThanOrEqualTo.emit(tuple);
notEqualTo.emit(tuple);
} else if (i < 0) {
lessThan.emit(tuple);
lessThanOrEqualTo.emit(tuple);
notEqualTo.emit(tuple);
} else {
equalTo.emit(tuple);
lessThanOrEqualTo.emit(tuple);
greaterThanOrEqualTo.emit(tuple);
}
}
};
/**
* Equal output port.
*/
public final transient DefaultOutputPort equalTo = new DefaultOutputPort();
/**
* Not Equal output port.
*/
public final transient DefaultOutputPort notEqualTo = new DefaultOutputPort();
/**
* Less Than output port.
*/
public final transient DefaultOutputPort lessThan = new DefaultOutputPort();
/**
* Greater than output port.
*/
public final transient DefaultOutputPort greaterThan = new DefaultOutputPort();
public final transient DefaultOutputPort lessThanOrEqualTo = new DefaultOutputPort();
public final transient DefaultOutputPort greaterThanOrEqualTo = new DefaultOutputPort();
/**
* Set constant for comparison.
*
* @param constant
* the constant to set
*/
public void setConstant(T constant)
{
this.constant = constant;
}
/**
* returns the value of constant
*/
public T getConstant()
{
return constant;
}
}