com.ondeck.datapipes.examples.SimpleFlowExample.java Source code

Java tutorial

Introduction

Here is the source code for com.ondeck.datapipes.examples.SimpleFlowExample.java

Source

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 OnDeck
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * Except as contained in this notice, the name of a copyright holder shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization of the copyright holder.
 */

package com.ondeck.datapipes.examples;

import java.util.List;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.reflect.TypeToken;
import com.ondeck.datapipes.flow.DataProviderStep;
import com.ondeck.datapipes.flow.FlowException;
import com.ondeck.datapipes.flow.FunctionStep;
import com.ondeck.datapipes.flow.SimpleExecuteVisitor;

/**
 * Created by abhijithrc on 3/13/15.
 */
public class SimpleFlowExample {
    public static void main(String[] args) throws FlowException {

        /**
         * A Dataprovider step is required to provide input to a Flow
         */
        DataProviderStep<String> dataProviderStep = new DataProviderStep<>(TypeToken.of(String.class));

        /**
         * Step one is a FunctionStep that takes a String argument and converts it into an Integer.
         * The parent of this step is the dataProviderStep.
         */
        FunctionStep<String, Integer> stepOne = new FunctionStep<>(new Function<String, Integer>() {
            @Override
            public Integer apply(String sourceValue) {
                List<Integer> list = Lists.newArrayList();
                list.add(Integer.valueOf(sourceValue));
                System.out.println("Step One : Input string is " + sourceValue + ". Converting to int.");
                return Integer.valueOf(sourceValue);
            }
        }, dataProviderStep, TypeToken.of(Integer.class));

        /**
         * Step two is a FunctionStep that takes the result of step one and multiplies it by two.
         */
        FunctionStep<Integer, Integer> stepTwo = new FunctionStep<>(new Function<Integer, Integer>() {
            @Override
            public Integer apply(Integer sourceValue) {
                System.out.println("Step Two : Multiplying input by 2");
                return sourceValue * 2;
            }
        }, stepOne, TypeToken.of(Integer.class));

        /**
         * Finally use an executor to execute the flow.
         */
        String[] inputs = new String[] { "1", "50", "12" };
        for (String i : inputs) {
            SimpleExecuteVisitor<Integer> executeVisitor = new SimpleExecuteVisitor<>();
            executeVisitor.addInput(new TypeToken<String>() {
            }, i);
            Integer result = executeVisitor.execute(stepTwo);
            System.out.println("Result is " + result);
            System.out.println();
        }
    }
}