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

com.merakianalytics.datapipelines.ChainTransform Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
package com.merakianalytics.datapipelines;

import java.util.List;

import com.merakianalytics.datapipelines.transformers.DataTransformer;

/**
 * A series of transformations using {@link com.merakianalytics.datapipelines.transformers.DataTransformer}s to convert between types
 *
 * @see com.merakianalytics.datapipelines.transformers.DataTransformer
 */
@SuppressWarnings("rawtypes") // Class specifier won't match the transform method signature. Have to pass it raw.
public class ChainTransform {
    public static  ChainTransform identity(final Class type) {
        return new ChainTransform<>(type, type, null, null);
    }

    private final List chain;
    private final int cost;
    private final Class from;
    private final Class to;
    private final List typeSteps;

    /**
     * @param from
     *        the type this {@link com.merakianalytics.datapipelines.ChainTransform} converts from
     * @param to
     *        the type this {@link com.merakianalytics.datapipelines.ChainTransform} converts to
     * @param typeSteps
     *        the incremental types that the data will be converted to along the way
     * @param chain
     *        the {@link com.merakianalytics.datapipelines.transformers.DataTransformer} chain which accomplishes this conversion
     */
    public ChainTransform(final Class from, final Class to, final List typeSteps, final List chain) {
        this.from = from;
        this.to = to;
        this.typeSteps = typeSteps;
        this.chain = chain;

        int cost = 0;
        if(chain != null) {
            for(final DataTransformer transformer : chain) {
                cost += transformer.cost();
            }
        }
        this.cost = cost;
    }

    /**
     * @return the total transform cost
     */
    public int cost() {
        return cost;
    }

    /**
     * @return the type this {@link com.merakianalytics.datapipelines.ChainTransform} converts from
     */
    public Class from() {
        return from;
    }

    /**
     * @return the type this {@link com.merakianalytics.datapipelines.ChainTransform} converts to
     */
    public Class to() {
        return to;
    }

    /**
     * Converts data using this {@link com.merakianalytics.datapipelines.transformers.DataTransformer} chain
     *
     * @param item
     *        the data to convert
     * @param context
     *        information about the context of the request such as what {@link com.merakianalytics.datapipelines.DataPipeline} called this method
     * @return the converted data
     */
    @SuppressWarnings("unchecked") // T == F when the chain is empty
    public T transform(final F item, final PipelineContext context) {
        if(chain == null || chain.isEmpty()) {
            return (T)item;
        }

        Object current = item;
        for(int i = 0; i < chain.size(); i++) {
            current = chain.get(i).transform(typeSteps.get(i), typeSteps.get(i + 1), current, context);
        }
        return (T)typeSteps.get(typeSteps.size() - 1).cast(current);
    }
}