|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| package edu.rice.cs.plt.tuple; |
|
36 |
| |
|
37 |
| import java.io.Serializable; |
|
38 |
| import java.util.Comparator; |
|
39 |
| |
|
40 |
| import edu.rice.cs.plt.collect.CollectUtil; |
|
41 |
| import edu.rice.cs.plt.collect.TotalOrder; |
|
42 |
| import edu.rice.cs.plt.lambda.Lambda; |
|
43 |
| import edu.rice.cs.plt.lambda.Lambda4; |
|
44 |
| import edu.rice.cs.plt.object.ObjectUtil; |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| public class Quad<T1, T2, T3, T4> extends Tuple { |
|
51 |
| |
|
52 |
| protected final T1 _first; |
|
53 |
| protected final T2 _second; |
|
54 |
| protected final T3 _third; |
|
55 |
| protected final T4 _fourth; |
|
56 |
| |
|
57 |
0
| public Quad(T1 first, T2 second, T3 third, T4 fourth) {
|
|
58 |
0
| _first = first;
|
|
59 |
0
| _second = second;
|
|
60 |
0
| _third = third;
|
|
61 |
0
| _fourth = fourth;
|
|
62 |
| } |
|
63 |
| |
|
64 |
0
| public T1 first() { return _first; }
|
|
65 |
0
| public T2 second() { return _second; }
|
|
66 |
0
| public T3 third() { return _third; }
|
|
67 |
0
| public T4 fourth() { return _fourth; }
|
|
68 |
| |
|
69 |
0
| public String toString() {
|
|
70 |
0
| return "(" + _first + ", " + _second + ", " + _third + ", " + _fourth + ")";
|
|
71 |
| } |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
0
| public boolean equals(Object o) {
|
|
78 |
0
| if (this == o) { return true; }
|
|
79 |
0
| else if (o == null || !getClass().equals(o.getClass())) { return false; }
|
|
80 |
| else { |
|
81 |
0
| Quad<?, ?, ?, ?> cast = (Quad<?, ?, ?, ?>) o;
|
|
82 |
0
| return
|
|
83 |
0
| (_first == null ? cast._first == null : _first.equals(cast._first)) &&
|
|
84 |
0
| (_second == null ? cast._second == null : _second.equals(cast._second)) &&
|
|
85 |
0
| (_third == null ? cast._third == null : _third.equals(cast._third)) &&
|
|
86 |
0
| (_fourth == null ? cast._fourth == null : _fourth.equals(cast._fourth));
|
|
87 |
| } |
|
88 |
| } |
|
89 |
| |
|
90 |
0
| protected int generateHashCode() {
|
|
91 |
0
| return
|
|
92 |
0
| (_first == null ? 0 : _first.hashCode()) ^
|
|
93 |
0
| (_second == null ? 0 : _second.hashCode() << 1) ^
|
|
94 |
0
| (_third == null ? 0 : _third.hashCode() << 2) ^
|
|
95 |
0
| (_fourth == null ? 0 : _fourth.hashCode() << 3) ^
|
|
96 |
| getClass().hashCode(); |
|
97 |
| } |
|
98 |
| |
|
99 |
| |
|
100 |
0
| public static <T1, T2, T3, T4> Quad<T1, T2, T3, T4> make(T1 first, T2 second, T3 third,
|
|
101 |
| T4 fourth) { |
|
102 |
0
| return new Quad<T1, T2, T3, T4>(first, second, third, fourth);
|
|
103 |
| } |
|
104 |
| |
|
105 |
| |
|
106 |
0
| @SuppressWarnings("unchecked") public static
|
|
107 |
| <T1, T2, T3, T4> Lambda4<T1, T2, T3, T4, Quad<T1, T2, T3, T4>> factory() { |
|
108 |
0
| return (Factory<T1, T2, T3, T4>) Factory.INSTANCE;
|
|
109 |
| } |
|
110 |
| |
|
111 |
| private static final class Factory<T1, T2, T3, T4> |
|
112 |
| implements Lambda4<T1, T2, T3, T4, Quad<T1, T2, T3, T4>>, Serializable { |
|
113 |
| public static final Factory<Object, Object, Object, Object> INSTANCE = |
|
114 |
| new Factory<Object, Object, Object, Object>(); |
|
115 |
0
| private Factory() {}
|
|
116 |
0
| public Quad<T1, T2, T3, T4> value(T1 first, T2 second, T3 third, T4 fourth) {
|
|
117 |
0
| return new Quad<T1, T2, T3, T4>(first, second, third, fourth);
|
|
118 |
| } |
|
119 |
| } |
|
120 |
| |
|
121 |
| |
|
122 |
0
| @SuppressWarnings("unchecked") public static <T> Lambda<Quad<? extends T, ?, ?, ?>, T> firstGetter() {
|
|
123 |
0
| return (GetFirst<T>) GetFirst.INSTANCE;
|
|
124 |
| } |
|
125 |
| |
|
126 |
| private static final class GetFirst<T> implements Lambda<Quad<? extends T, ?, ?, ?>, T>, Serializable { |
|
127 |
| public static final GetFirst<Void> INSTANCE = new GetFirst<Void>(); |
|
128 |
0
| private GetFirst() {}
|
|
129 |
0
| public T value(Quad<? extends T, ?, ?, ?> arg) { return arg.first(); }
|
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
0
| @SuppressWarnings("unchecked") public static <T> Lambda<Quad<?, ? extends T, ?, ?>, T> secondGetter() {
|
|
134 |
0
| return (GetSecond<T>) GetSecond.INSTANCE;
|
|
135 |
| } |
|
136 |
| |
|
137 |
| private static final class GetSecond<T> implements Lambda<Quad<?, ? extends T, ?, ?>, T>, Serializable { |
|
138 |
| public static final GetSecond<Void> INSTANCE = new GetSecond<Void>(); |
|
139 |
0
| private GetSecond() {}
|
|
140 |
0
| public T value(Quad<?, ? extends T, ?, ?> arg) { return arg.second(); }
|
|
141 |
| } |
|
142 |
| |
|
143 |
| |
|
144 |
0
| @SuppressWarnings("unchecked") public static <T> Lambda<Quad<?, ?, ? extends T, ?>, T> thirdGetter() {
|
|
145 |
0
| return (GetThird<T>) GetThird.INSTANCE;
|
|
146 |
| } |
|
147 |
| |
|
148 |
| private static final class GetThird<T> implements Lambda<Quad<?, ?, ? extends T, ?>, T>, Serializable { |
|
149 |
| public static final GetThird<Void> INSTANCE = new GetThird<Void>(); |
|
150 |
0
| private GetThird() {}
|
|
151 |
0
| public T value(Quad<?, ?, ? extends T, ?> arg) { return arg.third(); }
|
|
152 |
| } |
|
153 |
| |
|
154 |
| |
|
155 |
0
| @SuppressWarnings("unchecked") public static <T> Lambda<Quad<?, ?, ?, ? extends T>, T> fourthGetter() {
|
|
156 |
0
| return (GetFourth<T>) GetFourth.INSTANCE;
|
|
157 |
| } |
|
158 |
| |
|
159 |
| private static final class GetFourth<T> implements Lambda<Quad<?, ?, ?, ? extends T>, T>, Serializable { |
|
160 |
| public static final GetFourth<Void> INSTANCE = new GetFourth<Void>(); |
|
161 |
0
| private GetFourth() {}
|
|
162 |
0
| public T value(Quad<?, ?, ?, ? extends T> arg) { return arg.fourth(); }
|
|
163 |
| } |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
0
| public static <T1 extends Comparable<? super T1>, T2 extends Comparable<? super T2>,
|
|
170 |
| T3 extends Comparable<? super T3>, T4 extends Comparable<? super T4>> |
|
171 |
| TotalOrder<Quad<? extends T1, ? extends T2, ? extends T3, ? extends T4>> comparator() { |
|
172 |
0
| return new QuadComparator<T1, T2, T3, T4>(CollectUtil.<T1>naturalOrder(), CollectUtil.<T2>naturalOrder(),
|
|
173 |
| CollectUtil.<T3>naturalOrder(), CollectUtil.<T4>naturalOrder()); |
|
174 |
| } |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
0
| public static <T1, T2, T3, T4> TotalOrder<Quad<? extends T1, ? extends T2, ? extends T3, ? extends T4>>
|
|
181 |
| comparator(Comparator<? super T1> comp1, Comparator<? super T2> comp2, |
|
182 |
| Comparator<? super T3> comp3, Comparator<? super T4> comp4) { |
|
183 |
0
| return new QuadComparator<T1, T2, T3, T4>(comp1, comp2, comp3, comp4);
|
|
184 |
| } |
|
185 |
| |
|
186 |
| private static final class QuadComparator<T1, T2, T3, T4> |
|
187 |
| extends TotalOrder<Quad<? extends T1, ? extends T2, |
|
188 |
| ? extends T3, ? extends T4>> { |
|
189 |
| private final Comparator<? super T1> _comp1; |
|
190 |
| private final Comparator<? super T2> _comp2; |
|
191 |
| private final Comparator<? super T3> _comp3; |
|
192 |
| private final Comparator<? super T4> _comp4; |
|
193 |
0
| public QuadComparator(Comparator<? super T1> comp1, Comparator<? super T2> comp2,
|
|
194 |
| Comparator<? super T3> comp3, Comparator<? super T4> comp4) { |
|
195 |
0
| _comp1 = comp1;
|
|
196 |
0
| _comp2 = comp2;
|
|
197 |
0
| _comp3 = comp3;
|
|
198 |
0
| _comp4 = comp4;
|
|
199 |
| } |
|
200 |
0
| public int compare(Quad<? extends T1, ? extends T2, ? extends T3, ? extends T4> q1,
|
|
201 |
| Quad<? extends T1, ? extends T2, ? extends T3, ? extends T4> q2) { |
|
202 |
0
| return ObjectUtil.compare(_comp1, q1.first(), q2.first(), _comp2, q1.second(), q2.second(),
|
|
203 |
| _comp3, q1.third(), q2.third(), _comp4, q1.fourth(), q2.fourth()); |
|
204 |
| } |
|
205 |
0
| public boolean equals(Object o) {
|
|
206 |
0
| if (this == o) { return true; }
|
|
207 |
0
| else if (!(o instanceof QuadComparator<?,?,?,?>)) { return false; }
|
|
208 |
| else { |
|
209 |
0
| QuadComparator<?,?,?,?> cast = (QuadComparator<?,?,?,?>) o;
|
|
210 |
0
| return _comp1.equals(cast._comp1) && _comp2.equals(cast._comp2) && _comp3.equals(cast._comp3);
|
|
211 |
| } |
|
212 |
| } |
|
213 |
0
| public int hashCode() { return ObjectUtil.hash(QuadComparator.class, _comp1, _comp2, _comp3, _comp4); }
|
|
214 |
| } |
|
215 |
| |
|
216 |
| } |