1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ********************************************************************** 5 * Copyright (c) 2002-2014, Google, International Business Machines 6 * Corporation and others. All Rights Reserved. 7 ********************************************************************** 8 * Author: Mark Davis 9 ********************************************************************** 10 */ 11 package com.ibm.icu.impl; 12 13 import com.ibm.icu.util.Freezable; 14 15 16 @SuppressWarnings({ "unchecked", "rawtypes" }) 17 public class Row<C0, C1, C2, C3, C4> implements java.lang.Comparable, Cloneable, 18 Freezable<Row<C0, C1, C2, C3, C4>>{ 19 protected Object[] items; 20 protected volatile boolean frozen; 21 22 /** 23 * Convenience Methods 24 */ 25 public static <C0, C1> R2<C0,C1> of(C0 p0, C1 p1) { 26 return new R2<C0,C1>(p0,p1); 27 } 28 public static <C0, C1, C2> R3<C0,C1,C2> of(C0 p0, C1 p1, C2 p2) { 29 return new R3<C0,C1,C2>(p0,p1,p2); 30 } 31 public static <C0, C1, C2, C3> R4<C0,C1,C2,C3> of(C0 p0, C1 p1, C2 p2, C3 p3) { 32 return new R4<C0,C1,C2,C3>(p0,p1,p2,p3); 33 } 34 public static <C0, C1, C2, C3, C4> R5<C0,C1,C2,C3,C4> of(C0 p0, C1 p1, C2 p2, C3 p3, C4 p4) { 35 return new R5<C0,C1,C2,C3,C4>(p0,p1,p2,p3,p4); 36 } 37 38 public static class R2<C0, C1> extends Row<C0, C1, C1, C1, C1> { 39 public R2(C0 a, C1 b) { 40 items = new Object[] {a, b}; 41 } 42 } 43 public static class R3<C0, C1, C2> extends Row<C0, C1, C2, C2, C2> { 44 public R3(C0 a, C1 b, C2 c) { 45 items = new Object[] {a, b, c}; 46 } 47 } 48 public static class R4<C0, C1, C2, C3> extends Row<C0, C1, C2, C3, C3> { 49 public R4(C0 a, C1 b, C2 c, C3 d) { 50 items = new Object[] {a, b, c, d}; 51 } 52 } 53 public static class R5<C0, C1, C2, C3, C4> extends Row<C0, C1, C2, C3, C4> { 54 public R5(C0 a, C1 b, C2 c, C3 d, C4 e) { 55 items = new Object[] {a, b, c, d, e}; 56 } 57 } 58 59 public Row<C0, C1, C2, C3, C4> set0(C0 item) { 60 return set(0, item); 61 } 62 public C0 get0() { 63 return (C0) items[0]; 64 } 65 public Row<C0, C1, C2, C3, C4> set1(C1 item) { 66 return set(1, item); 67 } 68 public C1 get1() { 69 return (C1) items[1]; 70 } 71 public Row<C0, C1, C2, C3, C4> set2(C2 item) { 72 return set(2, item); 73 } 74 public C2 get2() { 75 return (C2) items[2]; 76 } 77 public Row<C0, C1, C2, C3, C4> set3(C3 item) { 78 return set(3, item); 79 } 80 public C3 get3() { 81 return (C3) items[3]; 82 } 83 public Row<C0, C1, C2, C3, C4> set4(C4 item) { 84 return set(4, item); 85 } 86 public C4 get4() { 87 return (C4) items[4]; 88 } 89 90 protected Row<C0, C1, C2, C3, C4> set(int i, Object item) { 91 if (frozen) { 92 throw new UnsupportedOperationException("Attempt to modify frozen object"); 93 } 94 items[i] = item; 95 return this; 96 } 97 98 @Override 99 public int hashCode() { 100 int sum = items.length; 101 for (Object item : items) { 102 sum = sum*37 + Utility.checkHash(item); 103 } 104 return sum; 105 } 106 107 @Override 108 public boolean equals(Object other) { 109 if (other == null) { 110 return false; 111 } 112 if (this == other) { 113 return true; 114 } 115 try { 116 Row<C0, C1, C2, C3, C4> that = (Row<C0, C1, C2, C3, C4>)other; 117 if (items.length != that.items.length) { 118 return false; 119 } 120 int i = 0; 121 for (Object item : items) { 122 if (!Utility.objectEquals(item, that.items[i++])) { 123 return false; 124 } 125 } 126 return true; 127 } catch (Exception e) { 128 return false; 129 } 130 } 131 132 @Override 133 public int compareTo(Object other) { 134 int result; 135 Row<C0, C1, C2, C3, C4> that = (Row<C0, C1, C2, C3, C4>)other; 136 result = items.length - that.items.length; 137 if (result != 0) { 138 return result; 139 } 140 int i = 0; 141 for (Object item : items) { 142 result = Utility.checkCompare(((Comparable)item), ((Comparable)that.items[i++])); 143 if (result != 0) { 144 return result; 145 } 146 } 147 return 0; 148 } 149 150 @Override 151 public String toString() { 152 StringBuilder result = new StringBuilder("["); 153 boolean first = true; 154 for (Object item : items) { 155 if (first) { 156 first = false; 157 } else { 158 result.append(", "); 159 } 160 result.append(item); 161 } 162 return result.append("]").toString(); 163 } 164 165 @Override 166 public boolean isFrozen() { 167 return frozen; 168 } 169 170 @Override 171 public Row<C0, C1, C2, C3, C4> freeze() { 172 frozen = true; 173 return this; 174 } 175 176 @Override 177 public Object clone() { 178 if (frozen) return this; 179 try { 180 Row<C0, C1, C2, C3, C4> result = (Row<C0, C1, C2, C3, C4>) super.clone(); 181 items = items.clone(); 182 return result; 183 } catch (CloneNotSupportedException e) { 184 return null; 185 } 186 } 187 188 @Override 189 public Row<C0, C1, C2, C3, C4> cloneAsThawed() { 190 try { 191 Row<C0, C1, C2, C3, C4> result = (Row<C0, C1, C2, C3, C4>) super.clone(); 192 items = items.clone(); 193 result.frozen = false; 194 return result; 195 } catch (CloneNotSupportedException e) { 196 return null; 197 } 198 } 199 } 200 201