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.nobohub.internal.model;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.nobohub.internal.NoboHubBindingConstants;
23 * Nobø serial numbers are 12 digits where 3 and 3 digits form 2 bytes as decimal. In total 32 bits.
25 * @author Jørgen Austvik - Initial contribution
26 * @author Espen Fossen - Initial contribution
29 public final class SerialNumber {
31 private final String serialNumber;
33 public SerialNumber(String serialNumber) {
34 this.serialNumber = serialNumber.trim();
37 public boolean isWellFormed() {
38 if (serialNumber.length() != 12) {
42 List<String> parts = new ArrayList<>(4);
43 for (int i = 0; i < 4; i++) {
44 parts.add(serialNumber.substring((i * 3), (i * 3) + 3));
47 if (parts.size() != 4) {
51 for (String part : parts) {
53 int num = Integer.parseInt(part);
54 if (num < 0 || num > 255) {
57 } catch (NumberFormatException nfe) {
66 * Returns the type string.
68 public String getTypeIdentifier() {
69 if (!isWellFormed()) {
73 return serialNumber.substring(0, 3);
77 * Returns the type of this component.
79 public String getComponentType() {
80 String id = getTypeIdentifier();
81 String type = getTypeForSerialNumber(id);
86 return "Unknown, please contact maintainer to add a new type for " + serialNumber;
89 private @Nullable String getTypeForSerialNumber(String id) {
90 return NoboHubBindingConstants.SERIALNUMBERS_FOR_TYPES.get(id);
94 public String toString() {
99 public boolean equals(@Nullable Object obj) {
104 if (obj == null || obj.getClass() != this.getClass()) {
108 SerialNumber other = (SerialNumber) obj;
109 return this.serialNumber.equals(other.serialNumber);
113 public int hashCode() {
114 return this.serialNumber.hashCode();