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

org.openimaj.math.util.IndependentPair Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2011, The University of Southampton and the individual contributors.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *   *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *
 *   *  Redistributions in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 *
 *   *  Neither the name of the University of Southampton nor the names of its
 *  contributors may be used to endorse or promote products derived from this
 *  software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.openimaj.math.util;

import java.util.ArrayList;
import java.util.List;

/**
 * {@code IndependentPair} represents a generic pair of objects of different
 * (independent) types.
 * 
 * @author Jonathon Hare
 * 
 * @param 
 *            the class of the first object in the pair
 * @param 
 *            the class of the second object in the pair
 */
public class IndependentPair {

    private final A o1;
    private final B o2;

    /**
     * Constructs a Pair object with two objects obj1 and obj2
     * 
     * @param obj1
     *            first object in pair
     * @param obj2
     *            second object in pair
     */
    public IndependentPair(A obj1, B obj2)
    {
        this.o1 = obj1;
        this.o2 = obj2;
    }

    /**
     * @return first object in pair
     */
    public A firstObject()
    {
        return o1;
    }

    /**
     * @return second object in pair
     */
    public B secondObject()
    {
        return o2;
    }

    /**
     * @return first object in pair
     */
    public A getFirstObject()
    {
        return o1;
    }

    /**
     * @return second object in pair
     */
    public B getSecondObject()
    {
        return o2;
    }

    @Override
    public String toString() {
        return "[" + this.o1 + "," + this.o2 + "]";
    }

    @Override
    public boolean equals(Object thatObject) {
        if (!(thatObject instanceof IndependentPair)) {
            return false;
        }
        @SuppressWarnings("rawtypes")
        IndependentPair that = (IndependentPair) thatObject;
        return this.o1 == that.o1 && this.o2 == that.o2;
    }

    /**
     * Create a pair from the given objects.
     * 
     * @param 
     *            Type of first object.
     * @param 
     *            Type of second object.
     * @param t
     *            The first object.
     * @param q
     *            The second object.
     * @return The pair.
     */
    public static  IndependentPair pair(T t, Q q) {
        return new IndependentPair(t, q);
    }

    /**
     * Extract the first objects from a list of pairs.
     * 
     * @param 
     *            type of first object
     * @param 
     *            type of second object
     * @param data
     *            the data
     * @return extracted first objects
     */
    public static  List getFirst(Iterable> data) {
        ArrayList extracted = new ArrayList();

        for (IndependentPair item : data) {
            extracted.add(item.o1);
        }

        return extracted;
    }

    /**
     * Extract the second objects from a list of pairs.
     * 
     * @param 
     *            type of first object
     * @param 
     *            type of second object
     * @param data
     *            the data
     * @return extracted second objects
     */
    public static  List getSecond(Iterable> data) {
        ArrayList extracted = new ArrayList();

        for (IndependentPair item : data) {
            extracted.add(item.o2);
        }

        return extracted;
    }

    /**
     * Get the function that returns the first object from the pair
     * 
     * @return the function that returns the first object from the pair
     */
    public static  MathFunction, T> getFirstFunction() {
        return new MathFunction, T>() {
            @Override
            public T apply(IndependentPair in) {
                return in.o1;
            }
        };
    }

    /**
     * Get the function that returns the second object from the pair
     * 
     * @return the function that returns the second object from the pair
     */
    public static  MathFunction, Q> getSecondFunction() {
        return new MathFunction, Q>() {
            @Override
            public Q apply(IndependentPair in) {
                return in.o2;
            }
        };
    }

    /**
     * Create a pair list from the given objects.
     * 
     * @param 
     *            Type of objects.
     * @param t
     *            The list of first objects.
     * @param q
     *            The list of second objects.
     * @return The list of pairs.
     */
    public static  List> pairList(List t, List q) {
        ArrayList> list = new ArrayList>(t.size());

        for (int i = 0; i < t.size(); i++) {
            list.add(new IndependentPair(t.get(i), q.get(i)));
        }

        return list;
    }

    /**
     * Create a new {@link IndependentPair} from this one with the elements
     * swapped
     * 
     * @return the swapped pair
     */
    public IndependentPair swap() {
        return new IndependentPair(o2, o1);
    }

    /**
     * Swap the order of the pairs
     * 
     * @param data
     *            the input
     * @return the swapped data
     */
    public static  List> swapList(
            List> data)
    {
        ArrayList> list = new ArrayList>(
                data.size());

        for (int i = 0; i < data.size(); i++) {
            list.add(data.get(i).swap());
        }

        return list;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy