Triple.java revision 2224:2a8815d86b93
1/*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package jdk.test.lib.util;
25
26import java.util.Objects;
27
28/**
29 * Triple - a three element tuple
30 *
31 * @param <F> first element type
32 * @param <S> second element type
33 * @param <T> third element type
34 */
35public class Triple<F, S, T> {
36    private final Pair<F, Pair<S, T>> container;
37
38    /**
39     * Constructor
40     *
41     * @param first  first element of the triple
42     * @param second second element of the triple
43     * @param third  third element of the triple
44     */
45    public Triple(F first, S second, T third) {
46        container = new Pair<>(first, new Pair<>(second, third));
47    }
48
49    /**
50     * Gets first element of the triple
51     */
52    public F getFirst() {
53        return container.first;
54    }
55
56    /**
57     * Gets second element of the triple
58     */
59    public S getSecond() {
60        return container.second.first;
61    }
62
63    /**
64     * Gets third element of the triple
65     */
66    public T getThird() {
67        return container.second.second;
68    }
69
70    @Override
71    public int hashCode() {
72        return container.hashCode();
73    }
74
75    @Override
76    public boolean equals(Object obj) {
77        if (obj instanceof Triple<?, ?, ?>) {
78            Triple<?, ?, ?> objTriple = (Triple<?, ?, ?>) obj;
79            return Objects.equals(container.first, objTriple.container.first)
80                    && Objects.equals(container.second,
81                    objTriple.container.second);
82        }
83        return false;
84    }
85
86    @Override
87    public String toString() {
88        return "(" + getFirst() + " : " + getSecond() + " : " + getThird() + ")";
89    }
90}
91