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

com.healthmarketscience.common.util.Tuple3 Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
/*
Copyright (c) 2007 Health Market Science, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.healthmarketscience.common.util;

import java.io.Serializable;
import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * Simple class for holding 3 typed objects.
 *
 * @author James Ahlborn
 */
public class Tuple3
  extends Tuple2
  implements Cloneable, Serializable
{

  private static final long serialVersionUID = 20071230;
  
  /** 
   * Helper array to get direct indexed access to the tuple values.
   */
  static final Accessor[] ACCESSORS3 = {
    ACCESSORS2[0],
    ACCESSORS2[1],
    new Accessor() {
      @Override
      public Object get(Tuple1 tuple) {
        return ((Tuple3)tuple).get2();
      }
    }
  };
  
  /** 
   * The third object stored in this tuple.
   */
  private ObjType2 _obj2;
  
  /**
   * Initializes a new Tuple3 with null objects.
   */
  public Tuple3() {
    this(null, null, null);
  }

  /**
   * Initializes a new Tuple3 with the given objects. You may want to use
   * {@link #create} instead, since it infers the type parameters based on
   * its arguments.
   *
   * @param obj0 the first object to store.
   * @param obj1 the second object to store.
   * @param obj2 the third object to store.
   */
  public Tuple3(ObjType0 obj0, ObjType1 obj1, ObjType2 obj2) {
    super(obj0, obj1);
    _obj2 = obj2;
  }

  /**
   * Creates a new Tuple3 object with the provided objects.
   *
   * @param obj0 the first object to store.
   * @param obj1 the second object to store.
   * @param obj2 the third object to store.
   * @return a new Tuple3 with the given objects.
   */
  public static 
  Tuple3 create(
      InObjType0 obj0, InObjType1 obj1, InObjType2 obj2)
  {
    return new Tuple3(obj0, obj1, obj2);
  }

  /** 
   * Get an Iterable adapter that returns an Iterator that iterates over
   * element 2 of the Tuples within the given Iterable/Iterator. 
   * 
   * @return an Iterable adapter that returns an Iterator that iterates over 
   *         element 2 of the Tuples within the given Iterable/Iterator.
   */
  public static  Iterable iterable2(
      final Iterable> iable)
  {
    return new IterableIterator()
      {
        private final Iterator> _iter =
          iable.iterator();
        @Override
        public Iterator iterator() { return this; }
        public boolean hasNext() {
          return _iter.hasNext();
        }
        public InObjType2 next() {
          return _iter.next().get2();
        }
        public void remove() {
          _iter.remove();
        }
      };
  }

  /** 
   * Get a Collection adapter for element 2 of the Tuples of the given
   * Collection.  The returned Collection supports element removal if the given
   * Collection supports element removal, but does not support element 
   * addition. 
   *
   * @return a Collection adapter for element 2 of the Tuples of the given
   *         Collection.
   */
  public static  Collection collection2(
      final Collection> col)
  {
    return new AbstractCollection()
      {
        private final Collection> _col =col;
        @Override
        public Iterator iterator() {
          return iterable2(_col).iterator();
        }
        @Override
        public int size() { return _col.size(); }
      };
  }
  
  /**
   * Returns the third object of this tuple.
   *
   * @return the third object of this tuple.
   */
  public final ObjType2 get2() { return _obj2; }

  /**
   * Sets the third object of this tuple.
   *
   * @param obj the object to set as the third object of this tuple.
   */
  public final void set2(ObjType2 obj) { _obj2 = obj; }

  /** 
   * Get the objects of this tuple as an array.
   *
   * @return the objects of this tuple as an array.
   */
  @Override
  public Object[] get() { return new Object[]{get0(), get1(), get2()}; }
  
  /** 
   * Get the value of this tuple identified by index, which must always be
   * 0, 1 or 2, since this tuple class stores exactly three objects.
   *
   * @param index the index of the object to retrieve.  Must be 0, 1, or 2.
   * @return the object in this tuple with the given index.
   */
  @Override
  public Object get(int index) { return ACCESSORS3[index].get(this); }
  
  /**
   * Sets the values of this Tuple3.
   *
   * @param obj0 the object to use as the first object of this tuple.
   * @param obj1 the object to use as the second object of this tuple.
   * @param obj2 the object to use as the third object of this tuple.
   */
  public final void set(ObjType0 obj0, ObjType1 obj1, ObjType2 obj2) {
    set0(obj0);
    set1(obj1);
    set2(obj2);
  }

  /** 
   * Get the size of this tuple, which is always 3.
   *
   * @return the size of this tuple, which is always 3.
   */
  @Override
  public int size() { return 3; }
  
  /** 
   * Makes a copy of this Tuple3.
   * 
   * @return a copy of this Tuple3.
   */
  @SuppressWarnings({"unchecked","PMD.CloneThrowsCloneNotSupportedException"})
  @Override
  public Tuple3 clone() {
    return (Tuple3)super.clone();
  }
  
  /** 
   * Indicates whether the given Object is equal to this Tuple3.
   * @return 
   *  
  • {@code true} if the given Object refers to this Tuple3.
  • *
  • {@code true} if the given Object is an instance of Tuple3 for which * the contained objects are equal to this Tuple3's objects.
  • *
  • {@code false} otherwise.
  • */ @Override public boolean equals(Object o) { if(this == o) { return true; } if((o == null) || (o.getClass() != getClass())) { return false; } return new EqualsBuilder() .append(get0(), ((Tuple3)o).get0()) .append(get1(), ((Tuple3)o).get1()) .append(get2(), ((Tuple3)o).get2()) .isEquals(); } /** * Returns a hash code for this Tuple3, based on the objects it contains. * * @return a hash code for this Tuple3. */ @Override public int hashCode() { return new HashCodeBuilder() .append(get0()) .append(get1()) .append(get2()) .toHashCode(); } }




    © 2015 - 2025 Weber Informatics LLC | Privacy Policy