Merge "MacAddress: add @NonNull annotations" am: bc3edde9c6

am: a7c125e5b2

Change-Id: Icaa08c082797c6dbc3382e0662e24e1c84114175
This commit is contained in:
Hugo Benichi
2018-01-16 10:30:20 +00:00
committed by android-build-merger
2 changed files with 16 additions and 15 deletions

View File

@@ -17,6 +17,7 @@
package android.net; package android.net;
import android.annotation.IntDef; import android.annotation.IntDef;
import android.annotation.NonNull;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
@@ -60,7 +61,7 @@ public final class MacAddress implements Parcelable {
}) })
public @interface MacAddressType { } public @interface MacAddressType { }
/** Indicates a MAC address of unknown type. */ /** @hide Indicates a MAC address of unknown type. */
public static final int TYPE_UNKNOWN = 0; public static final int TYPE_UNKNOWN = 0;
/** Indicates a MAC address is a unicast address. */ /** Indicates a MAC address is a unicast address. */
public static final int TYPE_UNICAST = 1; public static final int TYPE_UNICAST = 1;
@@ -92,7 +93,7 @@ public final class MacAddress implements Parcelable {
* *
* @return the int constant representing the MAC address type of this MacAddress. * @return the int constant representing the MAC address type of this MacAddress.
*/ */
public @MacAddressType int addressType() { public @MacAddressType int getAddressType() {
if (equals(BROADCAST_ADDRESS)) { if (equals(BROADCAST_ADDRESS)) {
return TYPE_BROADCAST; return TYPE_BROADCAST;
} }
@@ -120,12 +121,12 @@ public final class MacAddress implements Parcelable {
/** /**
* @return a byte array representation of this MacAddress. * @return a byte array representation of this MacAddress.
*/ */
public byte[] toByteArray() { public @NonNull byte[] toByteArray() {
return byteAddrFromLongAddr(mAddr); return byteAddrFromLongAddr(mAddr);
} }
@Override @Override
public String toString() { public @NonNull String toString() {
return stringAddrFromLongAddr(mAddr); return stringAddrFromLongAddr(mAddr);
} }
@@ -133,7 +134,7 @@ public final class MacAddress implements Parcelable {
* @return a String representation of the OUI part of this MacAddress made of 3 hexadecimal * @return a String representation of the OUI part of this MacAddress made of 3 hexadecimal
* numbers in [0,ff] joined by ':' characters. * numbers in [0,ff] joined by ':' characters.
*/ */
public String toOuiString() { public @NonNull String toOuiString() {
return String.format( return String.format(
"%02x:%02x:%02x", (mAddr >> 40) & 0xff, (mAddr >> 32) & 0xff, (mAddr >> 24) & 0xff); "%02x:%02x:%02x", (mAddr >> 40) & 0xff, (mAddr >> 32) & 0xff, (mAddr >> 24) & 0xff);
} }
@@ -197,7 +198,7 @@ public final class MacAddress implements Parcelable {
if (!isMacAddress(addr)) { if (!isMacAddress(addr)) {
return TYPE_UNKNOWN; return TYPE_UNKNOWN;
} }
return MacAddress.fromBytes(addr).addressType(); return MacAddress.fromBytes(addr).getAddressType();
} }
/** /**
@@ -211,7 +212,7 @@ public final class MacAddress implements Parcelable {
* *
* @hide * @hide
*/ */
public static byte[] byteAddrFromStringAddr(String addr) { public static @NonNull byte[] byteAddrFromStringAddr(String addr) {
Preconditions.checkNotNull(addr); Preconditions.checkNotNull(addr);
String[] parts = addr.split(":"); String[] parts = addr.split(":");
if (parts.length != ETHER_ADDR_LEN) { if (parts.length != ETHER_ADDR_LEN) {
@@ -239,7 +240,7 @@ public final class MacAddress implements Parcelable {
* *
* @hide * @hide
*/ */
public static String stringAddrFromByteAddr(byte[] addr) { public static @NonNull String stringAddrFromByteAddr(byte[] addr) {
if (!isMacAddress(addr)) { if (!isMacAddress(addr)) {
return null; return null;
} }
@@ -291,7 +292,7 @@ public final class MacAddress implements Parcelable {
// Internal conversion function equivalent to stringAddrFromByteAddr(byteAddrFromLongAddr(addr)) // Internal conversion function equivalent to stringAddrFromByteAddr(byteAddrFromLongAddr(addr))
// that avoids the allocation of an intermediary byte[]. // that avoids the allocation of an intermediary byte[].
private static String stringAddrFromLongAddr(long addr) { private static @NonNull String stringAddrFromLongAddr(long addr) {
return String.format("%02x:%02x:%02x:%02x:%02x:%02x", return String.format("%02x:%02x:%02x:%02x:%02x:%02x",
(addr >> 40) & 0xff, (addr >> 40) & 0xff,
(addr >> 32) & 0xff, (addr >> 32) & 0xff,
@@ -310,7 +311,7 @@ public final class MacAddress implements Parcelable {
* @return the MacAddress corresponding to the given String representation. * @return the MacAddress corresponding to the given String representation.
* @throws IllegalArgumentException if the given String is not a valid representation. * @throws IllegalArgumentException if the given String is not a valid representation.
*/ */
public static MacAddress fromString(String addr) { public static @NonNull MacAddress fromString(@NonNull String addr) {
return new MacAddress(longAddrFromStringAddr(addr)); return new MacAddress(longAddrFromStringAddr(addr));
} }
@@ -322,7 +323,7 @@ public final class MacAddress implements Parcelable {
* @return the MacAddress corresponding to the given byte array representation. * @return the MacAddress corresponding to the given byte array representation.
* @throws IllegalArgumentException if the given byte array is not a valid representation. * @throws IllegalArgumentException if the given byte array is not a valid representation.
*/ */
public static MacAddress fromBytes(byte[] addr) { public static @NonNull MacAddress fromBytes(@NonNull byte[] addr) {
return new MacAddress(longAddrFromByteAddr(addr)); return new MacAddress(longAddrFromByteAddr(addr));
} }
@@ -336,7 +337,7 @@ public final class MacAddress implements Parcelable {
* *
* @hide * @hide
*/ */
public static MacAddress createRandomUnicastAddress() { public static @NonNull MacAddress createRandomUnicastAddress() {
return createRandomUnicastAddress(BASE_GOOGLE_MAC, new Random()); return createRandomUnicastAddress(BASE_GOOGLE_MAC, new Random());
} }
@@ -352,7 +353,7 @@ public final class MacAddress implements Parcelable {
* *
* @hide * @hide
*/ */
public static MacAddress createRandomUnicastAddress(MacAddress base, Random r) { public static @NonNull MacAddress createRandomUnicastAddress(MacAddress base, Random r) {
long addr = (base.mAddr & OUI_MASK) | (NIC_MASK & r.nextLong()); long addr = (base.mAddr & OUI_MASK) | (NIC_MASK & r.nextLong());
addr = addr | LOCALLY_ASSIGNED_MASK; addr = addr | LOCALLY_ASSIGNED_MASK;
addr = addr & ~MULTICAST_MASK; addr = addr & ~MULTICAST_MASK;

View File

@@ -67,7 +67,7 @@ public class MacAddressTest {
assertEquals(msg, t.expectedType, got); assertEquals(msg, t.expectedType, got);
if (got != MacAddress.TYPE_UNKNOWN) { if (got != MacAddress.TYPE_UNKNOWN) {
assertEquals(got, MacAddress.fromBytes(t.addr).addressType()); assertEquals(got, MacAddress.fromBytes(t.addr).getAddressType());
} }
} }
} }
@@ -191,7 +191,7 @@ public class MacAddressTest {
assertTrue(stringRepr + " expected to be a locally assigned address", assertTrue(stringRepr + " expected to be a locally assigned address",
mac.isLocallyAssigned()); mac.isLocallyAssigned());
assertEquals(MacAddress.TYPE_UNICAST, mac.addressType()); assertEquals(MacAddress.TYPE_UNICAST, mac.getAddressType());
assertTrue(stringRepr + " expected to begin with " + expectedLocalOui, assertTrue(stringRepr + " expected to begin with " + expectedLocalOui,
stringRepr.startsWith(expectedLocalOui)); stringRepr.startsWith(expectedLocalOui));
} }