15ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler/*
25ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler * Copyright (C) 2010 The Android Open Source Project
35ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler *
45ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler * Licensed under the Apache License, Version 2.0 (the "License");
55ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler * you may not use this file except in compliance with the License.
65ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler * You may obtain a copy of the License at
75ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler *
85ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler *      http://www.apache.org/licenses/LICENSE-2.0
95ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler *
105ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler * Unless required by applicable law or agreed to in writing, software
115ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler * distributed under the License is distributed on an "AS IS" BASIS,
125ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler * See the License for the specific language governing permissions and
145ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler * limitations under the License.
155ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler */
165ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittlerpackage tests.util;
175ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler/**
185ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler * Pair of typed values.
195ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler *
205ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler * <p>Pairs are obtained using {@link #of(Object, Object) of}.
215ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler *
225ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler * @param <F> type of the first value.
235ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler * @param <S> type of the second value.
245ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler */
255ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittlerpublic class Pair<F, S> {
265ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    private final F mFirst;
275ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    private final S mSecond;
285ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    private Pair(F first, S second) {
295ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        mFirst = first;
305ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        mSecond = second;
315ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    }
325ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    /**
335ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler     * Gets the pair consisting of the two provided values.
345ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler     *
355ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler     * @param first first value or {@code null}.
365ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler     * @param second second value or {@code null}.
375ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler     */
385ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    public static <F, S> Pair<F, S> of(F first, S second) {
395ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        return new Pair<F, S>(first, second);
405ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    }
415ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    /**
425ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler     * Gets the first value from this pair.
435ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler     *
445ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler     * @return value or {@code null}.
455ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler     */
465ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    public F getFirst() {
475ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        return mFirst;
485ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    }
495ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    /**
505ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler     * Gets the second value from this pair.
515ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler     *
525ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler     * @return value or {@code null}.
535ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler     */
545ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    public S getSecond() {
555ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        return mSecond;
565ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    }
575ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    @Override
585ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    public String toString() {
595ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        return "Pair[" + mFirst + ", " + mSecond + "]";
605ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    }
615ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    @Override
625ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    public int hashCode() {
635ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        final int prime = 31;
645ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        int result = 1;
655ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        result = prime * result + ((mFirst == null) ? 0 : mFirst.hashCode());
665ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        result = prime * result + ((mSecond == null) ? 0 : mSecond.hashCode());
675ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        return result;
685ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    }
695ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    @Override
705ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    public boolean equals(Object obj) {
715ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        if (this == obj) {
725ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler            return true;
735ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        }
745ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        if (obj == null) {
755ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler            return false;
765ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        }
775ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        if (getClass() != obj.getClass()) {
785ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler            return false;
795ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        }
805ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        @SuppressWarnings("rawtypes")
815ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        Pair other = (Pair) obj;
825ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        if (mFirst == null) {
835ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler            if (other.mFirst != null) {
845ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler                return false;
855ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler            }
865ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        } else if (!mFirst.equals(other.mFirst)) {
875ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler            return false;
885ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        }
895ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        if (mSecond == null) {
905ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler            if (other.mSecond != null) {
915ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler                return false;
925ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler            }
935ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        } else if (!mSecond.equals(other.mSecond)) {
945ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler            return false;
955ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        }
965ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler        return true;
975ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler    }
985ca87050dafe566d8d4e2b11e395f7d06a2d837bNathan Mittler}
99