001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.lang3.tuple; 018 019import java.io.Serializable; 020 021import org.apache.commons.lang3.ObjectUtils; 022import org.apache.commons.lang3.builder.CompareToBuilder; 023 024/** 025 * <p>A triple consisting of three elements.</p> 026 * 027 * <p>This class is an abstract implementation defining the basic API. 028 * It refers to the elements as 'left', 'middle' and 'right'.</p> 029 * 030 * <p>Subclass implementations may be mutable or immutable. 031 * However, there is no restriction on the type of the stored objects that may be stored. 032 * If mutable objects are stored in the triple, then the triple itself effectively becomes mutable.</p> 033 * 034 * @param <L> the left element type 035 * @param <M> the middle element type 036 * @param <R> the right element type 037 * 038 * @version $Id: Triple.java 1552635 2013-12-20 13:02:03Z britter $ 039 * @since 3.2 040 */ 041public abstract class Triple<L, M, R> implements Comparable<Triple<L, M, R>>, Serializable { 042 043 /** Serialization version */ 044 private static final long serialVersionUID = 1L; 045 046 /** 047 * <p>Obtains an immutable triple of from three objects inferring the generic types.</p> 048 * 049 * <p>This factory allows the triple to be created using inference to 050 * obtain the generic types.</p> 051 * 052 * @param <L> the left element type 053 * @param <M> the middle element type 054 * @param <R> the right element type 055 * @param left the left element, may be null 056 * @param middle the middle element, may be null 057 * @param right the right element, may be null 058 * @return a triple formed from the three parameters, not null 059 */ 060 public static <L, M, R> Triple<L, M, R> of(final L left, final M middle, final R right) { 061 return new ImmutableTriple<L, M, R>(left, middle, right); 062 } 063 064 //----------------------------------------------------------------------- 065 /** 066 * <p>Gets the left element from this triple.</p> 067 * 068 * @return the left element, may be null 069 */ 070 public abstract L getLeft(); 071 072 /** 073 * <p>Gets the middle element from this triple.</p> 074 * 075 * @return the middle element, may be null 076 */ 077 public abstract M getMiddle(); 078 079 /** 080 * <p>Gets the right element from this triple.</p> 081 * 082 * @return the right element, may be null 083 */ 084 public abstract R getRight(); 085 086 //----------------------------------------------------------------------- 087 /** 088 * <p>Compares the triple based on the left element, followed by the middle element, 089 * finally the right element. 090 * The types must be {@code Comparable}.</p> 091 * 092 * @param other the other triple, not null 093 * @return negative if this is less, zero if equal, positive if greater 094 */ 095 @Override 096 public int compareTo(final Triple<L, M, R> other) { 097 return new CompareToBuilder().append(getLeft(), other.getLeft()) 098 .append(getMiddle(), other.getMiddle()) 099 .append(getRight(), other.getRight()).toComparison(); 100 } 101 102 /** 103 * <p>Compares this triple to another based on the three elements.</p> 104 * 105 * @param obj the object to compare to, null returns false 106 * @return true if the elements of the triple are equal 107 */ 108 @Override 109 public boolean equals(final Object obj) { 110 if (obj == this) { 111 return true; 112 } 113 if (obj instanceof Triple<?, ?, ?>) { 114 final Triple<?, ?, ?> other = (Triple<?, ?, ?>) obj; 115 return ObjectUtils.equals(getLeft(), other.getLeft()) 116 && ObjectUtils.equals(getMiddle(), other.getMiddle()) 117 && ObjectUtils.equals(getRight(), other.getRight()); 118 } 119 return false; 120 } 121 122 /** 123 * <p>Returns a suitable hash code.</p> 124 * 125 * @return the hash code 126 */ 127 @Override 128 public int hashCode() { 129 return (getLeft() == null ? 0 : getLeft().hashCode()) ^ 130 (getMiddle() == null ? 0 : getMiddle().hashCode()) ^ 131 (getRight() == null ? 0 : getRight().hashCode()); 132 } 133 134 /** 135 * <p>Returns a String representation of this triple using the format {@code ($left,$middle,$right)}.</p> 136 * 137 * @return a string describing this object, not null 138 */ 139 @Override 140 public String toString() { 141 return new StringBuilder().append('(').append(getLeft()).append(',').append(getMiddle()).append(',') 142 .append(getRight()).append(')').toString(); 143 } 144 145 /** 146 * <p>Formats the receiver using the given format.</p> 147 * 148 * <p>This uses {@link java.util.Formattable} to perform the formatting. Three variables may 149 * be used to embed the left and right elements. Use {@code %1$s} for the left 150 * element, {@code %2$s} for the middle and {@code %3$s} for the right element. 151 * The default format used by {@code toString()} is {@code (%1$s,%2$s,%3$s)}.</p> 152 * 153 * @param format the format string, optionally containing {@code %1$s}, {@code %2$s} and {@code %3$s}, not null 154 * @return the formatted string, not null 155 */ 156 public String toString(final String format) { 157 return String.format(format, getLeft(), getMiddle(), getRight()); 158 } 159 160} 161