]> git.basschouten.com Git - openhab-addons.git/blob
ae61204da5cbfae905a7b5be2bbd17afca58f0d0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.bluetooth;
14
15 /**
16  * The {@link BluetoothAddress} class defines a bluetooth address
17  *
18  * @author Chris Jackson - Initial contribution
19  */
20 public class BluetoothAddress {
21
22     public static final int BD_ADDRESS_LENGTH = 17;
23
24     private final String address;
25
26     /**
27      * The default constructor
28      *
29      * @param address the device address
30      */
31     public BluetoothAddress(String address) {
32         if (address == null || address.length() != BD_ADDRESS_LENGTH) {
33             throw new IllegalArgumentException("BT Address cannot be null and must be in format XX:XX:XX:XX:XX:XX");
34         }
35         for (int i = 0; i < BD_ADDRESS_LENGTH; i++) {
36             char c = address.charAt(i);
37
38             // Check address - 2 bytes should be hex, and then a colon
39             switch (i % 3) {
40                 case 0: // fall through
41                 case 1:
42                     if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
43                         break;
44                     }
45                     throw new IllegalArgumentException("BT Address must contain upper case hex values only");
46                 case 2:
47                     if (c == ':') {
48                         break;
49                     }
50                     throw new IllegalArgumentException("BT Address bytes must be separated with colon");
51             }
52         }
53
54         this.address = address;
55     }
56
57     @Override
58     public int hashCode() {
59         final int prime = 31;
60         int result = 1;
61         result = prime * result + ((address == null) ? 0 : address.hashCode());
62         return result;
63     }
64
65     @Override
66     public boolean equals(Object obj) {
67         if (this == obj) {
68             return true;
69         }
70         if (obj == null) {
71             return false;
72         }
73         if (getClass() != obj.getClass()) {
74             return false;
75         }
76         BluetoothAddress other = (BluetoothAddress) obj;
77         if (address == null) {
78             if (other.address != null) {
79                 return false;
80             }
81         } else if (!address.equals(other.address)) {
82             return false;
83         }
84         return true;
85     }
86
87     @Override
88     public String toString() {
89         return address;
90     }
91 }