001 /* 002 * Copyright (c) 2007-2015 Concurrent, 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 021 package cascading.tuple.io; 022 023 import java.util.Arrays; 024 import java.util.List; 025 026 import cascading.tuple.Tuple; 027 028 /** 029 * Class TuplePair is a utility class that is optimized to hold two Tuple instances for sorting and hashing of each 030 * part independently. 031 */ 032 public class TuplePair extends Tuple 033 { 034 /** Field tuples */ 035 private final Tuple[] tuples = new Tuple[ 2 ]; 036 037 /** 038 * Returns a reference to the private tuples of the given TuplePark 039 * 040 * @param tuplePair of type Tuple[] 041 * @return Tuple[] 042 */ 043 static Tuple[] tuples( TuplePair tuplePair ) 044 { 045 return tuplePair.tuples; 046 } 047 048 /** Constructor Tuple creates a new Tuple instance. */ 049 public TuplePair() 050 { 051 super( (List<Object>) null ); // bypass unnecessary List creation 052 tuples[ 0 ] = new Tuple(); 053 tuples[ 1 ] = new Tuple(); 054 } 055 056 /** 057 * Constructor TuplePair creates a new TuplePair instance. 058 * 059 * @param lhs of type Tuple 060 * @param rhs of type Tuple 061 */ 062 public TuplePair( Tuple lhs, Tuple rhs ) 063 { 064 super( (List<Object>) null ); // bypass unnecessary List creation 065 tuples[ 0 ] = lhs; 066 tuples[ 1 ] = rhs; 067 068 if( lhs == null ) 069 throw new IllegalArgumentException( "lhs may not be null" ); 070 071 if( rhs == null ) 072 throw new IllegalArgumentException( "rhs may not be null" ); 073 } 074 075 /** 076 * Method getLhs returns the lhs of this TuplePair object. 077 * 078 * @return the lhs (type Tuple) of this TuplePair object. 079 */ 080 public Tuple getLhs() 081 { 082 return tuples[ 0 ]; 083 } 084 085 /** 086 * Method getRhs returns the rhs of this TuplePair object. 087 * 088 * @return the rhs (type Tuple) of this TuplePair object. 089 */ 090 public Tuple getRhs() 091 { 092 return tuples[ 1 ]; 093 } 094 095 @Override 096 public boolean equals( Object object ) 097 { 098 if( this == object ) 099 return true; 100 if( object == null || getClass() != object.getClass() ) 101 return false; 102 103 TuplePair tuplePair = (TuplePair) object; 104 105 if( !Arrays.equals( tuples, tuplePair.tuples ) ) 106 return false; 107 108 return true; 109 } 110 111 @Override 112 public int hashCode() 113 { 114 return Arrays.hashCode( tuples ); 115 } 116 117 @Override 118 public int compareTo( Object other ) 119 { 120 if( other instanceof TuplePair ) 121 return compareTo( (TuplePair) other ); 122 else 123 return -1; 124 } 125 126 @Override 127 public int compareTo( Tuple other ) 128 { 129 if( other instanceof TuplePair ) 130 return compareTo( (TuplePair) other ); 131 else 132 return -1; 133 } 134 135 /** 136 * Method compareTo compares this instance to the given TuplePair. 137 * 138 * @param tuplePair of type TuplePair 139 * @return int 140 */ 141 public int compareTo( TuplePair tuplePair ) 142 { 143 int c = tuples[ 0 ].compareTo( tuplePair.tuples[ 0 ] ); 144 145 if( c != 0 ) 146 return c; 147 148 c = tuples[ 1 ].compareTo( tuplePair.tuples[ 1 ] ); 149 150 return c; 151 } 152 153 @Override 154 public String toString() 155 { 156 return tuples[ 0 ].print() + tuples[ 1 ].print(); 157 } 158 159 @Override 160 public String print() 161 { 162 return tuples[ 0 ].print() + tuples[ 1 ].print(); 163 } 164 }