1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.car.occupantconnection; 17 18 import android.annotation.NonNull; 19 20 import java.util.Objects; 21 22 /** 23 * A class used to identify a connection from a sender client to a receiver client. The sender 24 * client and the receiver client must have the same package name. 25 */ 26 final class ConnectionId { 27 28 /** The sender client. */ 29 public final ClientId senderClient; 30 /** The receiver client. */ 31 public final ClientId receiverClient; 32 ConnectionId(@onNull ClientId senderClient, @NonNull ClientId receiverClient)33 ConnectionId(@NonNull ClientId senderClient, @NonNull ClientId receiverClient) { 34 this.senderClient = Objects.requireNonNull(senderClient, "senderClient cannot be null"); 35 this.receiverClient = 36 Objects.requireNonNull(receiverClient, "receiverClient cannot be null"); 37 if (!senderClient.packageName.equals(receiverClient.packageName)) { 38 throw new IllegalArgumentException("Package name doesn't match! " 39 + "Sender:" + senderClient.packageName 40 + ", receiver:" + receiverClient.packageName); 41 } 42 } 43 44 @Override equals(Object o)45 public boolean equals(Object o) { 46 if (this == o) { 47 return true; 48 } 49 if (!(o instanceof ConnectionId)) { 50 return false; 51 } 52 ConnectionId other = (ConnectionId) o; 53 return senderClient.equals(other.senderClient) 54 && receiverClient.equals(other.receiverClient); 55 } 56 57 @Override hashCode()58 public int hashCode() { 59 return Objects.hash(senderClient, receiverClient); 60 } 61 62 @Override toString()63 public String toString() { 64 return "ConnectionId[senderClient=" + senderClient + ", receiverClient=" 65 + receiverClient + "]"; 66 } 67 } 68