1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package com.ohos.hapsigntool.hap.entity; 17 18 /** 19 * Pair of two elements 20 * 21 * @since 2021-12-13 22 */ 23 public final class Pair<A, B> { 24 private final A mFirst; 25 26 private final B mSecond; 27 Pair(A first, B second)28 private Pair(A first, B second) { 29 mFirst = first; 30 mSecond = second; 31 } 32 33 /** 34 * create a pair with key of type A and value of type B 35 * 36 * @param first key of pair 37 * @param second value of pair 38 * @param <A> type of key 39 * @param <B> type of value 40 * @return a pair with key of type A and value of type B 41 */ create(A first, B second)42 public static <A, B> Pair<A, B> create(A first, B second) { 43 return new Pair<A, B>(first, second); 44 } 45 46 /** 47 * get key of pair 48 * 49 * @return key of pair 50 */ getFirst()51 public A getFirst() { 52 return mFirst; 53 } 54 55 /** 56 * get value of pair 57 * 58 * @return value of pair 59 */ getSecond()60 public B getSecond() { 61 return mSecond; 62 } 63 64 @Override hashCode()65 public int hashCode() { 66 final int prime = 31; 67 int result = 1; 68 result = prime * result + ((mFirst == null) ? 0 : mFirst.hashCode()); 69 result = prime * result + ((mSecond == null) ? 0 : mSecond.hashCode()); 70 return result; 71 } 72 73 @Override equals(Object obj)74 public boolean equals(Object obj) { 75 if (this == obj) { 76 return true; 77 } 78 if ((obj == null) || (getClass() != obj.getClass()) || (!(obj instanceof Pair))) { 79 return false; 80 } 81 Pair other = (Pair) obj; 82 return compare(mFirst, other.mFirst) && compare(mSecond, other.mSecond); 83 } 84 compare(C value1, C value2)85 private <C> boolean compare(C value1, C value2) { 86 if (value1 == null) { 87 return value2 == null; 88 } 89 return value1.equals(value2); 90 } 91 } 92