2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.lifx.internal.fields;
15 import java.nio.ByteBuffer;
16 import java.util.LinkedList;
17 import java.util.List;
18 import java.util.Objects;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.util.HexUtils;
25 * @author Tim Buckley - Initial contribution
26 * @author Karel Goderis - Initial contribution
29 public class MACAddress {
31 public static final MACAddress BROADCAST_ADDRESS = new MACAddress("000000000000");
33 private ByteBuffer bytes;
34 private String hex = "";
36 public ByteBuffer getBytes() {
40 public String getHex() {
44 public MACAddress(ByteBuffer bytes) {
50 public MACAddress(String string) {
51 byte[] byteArray = HexUtils.hexToBytes(string);
52 this.bytes = ByteBuffer.wrap(byteArray);
53 this.hex = HexUtils.bytesToHex(byteArray, ":");
57 this(ByteBuffer.allocate(6));
60 private void createHex() {
63 List<String> byteStrings = new LinkedList<>();
64 while (bytes.hasRemaining()) {
65 byteStrings.add(String.format("%02X", bytes.get()));
68 hex = String.join(":", byteStrings);
73 public String getAsLabel() {
76 StringBuilder hex = new StringBuilder();
77 while (bytes.hasRemaining()) {
78 hex.append(String.format("%02X", bytes.get()));
83 return hex.toString();
87 public int hashCode() {
89 hash = 97 * hash + Objects.hashCode(this.hex);
94 public boolean equals(@Nullable Object obj) {
99 if (getClass() != obj.getClass()) {
103 final MACAddress other = (MACAddress) obj;
104 return this.hex.equalsIgnoreCase(other.hex);