Click here to Skip to main content
15,891,372 members
Articles / Programming Languages / Java

Functional Java

Rate me:
Please Sign up or sign in to vote.
4.11/5 (6 votes)
7 Dec 2010CPOL8 min read 39.7K   284   14  
Functional programming with functors and object streams in Java.
package swensen.functional;

/**
 * A generic immutable tuple.
 * @author Stephen Swensen
 * @param <T1>
 * @param <T2>
 * @param <T3>
 * @param <T4>
 * @param <T5>
 * @param <T6>
 */
public class Tuple6<T1,T2,T3,T4,T5,T6> {
    public final T1 t1;
    public final T2 t2;
    public final T3 t3;
    public final T4 t4;
    public final T5 t5;
    public final T6 t6;
    public Tuple6(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6) {
        this.t1 = t1;
        this.t2 = t2;
        this.t3 = t3;
        this.t4 = t4;
        this.t5 = t5;
        this.t6 = t6;
    }
    public static <T1,T2,T3,T4,T5,T6> Tuple6<T1,T2,T3,T4,T5,T6> create(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6)
    {
        return new Tuple6<T1,T2,T3,T4,T5,T6>(t1,t2,t3,t4,t5,t6);
    }
    public String toString()
    {
        return String.format("(%s, %s, %s, %s, %s, %s)", t1, t2, t3, t4, t5, t6);
    }
    public boolean equals(Object other) {
        if (this == other)
            return true;
        if (!(other instanceof Tuple6<?,?,?,?,?,?>))
            return false;

        Tuple6<T1,T2,T3,T4,T5,T6> rhs = (Tuple6<T1,T2,T3,T4,T5,T6>) other;
        if (!(this.t1==null ? rhs.t1==null : this.t1.equals(rhs.t1)))
            return false;

        if (!(this.t2==null ? rhs.t2==null : this.t2.equals(rhs.t2)))
            return false;

        if (!(this.t3==null ? rhs.t3==null : this.t3.equals(rhs.t3)))
            return false;

        if (!(this.t4==null ? rhs.t4==null : this.t4.equals(rhs.t4)))
            return false;

        if (!(this.t5==null ? rhs.t5==null : this.t5.equals(rhs.t5)))
            return false;

        if (!(this.t6==null ? rhs.t6==null : this.t6.equals(rhs.t6)))
            return false;

        return true;
    }
    public int getHashCode() {
        int hash = 1;
        hash *= 31 + (t1 == null ? 0 : t1.hashCode());
        hash *= 31 + (t2 == null ? 0 : t2.hashCode());
        hash *= 31 + (t3 == null ? 0 : t3.hashCode());
        hash *= 31 + (t4 == null ? 0 : t4.hashCode());
        hash *= 31 + (t5 == null ? 0 : t5.hashCode());
        hash *= 31 + (t6 == null ? 0 : t6.hashCode());
        return hash;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I'm developing Unquote, a library for writing unit test assertions as F# quoted expressions: http://code.google.com/p/unquote/

I am working through Project Euler with F#: http://projecteulerfun.blogspot.com/

I participate in Stack Overflow: http://stackoverflow.com/users/236255/stephen-swensen

Comments and Discussions