001package Torello.Java.Additional; 002 003 004/** 005 * This simple generic-class allows a function to return <B STYLE='color:red'>two</B> objects as a 006 * result, instead of just one. This is not always so useful, and can make code confusing. 007 * However there are some instances where the only alternative would be to create an entirely new 008 * class/object, when only a single method result would use that object. 009 * 010 * <EMBED CLASS=globalDefs DATA-KIND=Ret DATA-N=2> 011 * <EMBED CLASS='external-html' DATA-FILE-ID=IMMUTABLE_RET> 012 * @param <A> The type of the <B STYLE='color:red'>first</B> member-field ('{@link #a}'). 013 * @param <B> The type of the <B STYLE='color:red'>second</B> member-field ('{@link #b}'). 014 */ 015public class Ret2<A, B> 016 extends RetN 017 implements java.io.Serializable, Cloneable 018{ 019 /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID> */ 020 protected static final long serialVersionUID = 1; 021 022 /** This holds a pointer the first response object. */ 023 public final A a; 024 025 /** This holds a pointer to the second response object. */ 026 public final B b; 027 028 /** Constructs this object */ 029 public Ret2(A a, B b) 030 { 031 this.a = a; 032 this.b = b; 033 } 034 035 /** 036 * Returns {@code '2'}, indicating how many fields are declared by this class. 037 * @return As an instance of {@code Ret2}, this method returns {@code '2'}; 038 */ 039 public int n() { return 2; } 040 041 042 // Super-class uses this for toString, equals, and hashCode 043 // There is an optimization, so if this is requested multiple times, it is saved in a 044 // transient field. 045 046 final Object[] asArrayInternal() 047 { return new Object[] { a, b }; } 048 049 public Ret2<A, B> clone() 050 { return new Ret2<>(this.a, this.b); } 051 052 /** 053 * <EMBED CLASS=defs DATA-TEXT="Integer"> 054 * <EMBED CLASS='external-html' DATA-FILE-ID=MULTI_TYPE_EX_COMMON> 055 * <EMBED CLASS='external-html' DATA-FILE-ID=MULTI_TYPE_GET_EXAMPLE> 056 */ 057 public Object get(final int i) 058 { 059 // Throws Exception if i not 1 or 2 060 CHECK_GET(i); 061 return (i == 1) ? a : b; 062 } 063 064 public Tuple2<A, B> toModifiable() 065 { return new Tuple2<>(this.a, this.b); } 066}