001/* 002 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved. 003 * 004 * Project and contact information: http://www.cascading.org/ 005 * 006 * This file is part of the Cascading project. 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020 021package cascading.tuple.io; 022 023import java.util.Arrays; 024import java.util.List; 025 026import cascading.tuple.Tuple; 027import cascading.tuple.util.Resettable2; 028 029/** 030 * Class TuplePair is a utility class that is optimized to hold two Tuple instances for sorting and hashing of each 031 * part independently. 032 */ 033public class TuplePair extends Tuple implements Resettable2<Tuple, Tuple> 034 { 035 /** Field tuples */ 036 private final Tuple[] tuples = new Tuple[ 2 ]; 037 038 /** 039 * Returns a reference to the private tuples of the given TuplePark 040 * 041 * @param tuplePair of type Tuple[] 042 * @return Tuple[] 043 */ 044 public static Tuple[] tuples( TuplePair tuplePair ) 045 { 046 return tuplePair.tuples; 047 } 048 049 /** Constructor Tuple creates a new Tuple instance. */ 050 public TuplePair() 051 { 052 super( (List<Object>) null ); // bypass unnecessary List creation 053 tuples[ 0 ] = new Tuple(); 054 tuples[ 1 ] = new Tuple(); 055 } 056 057 /** 058 * Constructor TuplePair creates a new TuplePair instance. 059 * 060 * @param lhs of type Tuple 061 * @param rhs of type Tuple 062 */ 063 public TuplePair( Tuple lhs, Tuple rhs ) 064 { 065 super( (List<Object>) null ); // bypass unnecessary List creation 066 tuples[ 0 ] = lhs; 067 tuples[ 1 ] = rhs; 068 069 if( lhs == null ) 070 throw new IllegalArgumentException( "lhs may not be null" ); 071 072 if( rhs == null ) 073 throw new IllegalArgumentException( "rhs may not be null" ); 074 } 075 076 /** 077 * Method getLhs returns the lhs of this TuplePair object. 078 * 079 * @return the lhs (type Tuple) of this TuplePair object. 080 */ 081 public Tuple getLhs() 082 { 083 return tuples[ 0 ]; 084 } 085 086 /** 087 * Method getRhs returns the rhs of this TuplePair object. 088 * 089 * @return the rhs (type Tuple) of this TuplePair object. 090 */ 091 public Tuple getRhs() 092 { 093 return tuples[ 1 ]; 094 } 095 096 @Override 097 public void reset( Tuple lhs, Tuple rhs ) 098 { 099 tuples[ 0 ] = lhs; 100 tuples[ 1 ] = rhs; 101 } 102 103 @Override 104 public boolean equals( Object object ) 105 { 106 if( this == object ) 107 return true; 108 if( object == null || getClass() != object.getClass() ) 109 return false; 110 111 TuplePair tuplePair = (TuplePair) object; 112 113 if( !Arrays.equals( tuples, tuplePair.tuples ) ) 114 return false; 115 116 return true; 117 } 118 119 @Override 120 public int hashCode() 121 { 122 return Arrays.hashCode( tuples ); 123 } 124 125 @Override 126 public int compareTo( Object other ) 127 { 128 if( other instanceof TuplePair ) 129 return compareTo( (TuplePair) other ); 130 else 131 return -1; 132 } 133 134 @Override 135 public int compareTo( Tuple other ) 136 { 137 if( other instanceof TuplePair ) 138 return compareTo( (TuplePair) other ); 139 else 140 return -1; 141 } 142 143 /** 144 * Method compareTo compares this instance to the given TuplePair. 145 * 146 * @param tuplePair of type TuplePair 147 * @return int 148 */ 149 public int compareTo( TuplePair tuplePair ) 150 { 151 int c = tuples[ 0 ].compareTo( tuplePair.tuples[ 0 ] ); 152 153 if( c != 0 ) 154 return c; 155 156 c = tuples[ 1 ].compareTo( tuplePair.tuples[ 1 ] ); 157 158 return c; 159 } 160 161 @Override 162 public String toString() 163 { 164 return tuples[ 0 ].print() + tuples[ 1 ].print(); 165 } 166 167 @Override 168 public String print() 169 { 170 return tuples[ 0 ].print() + tuples[ 1 ].print(); 171 } 172 }