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

gov.sandia.cognition.learning.data.DefaultInputOutputPair Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
/*
 * File:                DefaultInputOutputPair.java
 * Authors:             Justin Basilico
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 * 
 * Copyright May 26, 2010, Sandia Corporation.
 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive 
 * license for use of this work by or on behalf of the U.S. Government. Export 
 * of this program may require a license from the United States Government. 
 * See CopyrightHistory.txt for complete details.
 * 
 */

package gov.sandia.cognition.learning.data;

import gov.sandia.cognition.util.Pair;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * A default implementation of the {@code InputOutputPair} interface. It stores
 * a pointer to the input and output objects that make up the pair.
 *
 * @param  
 *      The type for the input object in the pair.
 * @param  
 *      The type for the output object in the pair.
 *
 * @author Justin Basilico
 * @author Kevin R. Dixon
 * @since   3.0
 */
public class DefaultInputOutputPair
    extends AbstractInputOutputPair
{

    /** The input. */
    private InputType input;

    /** The output associated with the input. */
    private OutputType output;

    /**
     * Creates a new {@code DefaultInputOutputPair} with both the input and
     * output as null.
     */
    public DefaultInputOutputPair()
    {
        this(null, null);
    }

    /**
     * Creates a new {@code DefaultInputOutputPair} with the given input and
     * output.
     *
     * @param   input
     *      The input to store.
     * @param   output
     *      The output to store.
     */
    public DefaultInputOutputPair(
        final InputType input,
        final OutputType output)
    {
        super();

        this.setInput(input);
        this.setOutput(output);
    }

    /**
     * Creates a new {@code DefaultInputOutputPair} using the first element of
     * the given pair as the input and the second element of the given pair as
     * the output.
     *
     * @param   pair
     *      The pair to get the input and output from.
     */
    public DefaultInputOutputPair(
        final Pair pair)
    {
        this(pair.getFirst(), pair.getSecond());
    }

    public InputType getInput()
    {
        return this.input;
    }

    /**
     * Sets the input.
     *
     * @param input The new input.
     */
    public void setInput(
        final InputType input)
    {
        this.input = input;
    }

    public OutputType getOutput()
    {
        return this.output;
    }

    /**
     * Sets the output.
     *
     * @param output The new output.
     */
    public void setOutput(
        final OutputType output)
    {
        this.output = output;
    }

    /**
     * Convenience method to create a new {@code DefaultInputOutputPair}.
     *
     * @param  
     *      The type for the input object in the pair.
     * @param  
     *      The type for the output object in the pair.
     * @return
     *      A new default input-output pair.
     */
    public static  DefaultInputOutputPair
        create()
    {
        return new DefaultInputOutputPair();
    }

    /**
     * Convenience method to create a new {@code DefaultInputOutputPair}.
     *
     * @param  
     *      The type for the input object in the pair.
     * @param  
     *      The type for the output object in the pair.
     * @param input
     *      The input.
     * @param output
     *      The output.
     * @return
     *      A new default input-output pair with the given input and output.
     */
    public static  DefaultInputOutputPair
        create(
        final InputType input,
        final OutputType output)
    {
        return new DefaultInputOutputPair(input, output);
    }

    /**
     * Takes two collections of data of equal size and creates a single
     * ArrayList of InputOutputPairs out of them.
     *
     * @param   
     *      The type of the input.
     * @param   
     *      The type of the output.
     * @param   inputs
     *      A collection of the data to transform into the input element of the
     *      pair. Must have the same number of elements as outputs.
     * @param   outputs
     *      A collection of data to transform into the output element of the
     *      pair. Must have the same number of elements as inputs.
     * @return
     *      A new {@code ArrayList} of the same size as the
     *      two given collections where the input is from the first collection
     *      and output is from the second.
     */
    public static  ArrayList>
        mergeCollections(
        final Collection inputs,
        final Collection outputs)
    {
        final int count = inputs.size();
        if (count != outputs.size())
        {
            throw new IllegalArgumentException(
                "The inputs and outputs collections must be the same size ("
                    + inputs.size() + " != " + outputs.size() + ")");
        }

        final ArrayList> result =
            new ArrayList>(count);
        final Iterator outputsIterator = outputs.iterator();
        for (InputType input : inputs)
        {
            result.add(create(input, outputsIterator.next()));
        }

        return result;

    }

    /**
     * Takes a collection of input values and a single output value and creates
     * a new collection of default input output pairs with each of the given
     * inputs and the given output.
     *
     * @param   
     *      The type of the input.
     * @param   
     *      The type of the output.
     * @param   inputs
     *      A collection of the data to transform into the input element of the
     *      pair.
     * @param   output
     *      The value to use as the output element of the pair. (The label)
     * @return
     *      A new {@code ArrayList} of the same size as the
     *      input collection where the inputs are from the given collection and
     *      outputs are all the given value.
     */
    public static  ArrayList>
        labelCollection(
        final Collection inputs,
        final OutputType output)
    {
        final int count = inputs.size();

        final ArrayList> result =
            new ArrayList>(count);
        for (InputType input : inputs)
        {
            result.add(create(input, output));
        }

        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy