gov.sandia.cognition.util.DefaultTriple Maven / Gradle / Ivy
/*
* File: DefaultTriple.java
* Authors: Justin Basilico
* Company: Sandia National Laboratories
* Project: Cognitive Foundry
*
* Copyright September 26, 2008, 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.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* The {@code DefaultTriple} class implements a simple structure for a triple
* of three objects, potentially of different types.
*
* @param The type of the first object in the triple.
* @param The type of second object in the triple.
* @param The type of the third object in the triple.
* @author Justin Basilico
* @since 2.1
*/
public class DefaultTriple
extends AbstractCloneableSerializable
implements Triple
{
/** The first object. */
protected FirstType first;
/** The second object. */
protected SecondType second;
/** The third type. */
protected ThirdType third;
/**
* Creates a new instance of {@code DefaultTriple} with initial null values.
*/
public DefaultTriple()
{
this(null, null, null);
}
/**
* Creates a new instance of {@code DefaultTriple}.
*
* @param first The first object.
* @param second The second object.
* @param third The third object.
*/
public DefaultTriple(
final FirstType first,
final SecondType second,
final ThirdType third)
{
super();
this.setFirst(first);
this.setSecond(second);
this.setThird(third);
}
@Override
public DefaultTriple clone()
{
@SuppressWarnings("unchecked")
final DefaultTriple result =
(DefaultTriple) super.clone();
result.first = ObjectUtil.cloneSmart(result.first);
result.second = ObjectUtil.cloneSmart(result.second);
result.third = ObjectUtil.cloneSmart(result.third);
return result;
}
public FirstType getFirst()
{
return this.first;
}
/**
* Sets the first object.
*
* @param first The new value for the first object.
*/
public void setFirst(
final FirstType first)
{
this.first = first;
}
public SecondType getSecond()
{
return this.second;
}
/**
* Sets the second object.
*
* @param second The new value for the second object.
*/
public void setSecond(
final SecondType second)
{
this.second = second;
}
public ThirdType getThird()
{
return this.third;
}
/**
* Sets the third object.
*
* @param third The new value for the third object.
*/
public void setThird(
final ThirdType third)
{
this.third = third;
}
/**
* Takes three collections of data of the same size and creates a new single
* {@code ArrayList} out of their elements.
*
* @param
* The type of the first element.
* @param
* The type of the second element.
* @param
* The type of the third element.
* @param firsts A collection of data to transform into the first
* element of the triple. Must have the same size as seconds and
* thirds.
* @param seconds A collection of data to transform into the second
* element of the triple. Must have the same size as firsts and
* thirds.
* @param thirds A collection of data to transform into the third
* element of the triple. Must have the same size as firsts and
* seconds.
* @return A new array list of triples of the same time as the first,
* second, and third collections.
*/
public static
ArrayList>
mergeCollections(
final Collection firsts,
final Collection seconds,
final Collection thirds)
{
// Ensure that the two collections are of the same size.
final int count = firsts.size();
if (count != seconds.size() || count != thirds.size())
{
throw new IllegalArgumentException(
"Collections are not the same size.");
}
// Create the result list.
final ArrayList> result
= new ArrayList>(
count);
final Iterator i2 = seconds.iterator();
final Iterator i3 = thirds.iterator();
for (FirstType d1 : firsts)
{
result.add(new DefaultTriple(
d1, i2.next(), i3.next()));
}
return result;
}
}