001/** 002 * 003 * Copyright 2020 Florian Schmaus. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smack.util; 018 019public final class Pair<F, S> { 020 021 private final F first; 022 private final S second; 023 024 private Pair(F first, S second) { 025 this.first = first; 026 this.second = second; 027 } 028 029 public static <F extends Object, S extends Object> Pair<F, S> create(F first, S second) { 030 return new Pair<>(first, second); 031 } 032 033 public static <F extends Object, S extends Object> Pair<F, S> createAndInitHashCode(F first, S second) { 034 Pair<F, S> pair = new Pair<>(first, second); 035 pair.hashCode(); 036 return pair; 037 } 038 039 public F getFirst() { 040 return first; 041 } 042 043 public S getSecond() { 044 return second; 045 } 046 047 private final HashCode.Cache hashCodeCache = new HashCode.Cache(); 048 049 @Override 050 public int hashCode() { 051 return hashCodeCache.getHashCode(c -> 052 c.append(first) 053 .append(second) 054 ); 055 } 056 057 @Override 058 public boolean equals(Object object) { 059 return EqualsUtil.equals(this, object, (e, o) -> 060 e.append(first, o.first) 061 .append(second, o.second) 062 ); 063 } 064}