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

com.datatorrent.lib.formatter.Formatter 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.formatter;

import com.datatorrent.api.Context;
import com.datatorrent.api.Context.PortContext;
import com.datatorrent.api.DefaultInputPort;
import com.datatorrent.api.DefaultOutputPort;
import com.datatorrent.api.annotation.InputPortFieldAnnotation;
import com.datatorrent.api.annotation.OutputPortFieldAnnotation;
import com.datatorrent.common.util.BaseOperator;
import com.datatorrent.lib.converter.Converter;

/**
 * Abstract class that implements Converter interface. This is a schema enabled
 * Formatter 
* Sub classes need to implement the convert method
* Port Interface
* in: expects <Object> this is a schema enabled port
* out: emits <OUTPUT>
* err: emits <Object> error port that emits input tuple that could * not be converted
*
* * @displayName Parser * @tags parser converter * @param * @since 3.2.0 */ @org.apache.hadoop.classification.InterfaceStability.Evolving public abstract class Formatter extends BaseOperator implements Converter { protected transient Class clazz; @OutputPortFieldAnnotation public transient DefaultOutputPort out = new DefaultOutputPort(); @OutputPortFieldAnnotation(optional = true) public transient DefaultOutputPort err = new DefaultOutputPort(); @InputPortFieldAnnotation(schemaRequired = true) public transient DefaultInputPort in = new DefaultInputPort() { public void setup(PortContext context) { clazz = context.getValue(Context.PortContext.TUPLE_CLASS); } @Override public void process(Object inputTuple) { OUTPUT tuple = convert(inputTuple); if (tuple == null && err.isConnected()) { err.emit(inputTuple); return; } if (out.isConnected()) { out.emit(tuple); } } }; /** * Get the class that needs to be formatted * * @return Class */ public Class getClazz() { return clazz; } /** * Set the class of tuple that needs to be formatted * * @param clazz */ public void setClazz(Class clazz) { this.clazz = clazz; } }