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.List; 024 025import cascading.tuple.Tuple; 026 027/** Class IndexTuple allows for managing an int index value with a Tuple instance. Used internally for co-grouping values. */ 028public class IndexTuple extends Tuple implements Comparable<Object> 029 { 030 int index; 031 Tuple tuple; 032 033 /** Constructor IndexTuple creates a new IndexTuple instance. */ 034 public IndexTuple() 035 { 036 super( (List<Object>) null ); 037 } 038 039 /** 040 * Constructor IndexTuple creates a new IndexTuple instance. 041 * 042 * @param index of type int 043 * @param tuple of type Tuple 044 */ 045 public IndexTuple( int index, Tuple tuple ) 046 { 047 super( (List<Comparable>) null ); 048 this.index = index; 049 this.tuple = tuple; 050 } 051 052 public void setIndex( int index ) 053 { 054 this.index = index; 055 } 056 057 public int getIndex() 058 { 059 return index; 060 } 061 062 public void setTuple( Tuple tuple ) 063 { 064 this.tuple = tuple; 065 } 066 067 public Tuple getTuple() 068 { 069 return tuple; 070 } 071 072 @Override 073 public String print() 074 { 075 return printTo( new StringBuffer() ).toString(); 076 } 077 078 public StringBuffer printTo( StringBuffer buffer ) 079 { 080 buffer.append( "{" ); 081 082 buffer.append( index ).append( ":" ); 083 084 tuple.printTo( buffer ); 085 086 buffer.append( "}" ); 087 088 return buffer; 089 } 090 091 public int compareTo( Object object ) 092 { 093 if( object instanceof IndexTuple ) 094 return compareTo( (IndexTuple) object ); 095 096 return -1; 097 } 098 099 public int compareTo( IndexTuple indexTuple ) 100 { 101 int c = this.index - indexTuple.index; 102 103 if( c != 0 ) 104 return c; 105 106 return this.tuple.compareTo( indexTuple.tuple ); 107 } 108 109 @Override 110 public boolean equals( Object object ) 111 { 112 if( this == object ) 113 return true; 114 if( object == null || getClass() != object.getClass() ) 115 return false; 116 117 IndexTuple that = (IndexTuple) object; 118 119 if( index != that.index ) 120 return false; 121 if( tuple != null ? !tuple.equals( that.tuple ) : that.tuple != null ) 122 return false; 123 124 return true; 125 } 126 127 @Override 128 public int hashCode() 129 { 130 int result = index; 131 result = 31 * result + ( tuple != null ? tuple.hashCode() : 0 ); 132 return result; 133 } 134 135 @Override 136 public String toString() 137 { 138 return "[" + index + "]" + tuple; 139 } 140 }