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