Code Snippet: A common duplet class

Java doesn’t allow multi-value method returning. To return 2 values for a method, you can create a "pair" object which contains two values.

/**
 * a pair of objects. like other containers, you'd rather return an empty
 * container than a null one
 * 
 * 
 *
 */
public class MyDuplet<L,R> {
	public L left;
	public R right;

	public static  MyDuplet<L,R> newInstance(L left, R right) {
		MyDuplet<L,R> instance = new MyDuplet<L,R>();
		instance.left = left;
		instance.right = right;
		return instance;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.SHORT_PREFIX_STYLE);
	}
}

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.