summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/java/android/uwb/UwbAddress.java35
1 files changed, 27 insertions, 8 deletions
diff --git a/core/java/android/uwb/UwbAddress.java b/core/java/android/uwb/UwbAddress.java
index 48fcb10e1a1a..335e4fdf8c62 100644
--- a/core/java/android/uwb/UwbAddress.java
+++ b/core/java/android/uwb/UwbAddress.java
@@ -19,6 +19,8 @@ package android.uwb;
import android.annotation.NonNull;
import android.annotation.Nullable;
+import java.util.Arrays;
+
/**
* A class representing a UWB address
*
@@ -28,6 +30,12 @@ public final class UwbAddress {
public static final int SHORT_ADDRESS_BYTE_LENGTH = 2;
public static final int EXTENDED_ADDRESS_BYTE_LENGTH = 8;
+ private final byte[] mAddressBytes;
+
+ private UwbAddress(byte[] address) {
+ mAddressBytes = address;
+ }
+
/**
* Create a {@link UwbAddress} from a byte array.
*
@@ -37,12 +45,16 @@ public final class UwbAddress {
*
* @param address a byte array to convert to a {@link UwbAddress}
* @return a {@link UwbAddress} created from the input byte array
- * @throw IllegableArumentException when the length is not one of
+ * @throws IllegalArgumentException when the length is not one of
* {@link #SHORT_ADDRESS_BYTE_LENGTH} or {@link #EXTENDED_ADDRESS_BYTE_LENGTH} bytes
*/
@NonNull
- public static UwbAddress fromBytes(byte[] address) throws IllegalArgumentException {
- throw new UnsupportedOperationException();
+ public static UwbAddress fromBytes(@NonNull byte[] address) throws IllegalArgumentException {
+ if (address.length != SHORT_ADDRESS_BYTE_LENGTH
+ && address.length != EXTENDED_ADDRESS_BYTE_LENGTH) {
+ throw new IllegalArgumentException("Invalid UwbAddress length " + address.length);
+ }
+ return new UwbAddress(address);
}
/**
@@ -52,7 +64,7 @@ public final class UwbAddress {
*/
@NonNull
public byte[] toBytes() {
- throw new UnsupportedOperationException();
+ return mAddressBytes;
}
/**
@@ -61,22 +73,29 @@ public final class UwbAddress {
* {@link #EXTENDED_ADDRESS_BYTE_LENGTH}.
*/
public int size() {
- throw new UnsupportedOperationException();
+ return mAddressBytes.length;
}
@NonNull
@Override
public String toString() {
- throw new UnsupportedOperationException();
+ StringBuilder builder = new StringBuilder("0x");
+ for (byte addressByte : mAddressBytes) {
+ builder.append(String.format("%02X", addressByte));
+ }
+ return builder.toString();
}
@Override
public boolean equals(@Nullable Object obj) {
- throw new UnsupportedOperationException();
+ if (obj instanceof UwbAddress) {
+ return Arrays.equals(mAddressBytes, ((UwbAddress) obj).toBytes());
+ }
+ return false;
}
@Override
public int hashCode() {
- throw new UnsupportedOperationException();
+ return Arrays.hashCode(mAddressBytes);
}
}