/**************************************************************************** * This software is part of the gCube Project. * Site: http://www.gcube-system.org/ **************************************************************************** * The gCube/gCore software is licensed as Free Open Source software * conveying to the EUPL (http://ec.europa.eu/idabc/eupl). * The software and documentation is provided by its authors/distributors * "as is" and no expressed or * implied warranty is given for its use, quality or fitness for a * particular case. **************************************************************************** * Filename: Tuple.java **************************************************************************** * @author Daniele Strollo ***************************************************************************/ package org.gcube.resourcemanagement.support.shared.types; import java.io.Serializable; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import com.google.gwt.user.client.rpc.IsSerializable; /** * General purpose tuple representation. * A tuple is a sequence (or ordered list) of finite length. * *
 * Example:
 *
 * 1) Creation
 * // single typed tuple
 * Tuple<Long> nt = new Tuple<Long>(42L);
 *
 * // multi typed tuple
 * Tuple<Object> ot = new Tuple<Object>("Lars Tackmann",
 *    		"Age", 26);
 *
 * 2) Usage
 * // get single element
 * Integer val = (Integer) ot.get(2);
 * // iterate tuple
 * for (Object o : ot)
 *   System.out.printf("'%s' ", o.toString());
 * // print all elems
 * System.out.printf("Object tuple: %s\n", ot.toString());
 *
 *
 * 3) Operations
 * // The elements of two tuples a and b can be joined with
 * // union operation that returns a new tuple.
 * Tuple c = a.union (b);
 * 
* */ public class Tuple implements Iterable, Serializable, IsSerializable { private static final long serialVersionUID = 5783359179069297888L; private List content = new LinkedList(); /** * @deprecated For serialization purpose use the other constructors */ public final List getContent() { return content; } /** * @deprecated For serialization purpose use the other constructors */ public final void setContent(final List content) { this.content = content; } /** * @deprecated For serialization purpose use the other constructors */ public Tuple() { super(); } public Tuple(final T... args) { for (T t : args) { content.add(t); } } /** * Appends elements inside a tuple. */ public final void append(final T... args) { if (content != null) { for (T t : args) { content.add(t); } } } @SuppressWarnings({ "unchecked", "deprecation" }) public final Tuple union(final Tuple t) { Tuple retval = new Tuple(); for (T elem : content) { retval.append(elem); } for (int i = 0; i < t.size(); i++) { retval.append(t.get(i)); } return retval; } public final T get(final int index) { return content.get(index); } public final Iterator iterator() { return content.iterator(); } public final int size() { return content.size(); } /** * Compares two tuples. * The comparison is applied to all the contained elements. * * @param obj the {@link Tuple} element to compare * @return true if the number of contained elements is the same and * all the elements are equals. */ @SuppressWarnings("unchecked") public final boolean equals(final Object obj) { if (!(obj instanceof Tuple)) { return false; } Tuple tuple = (Tuple) obj; if (tuple.size() != this.content.size()) { return false; } Iterator internalElems = this.content.iterator(); for (T elem : tuple) { if (!elem.equals(internalElems.next())) { return false; } } return true; } @Override public final int hashCode() { int retval = 0; Iterator internalElems = this.content.iterator(); while (internalElems.hasNext()) { retval += internalElems.next().hashCode(); } return retval; } @Override public final String toString() { StringBuilder retval = new StringBuilder(); for (Object o : content) { retval.append(o.toString() + "/"); } return "(" + retval.substring(0, retval.length() - 1) + ")"; } }