001: /*
002: * xtc - The eXTensible Compiler
003: * Copyright (C) 2007 New York University
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public License
007: * version 2.1 as published by the Free Software Foundation.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
017: * USA.
018: */
019: package xtc.typical;
020:
021: /**
022: * The superclass of all anonymous tuples.
023: *
024: * @author Laune Harris
025: * @version $Revision: 1.38 $
026: */
027: public abstract class Tuple {
028:
029: /** Create a new tuple. */
030: public Tuple() { /* Nothing to do. */
031: }
032:
033: /**
034: * Get this tuple's size.
035: *
036: * @return The size.
037: */
038: public abstract int size();
039:
040: // =========================================================================
041:
042: /** The canonical empty tuple. */
043: public final static T0 EMPTY = new T0();
044:
045: /** Get the first element. */
046: public <A> A get1() {
047: throw new IllegalStateException("Empty tuple");
048: }
049:
050: /** Get the second element. */
051: public <B> B get2() {
052: throw new IllegalStateException("Empty tuple");
053: }
054:
055: /** Get the third element. */
056: public <C> C get3() {
057: throw new IllegalStateException("Empty tuple");
058: }
059:
060: /** Get the fourth element. */
061: public <D> D get4() {
062: throw new IllegalStateException("Empty tuple");
063: }
064:
065: /** Get the fifth element. */
066: public <E> E get5() {
067: throw new IllegalStateException("Empty tuple");
068: }
069:
070: /** Get the sixth element. */
071: public <F> F get6() {
072: throw new IllegalStateException("Empty tuple");
073: }
074:
075: /** Get the seventh element. */
076: public <G> G get7() {
077: throw new IllegalStateException("Empty tuple");
078: }
079:
080: /** A tuple with zero members. */
081: public static class T0 extends Tuple {
082:
083: /** Create an empty tuple. */
084: public T0() { /* Nothing to do. */
085: }
086:
087: public int size() {
088: return 0;
089: }
090:
091: public int hashCode() {
092: return 0;
093: }
094:
095: public boolean equals(Object o) {
096: if (this == o)
097: return true;
098:
099: return (o instanceof T0);
100: }
101:
102: public String toString() {
103: return "()";
104: }
105:
106: }
107:
108: /** A tuple with one member. */
109: public static class T1<A> extends Tuple {
110:
111: /** The first member. */
112: A member1;
113:
114: /** Create a new T1. */
115: public T1(A m1) {
116: member1 = m1;
117: }
118:
119: public int size() {
120: return 1;
121: }
122:
123: @SuppressWarnings("unchecked")
124: public A get1() {
125: return member1;
126: }
127:
128: public int hashcode() {
129: return member1.hashCode() + 7;
130: }
131:
132: public boolean equals(Object o) {
133: if (this == o)
134: return true;
135:
136: if (o instanceof T1) {
137: T1<?> t = (T1<?>) o;
138: return member1.equals(t.member1);
139: }
140:
141: return false;
142: }
143:
144: public String toString() {
145: return "(" + toString(member1) + ")";
146: }
147:
148: }
149:
150: /** A tuple witn 2 members. */
151: public static class T2<A, B> extends Tuple {
152:
153: /** The first member. */
154: A member1;
155:
156: /** The second member. */
157: B member2;
158:
159: /** Create a new T2. */
160: public T2(A m1, B m2) {
161: member1 = m1;
162: member2 = m2;
163: }
164:
165: public int size() {
166: return 2;
167: }
168:
169: @SuppressWarnings("unchecked")
170: public A get1() {
171: return member1;
172: }
173:
174: @SuppressWarnings("unchecked")
175: public B get2() {
176: return member2;
177: }
178:
179: public int hashCode() {
180: return member1.hashCode() * member2.hashCode() + 7;
181: }
182:
183: public boolean equals(Object o) {
184: if (this == o)
185: return true;
186:
187: if (this == o)
188: return true;
189:
190: if (o instanceof T2) {
191: T2<?, ?> other = (T2<?, ?>) o;
192: return member1.equals(other.member1)
193: && member2.equals(other.member2);
194: }
195: return false;
196: }
197:
198: public String toString() {
199: return "(" + toString(member1) + "," + toString(member2)
200: + ")";
201: }
202: }
203:
204: /** A tuple witn 3 members. */
205: public static class T3<A, B, C> extends Tuple {
206:
207: /** The first member. */
208: A member1;
209:
210: /** The second member. */
211: B member2;
212:
213: /** The third member. */
214: C member3;
215:
216: /** Createa a new T3.*/
217: public T3(A m1, B m2, C m3) {
218: member1 = m1;
219: member2 = m2;
220: member3 = m3;
221: }
222:
223: public int size() {
224: return 3;
225: }
226:
227: @SuppressWarnings("unchecked")
228: public A get1() {
229: return member1;
230: }
231:
232: @SuppressWarnings("unchecked")
233: public B get2() {
234: return member2;
235: }
236:
237: @SuppressWarnings("unchecked")
238: public C get3() {
239: return member3;
240: }
241:
242: public int hashCode() {
243: return member1.hashCode() * member2.hashCode()
244: * member3.hashCode() + 7;
245: }
246:
247: public boolean equals(Object o) {
248: if (this == o)
249: return true;
250:
251: if (o instanceof T3) {
252: T3<?, ?, ?> t = (T3<?, ?, ?>) o;
253:
254: return member1.equals(t.get1())
255: && member2.equals(t.get2())
256: && member3.equals(t.get3());
257: }
258: return false;
259: }
260:
261: public String toString() {
262: return "(" + toString(member1) + "," + toString(member2)
263: + "," + toString(member3) + ")";
264: }
265: }
266:
267: /** A tuple witn 4 members. */
268: public static class T4<A, B, C, D> extends Tuple {
269:
270: /** The first member. */
271: A member1;
272:
273: /** The second member. */
274: B member2;
275:
276: /** The third member. */
277: C member3;
278:
279: /** The fourth member. */
280: D member4;
281:
282: /** Create a new T4. */
283: public T4(A m1, B m2, C m3, D m4) {
284: member1 = m1;
285: member2 = m2;
286: member3 = m3;
287: member4 = m4;
288: }
289:
290: public int size() {
291: return 4;
292: }
293:
294: @SuppressWarnings("unchecked")
295: public A get1() {
296: return member1;
297: }
298:
299: @SuppressWarnings("unchecked")
300: public B get2() {
301: return member2;
302: }
303:
304: @SuppressWarnings("unchecked")
305: public C get3() {
306: return member3;
307: }
308:
309: @SuppressWarnings("unchecked")
310: public D get4() {
311: return member4;
312: }
313:
314: public int hashCode() {
315: return member1.hashCode() * member2.hashCode()
316: * member3.hashCode() * member4.hashCode() + 7;
317: }
318:
319: public boolean equals(Object o) {
320: if (this == o)
321: return true;
322:
323: if (o instanceof T4) {
324: T4<?, ?, ?, ?> t = (T4<?, ?, ?, ?>) o;
325:
326: return member1.equals(t.get1())
327: && member2.equals(t.get2())
328: && member3.equals(t.get3())
329: && member4.equals(t.get4());
330: }
331:
332: return false;
333: }
334:
335: public String toString() {
336: return "(" + toString(member1) + "," + toString(member2)
337: + "," + toString(member3) + "," + toString(member4)
338: + ")";
339: }
340:
341: }
342:
343: /** A tuple witn 5 members. */
344: public static class T5<A, B, C, D, E> extends Tuple {
345:
346: /** The first member. */
347: A member1;
348:
349: /** The second member. */
350: B member2;
351:
352: /** The third member. */
353: C member3;
354:
355: /** The fourth member. */
356: D member4;
357:
358: /** The fifth member. */
359: E member5;
360:
361: /** Create a new T5. */
362: public T5(A m1, B m2, C m3, D m4, E m5) {
363: member1 = m1;
364: member2 = m2;
365: member3 = m3;
366: member4 = m4;
367: member5 = m5;
368: }
369:
370: public int size() {
371: return 5;
372: }
373:
374: @SuppressWarnings("unchecked")
375: public A get1() {
376: return member1;
377: }
378:
379: @SuppressWarnings("unchecked")
380: public B get2() {
381: return member2;
382: }
383:
384: @SuppressWarnings("unchecked")
385: public C get3() {
386: return member3;
387: }
388:
389: @SuppressWarnings("unchecked")
390: public D get4() {
391: return member4;
392: }
393:
394: @SuppressWarnings("unchecked")
395: public E get5() {
396: return member5;
397: }
398:
399: public int hashCode() {
400: return member1.hashCode() * member2.hashCode()
401: * member3.hashCode() * member4.hashCode()
402: * member5.hashCode() + 7;
403: }
404:
405: public boolean equals(Object o) {
406: if (this == o)
407: return true;
408:
409: if (o instanceof T5) {
410: T5<?, ?, ?, ?, ?> t = (T5<?, ?, ?, ?, ?>) o;
411:
412: return member1.equals(t.get1())
413: && member2.equals(t.get2())
414: && member3.equals(t.get3())
415: && member4.equals(t.get4())
416: && member5.equals(t.get5());
417: }
418:
419: return false;
420: }
421:
422: public String toString() {
423: return "(" + toString(member1) + "," + toString(member2)
424: + "," + toString(member3) + "," + toString(member4)
425: + "," + toString(member5) + ")";
426: }
427:
428: }
429:
430: /** A tuple with 6 members. */
431: @SuppressWarnings("unchecked")
432: public static class T6<A, B, C, D, E, F> extends Tuple {
433:
434: /** The first member. */
435: A member1;
436:
437: /** The second member. */
438: B member2;
439:
440: /** The third member. */
441: C member3;
442:
443: /** The fourth member. */
444: D member4;
445:
446: /** The fifth member. */
447: E member5;
448:
449: /** The sixth member. */
450: F member6;
451:
452: /** Create a new T6. */
453: public T6(A m1, B m2, C m3, D m4, E m5, F m6) {
454: member1 = m1;
455: member2 = m2;
456: member3 = m3;
457: member4 = m4;
458: member5 = m5;
459: member6 = m6;
460: }
461:
462: public int size() {
463: return 6;
464: }
465:
466: public A get1() {
467: return member1;
468: }
469:
470: public B get2() {
471: return member2;
472: }
473:
474: public C get3() {
475: return member3;
476: }
477:
478: public D get4() {
479: return member4;
480: }
481:
482: public E get5() {
483: return member5;
484: }
485:
486: public F get6() {
487: return member6;
488: }
489:
490: public int hashCode() {
491: return member1.hashCode() * member2.hashCode()
492: * member3.hashCode() * member4.hashCode()
493: * member5.hashCode() * member6.hashCode() + 7;
494: }
495:
496: public boolean equals(Object o) {
497: if (this == o)
498: return true;
499:
500: if (o instanceof T6) {
501: T6<?, ?, ?, ?, ?, ?> t = (T6<?, ?, ?, ?, ?, ?>) o;
502:
503: return member1.equals(t.get1())
504: && member2.equals(t.get2())
505: && member3.equals(t.get3())
506: && member4.equals(t.get4())
507: && member5.equals(t.get5())
508: && member6.equals(t.get6());
509: }
510: return false;
511: }
512:
513: public String toString() {
514: return "(" + toString(member1) + "," + toString(member2)
515: + "," + toString(member3) + "," + toString(member4)
516: + "," + toString(member5) + "," + toString(member6)
517: + ")";
518: }
519: }
520:
521: /** A tuple with 7 members. */
522: @SuppressWarnings("unchecked")
523: public static class T7<A, B, C, D, E, F, G> extends Tuple {
524:
525: /** The first member. */
526: A member1;
527:
528: /** The second member. */
529: B member2;
530:
531: /** The third member. */
532: C member3;
533:
534: /** The fourth member. */
535: D member4;
536:
537: /** The fifth member. */
538: E member5;
539:
540: /** The sixth member. */
541: F member6;
542:
543: /** The seventh member. */
544: G member7;
545:
546: /** Create a new T7. */
547: public T7(A m1, B m2, C m3, D m4, E m5, F m6, G m7) {
548: member1 = m1;
549: member2 = m2;
550: member3 = m3;
551: member4 = m4;
552: member5 = m5;
553: member6 = m6;
554: member7 = m7;
555: }
556:
557: public int size() {
558: return 7;
559: }
560:
561: public A get1() {
562: return member1;
563: }
564:
565: public B get2() {
566: return member2;
567: }
568:
569: public C get3() {
570: return member3;
571: }
572:
573: public D get4() {
574: return member4;
575: }
576:
577: public E get5() {
578: return member5;
579: }
580:
581: public F get6() {
582: return member6;
583: }
584:
585: public G get7() {
586: return member7;
587: }
588:
589: public int hashCode() {
590: return member1.hashCode() * member2.hashCode()
591: * member3.hashCode() * member4.hashCode()
592: * member5.hashCode() * member7.hashCode() + 7;
593: }
594:
595: public boolean equals(Object o) {
596: if (o instanceof T7) {
597: T7<?, ?, ?, ?, ?, ?, ?> t = (T7<?, ?, ?, ?, ?, ?, ?>) o;
598:
599: return member1.equals(t.get1())
600: && member2.equals(t.get2())
601: && member3.equals(t.get3())
602: && member4.equals(t.get4())
603: && member5.equals(t.get5())
604: && member6.equals(t.get6())
605: && member7.equals(t.get7());
606: }
607:
608: return false;
609: }
610:
611: public String toString() {
612: return "(" + toString(member1) + "," + toString(member2)
613: + "," + toString(member3) + "," + toString(member4)
614: + "," + toString(member5) + "," + toString(member6)
615: + "," + toString(member7) + ")";
616: }
617: }
618:
619: /**
620: * Print an object as a string.
621: *
622: * @param o The object.
623: * @return The corresponding string.
624: */
625: public final static String toString(Object o) {
626: return (null == o) ? "?" : o.toString();
627: }
628:
629: }
|