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