16 package org.openkilda.messaging.model;
20 import com.fasterxml.jackson.annotation.JsonValue;
21 import com.google.common.annotations.VisibleForTesting;
23 import java.io.Serializable;
24 import java.util.Objects;
29 public class SwitchId implements Serializable, Comparable<SwitchId> {
31 private static final long serialVersionUID = 1L;
36 private final long switchId;
44 value = Long.parseUnsignedLong(switchId.replaceAll(
"[-:]",
""), 16);
45 }
catch (NumberFormatException e) {
49 this.switchId =
value;
56 this.switchId = switchId;
74 return colonSeparatedBytes(toHexArray(), 4);
83 return colonSeparatedBytes(toHexArray(), 0);
92 return "SW" +
new String(toHexArray()).toUpperCase();
96 String colonSeparatedBytes(
char[] hex,
int offset) {
97 if (offset < 0 || offset % 2 != 0 || offset >= hex.length) {
98 throw new IllegalArgumentException(String.format(
99 "Illegal offset value %d (expect offset > 0 and offset %% 2 == 0 and offset < hex.length)", offset));
102 int length = hex.length - offset;
103 length += length / 2 - 1;
104 char[] buffer =
new char[length];
106 for (
int src = offset; src < hex.length; src++) {
107 if (offset < src && src % 2 == 0) {
110 buffer[dst++] = hex[src];
113 return new String(buffer);
116 private char[] toHexArray() {
117 String hexString = String.format(
"%016x", switchId);
118 return hexString.toCharArray();
130 if (o == null || getClass() != o.getClass()) {
136 return switchId == other.switchId;
144 return Objects.hash(switchId);
153 return Long.compareUnsigned(switchId, other.switchId);
SwitchId(String switchId)
int compareTo(SwitchId other)