1 46 package groovy.lang; 47 48 import java.util.AbstractList ; 49 import java.util.List ; 50 51 import org.codehaus.groovy.runtime.InvokerHelper; 52 53 60 public class Tuple extends AbstractList { 61 62 private Object [] contents; 63 private int hashCode; 64 65 public Tuple(Object [] contents) { 66 this.contents = contents; 67 } 68 69 public Object get(int index) { 70 return contents[index]; 71 } 72 73 public int size() { 74 return contents.length; 75 } 76 77 public boolean equals(Object that) { 78 if (that instanceof Tuple) { 79 return equals((Tuple) that); 80 } 81 return false; 82 } 83 84 public boolean equals(Tuple that) { 85 if (contents.length == that.contents.length) { 86 for (int i = 0; i < contents.length; i++) { 87 if (! InvokerHelper.compareEqual(this.contents[i], that.contents[i])) { 88 return false; 89 } 90 } 91 return true; 92 } 93 return false; 94 } 95 96 97 public int hashCode() { 98 if (hashCode == 0) { 99 for (int i = 0; i < contents.length; i++ ) { 100 Object value = contents[i]; 101 int hash = (value != null) ? value.hashCode() : 0xbabe; 102 hashCode ^= hash; 103 } 104 if (hashCode == 0) { 105 hashCode = 0xbabe; 106 } 107 } 108 return hashCode; 109 } 110 111 public List subList(int fromIndex, int toIndex) { 112 int size = toIndex - fromIndex; 113 Object [] newContent = new Object [size]; 114 System.arraycopy(contents, fromIndex, newContent, 0, size); 115 return new Tuple(newContent); 116 } 117 } 118 | Popular Tags |