1 /*
2  * Copyright (C) 2021 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 
17 package android.net;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.text.TextUtils;
24 
25 import java.util.Objects;
26 
27 /**
28  * A {@link NetworkSpecifier} used to identify ethernet interfaces.
29  */
30 public final class EthernetNetworkSpecifier extends NetworkSpecifier implements Parcelable {
31 
32     /**
33      * Name of the network interface.
34      */
35     @NonNull
36     private final String mInterfaceName;
37 
38     /**
39      * Create a new EthernetNetworkSpecifier.
40      * @param interfaceName Name of the ethernet interface the specifier refers to.
41      */
EthernetNetworkSpecifier(@onNull String interfaceName)42     public EthernetNetworkSpecifier(@NonNull String interfaceName) {
43         if (TextUtils.isEmpty(interfaceName)) {
44             throw new IllegalArgumentException();
45         }
46         mInterfaceName = interfaceName;
47     }
48 
49     /**
50      * Get the name of the ethernet interface the specifier refers to.
51      */
52     @Nullable
getInterfaceName()53     public String getInterfaceName() {
54         // This may be null in the future to support specifiers based on data other than the
55         // interface name.
56         return mInterfaceName;
57     }
58 
59     /** @hide */
60     @Override
canBeSatisfiedBy(@ullable NetworkSpecifier other)61     public boolean canBeSatisfiedBy(@Nullable NetworkSpecifier other) {
62         return equals(other);
63     }
64 
65     @Override
equals(@ullable Object o)66     public boolean equals(@Nullable Object o) {
67         if (!(o instanceof EthernetNetworkSpecifier)) return false;
68         return TextUtils.equals(mInterfaceName, ((EthernetNetworkSpecifier) o).mInterfaceName);
69     }
70 
71     @Override
hashCode()72     public int hashCode() {
73         return Objects.hashCode(mInterfaceName);
74     }
75 
76     @Override
toString()77     public String toString() {
78         return "EthernetNetworkSpecifier (" + mInterfaceName + ")";
79     }
80 
81     @Override
describeContents()82     public int describeContents() {
83         return 0;
84     }
85 
86     @Override
writeToParcel(@onNull Parcel dest, int flags)87     public void writeToParcel(@NonNull Parcel dest, int flags) {
88         dest.writeString(mInterfaceName);
89     }
90 
91     public static final @NonNull Parcelable.Creator<EthernetNetworkSpecifier> CREATOR =
92             new Parcelable.Creator<EthernetNetworkSpecifier>() {
93         public EthernetNetworkSpecifier createFromParcel(Parcel in) {
94             return new EthernetNetworkSpecifier(in.readString());
95         }
96         public EthernetNetworkSpecifier[] newArray(int size) {
97             return new EthernetNetworkSpecifier[size];
98         }
99     };
100 }
101