+ /**
+ * Create a MacAddress object holding the canonical representation of the 6 bytes
+ * passed in as argument.
+ * @param bytes 6-byte input array
+ * @return MacAddress with canonical string derived from input bytes
+ * @throws NullPointerException if bytes is null
+ * @throws IllegalArgumentException if length of input is not 6 bytes
+ */
+ public final @NonNull M macAddressFor(final byte @NonNull[] bytes) {
+ checkArgument(bytes.length == MAC_BYTE_LENGTH, "MAC address should have 6 bytes, not %s",
+ bytes.length);
+ return macFactory.newInstance(bytesToString(bytes, 17));
+ }
+
+ public final byte @NonNull[] macAddressBytes(final @NonNull M macAddress) {
+ return stringToBytes(getValue(macAddress), MAC_BYTE_LENGTH);
+ }
+
+ /**
+ * Convert the value of a PhysAddress into the canonical representation.
+ *
+ * @param physAddress Input MAC address
+ * @return A PhysAddress containing the canonical representation.
+ * @throws NullPointerException if physAddress is null
+ */
+ public final @NonNull P canonizePhysAddress(final @NonNull P physAddress) {
+ final char[] input = getPhysValue(physAddress).toCharArray();
+ return ensureLowerCase(input) ? physFactory.newInstance(String.valueOf(input)) : physAddress;
+ }
+
+ /**
+ * Create a PhysAddress object holding the canonical representation of the bytes passed in as argument.
+ *
+ * @param bytes input array
+ * @return PhysAddress with canonical string derived from input bytes
+ * @throws NullPointerException if bytes is null
+ * @throws IllegalArgumentException if length of input is not at least 1 byte
+ */
+ public final @NonNull P physAddressFor(final byte @NonNull[] bytes) {
+ checkArgument(bytes.length > 0, "Physical address should have at least one byte");
+ return physFactory.newInstance(bytesToString(bytes, bytes.length * 3 - 1));
+ }
+
+ public final byte @NonNull[] physAddressBytes(final @NonNull P physAddress) {
+ final String str = getPhysValue(physAddress);
+ return str.isEmpty() ? EMPTY_BYTES : stringToBytes(str, str.length() / 3 + 1);
+ }
+
+ @Deprecated
+ public final byte @NonNull[] bytesFor(final @NonNull M macAddress) {
+ return macAddressBytes(macAddress);
+ }
+
+ public final @NonNull H hexStringFor(final byte @NonNull[] bytes) {
+ checkArgument(bytes.length > 0, "Hex string should have at least one byte");
+ return hexFactory.newInstance(bytesToString(bytes, bytes.length * 3 - 1));
+ }
+
+ public final byte @NonNull[] hexStringBytes(final @NonNull H hexString) {
+ final String str = getHexValue(hexString);
+ return stringToBytes(str, str.length() / 3 + 1);
+ }
+
+ public final @NonNull Q dottedQuadFor(final byte @NonNull[] bytes) {
+ checkArgument(bytes.length == 4, "Dotted-quad should have 4 bytes");
+ return quadFactory.newInstance(AbstractIetfInetUtil.addressStringV4(bytes));
+ }
+
+ public final byte @NonNull[] dottedQuadBytes(final @NonNull Q hexString) {
+ final String str = getQuadValue(hexString);
+ final byte[] bytes = new byte[4];
+ Ipv4Utils.fillIpv4Bytes(bytes, 0, str, 0, str.length());
+ return bytes;
+ }
+
+ public final @NonNull U uuidFor(final @NonNull UUID uuid) {
+ return uuidFactory.newInstance(uuid.toString());
+ }
+
+ protected abstract String getValue(M macAddress);
+
+ protected abstract String getPhysValue(P physAddress);
+
+ protected abstract String getHexValue(H hexString);
+
+ protected abstract String getQuadValue(Q dottedQuad);
+
+ static byte hexValue(final char ch) {
+ byte value;
+ try {
+ // Performance optimization: access the array and rely on the VM for catching
+ // illegal access (which boils down to illegal character, which should never happen.
+ value = HEX_VALUES[ch];
+ } catch (IndexOutOfBoundsException e) {
+ value = -1;
+ }
+
+ if (value < 0) {
+ throw new IllegalArgumentException("Invalid character '" + ch + "' encountered");
+ }
+
+ return value;