-
Notifications
You must be signed in to change notification settings - Fork 3
/
URL.java
692 lines (624 loc) · 25.7 KB
/
URL.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
package org.sputnikdev.bluetooth;
/*-
* #%L
* org.sputnikdev:bluetooth-utils
* %%
* Copyright (C) 2017 Sputnik Dev
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* Class {@code URL} represents a Uniform Resource Locator for bluetooth resources,
* e.g. bluetooth adapters, bluetooth devices, GATT services, GATT characteristics and GATT fields.
* For example, if you have an adapter with MAC address B8:27:EB:60:0C:43,
* a device with MAC address 54:60:09:95:86:01 is connected to the adapter,
* the device has a GATT service with UUID 0000180f-0000-1000-8000-00805f9b34fb,
* the service has a characteristic with UUID 00002a19-0000-1000-8000-00805f9b34fb
* and the characteristic has a field called "Level", then a URL for the field can be:
* /B8:27:EB:60:0C:43/54:60:09:95:86:01/0000180f-0000-1000-8000-00805f9b34fb/00002a19-0000-1000-8000-00805f9b34fb/Level
* Or a URL for the service can be:
* /B8:27:EB:60:0C:43/54:60:09:95:86:01/0000180f-0000-1000-8000-00805f9b34fb
* Similarly, it is easy to define a URL for other components, e.g. adapters, devices and characteristics.
*
* <p>If there are more than one protocol used to access Bluetooth devices (e.g. DBus, serial interface etc),
* then it is possible to define protocol as well:
* tinyb://B8:27:EB:60:0C:43/54:60:09:95:86:01/0000180f-0000-1000-8000-00805f9b34fb/
* 00002a19-0000-1000-8000-00805f9b34fb/Level
*
* @author Vlad Kolotov
*/
public class URL implements Comparable<URL> {
private static final Pattern URL_PATTERN =
Pattern.compile("^((?<protocol>\\w*):)?/(?<adapter>(\\w\\w:){5}\\w\\w)?"
+ "(/(?<device>(\\w\\w:){5}\\w\\w)?(\\[name=(?<devicename>[\\w\\s'-]+)\\])?)?"
+ "(/(?<service>[0-9A-Fa-f]{4,8}(-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12})?))?"
+ "(/(?<charact>[0-9A-Fa-f]{4,8}(-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12})?))?"
+ "(/(?<field>\\w+))?$");
public static final URL ROOT = new URL("/");
private final String protocol;
private final String adapterAddress;
private final String deviceAddress;
private final Map<String, String> deviceAttributes = new HashMap<>();
private final String serviceUUID;
private final String characteristicUUID;
private final String fieldName;
/**
* Constructor to build a URL object from its text representation.
* E.g. / bluetooth adapter / bluetooth device / GATT service / GATT characteristic / Characteristic Field Name
*
* <p>The following are some examples of a valid URL:
*
* <p>URL pointing to a field:
* /B8:27:EB:60:0C:43/54:60:09:95:86:01/0000180f-0000-1000-8000-00805f9b34fb/
* 00002a19-0000-1000-8000-00805f9b34fb/Level
*
* <p>URL pointing to a characteristic:
* /B8:27:EB:60:0C:43/54:60:09:95:86:01/0000180f-0000-1000-8000-00805f9b34fb/00002a19-0000-1000-8000-00805f9b34fb
*
* <p>URL pointing to a service:
* /B8:27:EB:60:0C:43/54:60:09:95:86:01/0000180f-0000-1000-8000-00805f9b34fb
*
* <p>URL pointing to a bluetooth device:
* /B8:27:EB:60:0C:43/54:60:09:95:86:01
*
* <p>URL point to a bluetooth adapter:
* /B8:27:EB:60:0C:43
*
* <p>URL pointing to the "root":
* /
*
* <p>Services and characteristic UUIDs can be in a short form:
* /B8:27:EB:60:0C:43/54:60:09:95:86:01/0000180f/00002a19/Level
* /B8:27:EB:60:0C:43/54:60:09:95:86:01/180f/2a19/Level
*
*
* @param url text representation of a URL
*/
public URL(String url) {
Matcher matcher = URL_PATTERN.matcher(url);
if (matcher.find()) {
protocol = toLowerCase(matcher.group("protocol"));
adapterAddress = toUpperCase(matcher.group("adapter"));
deviceAddress = toUpperCase(matcher.group("device"));
String deviceName = matcher.group("devicename");
if (deviceName != null) {
deviceAttributes.put("name", deviceName);
}
serviceUUID = toLowerCase(matcher.group("service"));
characteristicUUID = toLowerCase(matcher.group("charact"));
fieldName = matcher.group("field");
validate();
} else {
throw new IllegalArgumentException("Invalid URL: " + url);
}
}
/**
* Constructs the "root" URL object. All components of the such URL are nulls.
* A text representation of the "root" URL is "/"
*/
public URL() {
this(null, null);
}
/**
* Constructs a URL pointing to a bluetooth device.
* @param adapterAddress bluetooth adapter MAC address
* @param deviceAddress bluetooth device MAC address
*/
public URL(String adapterAddress, String deviceAddress) {
this(adapterAddress, deviceAddress, null, null, null);
}
/**
* Constructs a URL pointing to a GATT service.
* @param protocol protocol name
* @param adapterAddress bluetooth adapter MAC address
* @param deviceAddress bluetooth device MAC address
*/
public URL(String protocol, String adapterAddress, String deviceAddress) {
this(protocol, adapterAddress, deviceAddress, null, null, null);
}
/**
* Constructs a URL pointing to a GATT characteristic.
* @param adapterAddress bluetooth adapter MAC address
* @param deviceAddress bluetooth device MAC address
* @param serviceUUID UUID of a GATT service
* @param characteristicUUID UUID of a GATT characteristic
*/
public URL(String adapterAddress, String deviceAddress, String serviceUUID, String characteristicUUID) {
this(adapterAddress, deviceAddress, serviceUUID, characteristicUUID, null);
}
/**
* Constructs a URL pointing to a GATT field.
* @param adapterAddress bluetooth adapter MAC address
* @param deviceAddress bluetooth device MAC address
* @param serviceUUID UUID of a GATT service
* @param characteristicUUID UUID of a GATT characteristic
* @param fieldName name of a field of the characteristic
*/
public URL(String adapterAddress, String deviceAddress, String serviceUUID, String characteristicUUID,
String fieldName) {
this(null, adapterAddress, deviceAddress, serviceUUID, characteristicUUID, fieldName);
}
/**
* Constructs a URL pointing to a GATT field.
* @param protocol protocol name
* @param adapterAddress bluetooth adapter MAC address
* @param deviceAddress bluetooth device MAC address
* @param serviceUUID UUID of a GATT service
* @param characteristicUUID UUID of a GATT characteristic
* @param fieldName name of a field of the characteristic
*/
public URL(String protocol, String adapterAddress, String deviceAddress, String serviceUUID,
String characteristicUUID, String fieldName) {
this.protocol = toLowerCase(protocol);
this.adapterAddress = toUpperCase(adapterAddress);
this.deviceAddress = toUpperCase(deviceAddress);
this.serviceUUID = toLowerCase(serviceUUID);
this.characteristicUUID = toLowerCase(characteristicUUID);
this.fieldName = fieldName;
validate();
}
/**
* Copy constructor.
* @param protocol protocol name
* @param adapterAddress bluetooth adapter MAC address
* @param deviceAttributes bluetooth device attributes
* @param serviceUUID UUID of a GATT service
* @param characteristicUUID UUID of a GATT characteristic
* @param fieldName name of a field of the characteristic
*/
public URL(String protocol, String adapterAddress, Map<String, String> deviceAttributes,
String serviceUUID, String characteristicUUID, String fieldName) {
this.protocol = toLowerCase(protocol);
this.adapterAddress = toUpperCase(adapterAddress);
this.deviceAddress = null;
if (deviceAttributes != null && !deviceAttributes.isEmpty()) {
this.deviceAttributes.putAll(deviceAttributes);
}
this.serviceUUID = toLowerCase(serviceUUID);
this.characteristicUUID = toLowerCase(characteristicUUID);
this.fieldName = fieldName;
validate();
}
URL(String protocol, String adapterAddress, String deviceAddress, Map<String, String> deviceAttributes,
String serviceUUID, String characteristicUUID, String fieldName) {
this.protocol = toLowerCase(protocol);
this.adapterAddress = toUpperCase(adapterAddress);
this.deviceAddress = toUpperCase(deviceAddress);
if (deviceAttributes != null && !deviceAttributes.isEmpty()) {
this.deviceAttributes.putAll(deviceAttributes);
}
this.serviceUUID = toLowerCase(serviceUUID);
this.characteristicUUID = toLowerCase(characteristicUUID);
this.fieldName = fieldName;
validate();
}
/**
* Makes a copy of a given URL with some additional components.
* @param protocol protocol name
* @return a copy of a given URL with some additional components
*/
public URL copyWithProtocol(String protocol) {
return new URL(protocol, this.adapterAddress, this.deviceAddress, this.deviceAttributes, this.serviceUUID,
this.characteristicUUID, this.fieldName);
}
/**
* Makes a copy of a given URL with some additional components.
* @param adapterAddress bluetooth adapter MAC address
* @return a copy of a given URL with some additional components
*/
public URL copyWithAdapter(String adapterAddress) {
return new URL(this.protocol, adapterAddress, this.deviceAddress, this.deviceAttributes, this.serviceUUID,
this.characteristicUUID, this.fieldName);
}
/**
* Makes a copy of a given URL with some additional components.
* @param deviceAddress bluetooth device MAC address
* @return a copy of a given URL with some additional components
*/
public URL copyWithDevice(String deviceAddress) {
return new URL(this.protocol, this.adapterAddress, deviceAddress, this.deviceAttributes, this.serviceUUID,
this.characteristicUUID, this.fieldName);
}
/**
* Makes a copy of a given URL with some additional components.
* @param deviceAddress bluetooth device MAC address
* @param attr device attributes
* @return a copy of a given URL with some additional components
*/
public URL copyWithDevice(String deviceAddress, Map<String, String> attr) {
return new URL(this.protocol, this.adapterAddress, deviceAddress, attr, this.serviceUUID,
this.characteristicUUID, this.fieldName);
}
/**
* Makes a copy of a given URL with a new device name and a single attribute.
* @param deviceAddress bluetooth device MAC address
* @param attrName attribute name
* @param attrValue attribute value
* @return a copy of a given URL with some additional components
*/
public URL copyWithDevice(String deviceAddress, String attrName, String attrValue) {
return new URL(this.protocol, this.adapterAddress, deviceAddress, Collections.singletonMap(attrName, attrValue),
this.serviceUUID, this.characteristicUUID, this.fieldName);
}
/**
* Makes a copy of a given URL with some additional components.
* @param serviceUUID UUID of a GATT service
* @return a copy of a given URL with some additional components
*/
public URL copyWithService(String serviceUUID) {
return new URL(this.protocol, this.adapterAddress, this.deviceAddress, this.deviceAttributes,
serviceUUID, null, null);
}
/**
* Makes a copy of a given URL with some additional components.
* @param characteristicUUID UUID of a GATT characteristic
* @return a copy of a given URL with some additional components
*/
public URL copyWithCharacteristic(String characteristicUUID) {
return new URL(this.protocol, this.adapterAddress, this.deviceAddress, this.deviceAttributes,
serviceUUID, characteristicUUID, null);
}
/**
* Makes a copy of a given URL with some additional components.
* @param serviceUUID UUID of a GATT service
* @param characteristicUUID UUID of a GATT characteristic
* @return a copy of a given URL with some additional components
*/
public URL copyWith(String serviceUUID, String characteristicUUID) {
return new URL(this.protocol, this.adapterAddress, this.deviceAddress, this.deviceAttributes,
serviceUUID, characteristicUUID, null);
}
/**
* Makes a copy of a given URL with some additional components.
* @param serviceUUID UUID of a GATT service
* @param characteristicUUID UUID of a GATT characteristic
* @param fieldName name of a field of the characteristic
* @return a copy of a given URL with some additional components
*/
public URL copyWith(String serviceUUID, String characteristicUUID, String fieldName) {
return new URL(this.protocol, this.adapterAddress, this.deviceAddress, this.deviceAttributes, serviceUUID,
characteristicUUID, fieldName);
}
/**
* Makes a copy of a given URL with some additional components.
* @param fieldName name of a field of the characteristic
* @return a copy of a given URL with some additional components
*/
public URL copyWithField(String fieldName) {
return new URL(this.protocol, this.adapterAddress, this.deviceAddress, this.deviceAttributes, this.serviceUUID,
this.characteristicUUID, fieldName);
}
/**
* Returns a copy of a given URL truncated to its device component.
* Service, characteristic and field components will be null.
* @return a copy of a given URL truncated to the device component
*/
public URL getDeviceURL() {
return new URL(this.protocol, this.adapterAddress, this.deviceAddress, this.deviceAttributes, null, null, null);
}
public Map<String, String> getDeviceAttributes() {
return Collections.unmodifiableMap(deviceAttributes);
}
public String getDeviceName() {
return deviceAttributes.get("name");
}
/**
* Returns a copy of a given URL truncated to its service component.
* Characteristic and field components will be null.
* @return a copy of a given URL truncated to the service component
*/
public URL getServiceURL() {
return new URL(this.protocol, this.adapterAddress, this.deviceAddress, this.deviceAttributes,
this.serviceUUID, null, null);
}
/**
* Returns a copy of a given URL truncated to its characteristic component.
* The field component will be null.
* @return a copy of a given URL truncated to the service component
*/
public URL getCharacteristicURL() {
return new URL(this.protocol, this.adapterAddress, this.deviceAddress, this.deviceAttributes, this.serviceUUID,
this.characteristicUUID, null);
}
/**
* Returns a copy of a given URL truncated to its adapter component.
* Device, Service, characteristic and field components will be null.
* @return a copy of a given URL truncated to the adapter component
*/
public URL getAdapterURL() {
return new URL(this.protocol, this.adapterAddress, null);
}
/**
* Returns a copy of a given URL truncated to its protocol component.
* Adapter, Device, Service, characteristic and field components will be null.
* @return a copy of a given URL truncated to the adapter component
*/
public URL getProtocolURL() {
return new URL(this.protocol, null, null);
}
/**
* Returns bluetooth protocol.
* @return bluetooth protocol
*/
public String getProtocol() {
return protocol;
}
/**
* Returns bluetooth adapter address (MAC address).
* @return adapter address (MAC address)
*/
public String getAdapterAddress() {
return adapterAddress;
}
/**
* Returns bluetooth device address.
* @return bluetooth device address
*/
public String getDeviceAddress() {
return deviceAddress;
}
/**
* Returns bluetooth device composite address (a combination of the address and its attributes).
* @return bluetooth device composite address
*/
public String getDeviceCompositeAddress() {
String device = deviceAddress;
if (!deviceAttributes.isEmpty()) {
device = (device != null ? device : "") + deviceAttributes.entrySet().stream()
.map(Map.Entry::toString).collect(Collectors.joining(",", "[", "]"));
}
return device;
}
/**
* Returns service UUID.
* @return service UUID
*/
public String getServiceUUID() {
return serviceUUID;
}
/**
* Return characteristic UUID.
* @return characteristic UUID
*/
public String getCharacteristicUUID() {
return characteristicUUID;
}
/**
* Returns field name.
* @return field name
*/
public String getFieldName() {
return fieldName;
}
/**
* Checks whether a given URL is the "root" URL object.
* @return true if it is the "root" URL, false otherwise
*/
public boolean isProtocol() {
return protocol != null && adapterAddress == null;
}
/**
* Checks whether a given URL is the "root" URL object.
* @return true if it is the "root" URL, false otherwise
*/
public boolean isRoot() {
return adapterAddress == null;
}
/**
* Checks whether a given URL is pointing to a bluetooth adapter.
* @return true if a given URL is pointing to a bluetooth adapter, false otherwise
*/
public boolean isAdapter() {
return adapterAddress != null && deviceAddress == null && deviceAttributes.isEmpty();
}
/**
* Checks whether a given URL is pointing to a bluetooth device.
* @return true if a given URL is pointing to a bluetooth device, false otherwise
*/
public boolean isDevice() {
return (deviceAddress != null || !deviceAttributes.isEmpty()) && serviceUUID == null;
}
/**
* Checks whether a given URL is pointing to a GATT service.
* @return true if a given URL is pointing to a GATT service, false otherwise
*/
public boolean isService() {
return serviceUUID != null && characteristicUUID == null;
}
/**
* Checks whether a given URL is pointing to a GATT characteristic.
* @return true if a given URL is pointing to a GATT characteristic, false otherwise
*/
public boolean isCharacteristic() {
return characteristicUUID != null && fieldName == null;
}
/**
* Checks whether a given URL is pointing to a field of a characteristic.
* @return true if a given URL is pointing to a field of a characteristic, false otherwise
*/
public boolean isField() {
return fieldName != null;
}
/**
* Returns a new URL representing its "parent" component.
* @return a new URL representing its "parent" component
*/
public URL getParent() {
if (isField()) {
return getCharacteristicURL();
} else if (isCharacteristic()) {
return getServiceURL();
} else if (isService()) {
return getDeviceURL();
} else if (isDevice()) {
return getAdapterURL();
} else if (isAdapter()) {
return getProtocolURL();
} else {
return null;
}
}
/**
* Checks whether the URL is a descendant of a URL provided as an argument.
* If the provided URL does not specify protocol, then it will match any protocol of the URL
* @param url a bluetooth URL
* @return true if the URL is a descendant of a provided URL
*/
public boolean isDescendant(URL url) {
if (url.protocol != null && protocol != null && !protocol.equals(url.protocol)) {
return false;
}
if (adapterAddress != null && url.adapterAddress == null) {
return true;
}
if (adapterAddress != null && !adapterAddress.equals(url.adapterAddress)) {
return false;
}
if (deviceAddress != null && url.deviceAddress == null) {
return true;
}
if (deviceAddress != null && !deviceAddress.equals(url.deviceAddress)) {
return false;
}
if (serviceUUID != null && url.serviceUUID == null) {
return true;
}
if (serviceUUID != null ? !serviceUUID.equals(url.serviceUUID) : url.serviceUUID != null) {
return false;
}
if (characteristicUUID != null && url.characteristicUUID == null) {
return true;
}
if (characteristicUUID != null
? !characteristicUUID.equals(url.characteristicUUID) :
url.characteristicUUID != null) {
return false;
}
return fieldName != null && url.fieldName == null;
}
@Override
public String toString() {
LinkedList<String> fields = new LinkedList<>();
String deviceAddress = getDeviceCompositeAddress();
if (adapterAddress == null && deviceAddress != null) {
fields.add("");
} else {
fields.add(adapterAddress);
}
fields.add(deviceAddress);
fields.add(serviceUUID);
fields.add(characteristicUUID);
fields.add(fieldName);
while (!fields.isEmpty() && fields.getLast() == null) {
fields.removeLast();
}
return (protocol != null ? protocol + ":" : "") + "/" + String.join("/", fields);
}
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null || getClass() != that.getClass()) {
return false;
}
URL url = (URL) that;
if (protocol != null ? !protocol.equals(url.protocol) : url.protocol != null) {
return false;
}
if (adapterAddress != null ? !adapterAddress.equals(url.adapterAddress) : url.adapterAddress != null) {
return false;
}
if (deviceAddress != null ? !deviceAddress.equals(url.deviceAddress) : url.deviceAddress != null) {
return false;
}
if (!deviceAttributes.equals(url.deviceAttributes)) {
return false;
}
if (serviceUUID != null ? !serviceUUID.equals(url.serviceUUID) : url.serviceUUID != null) {
return false;
}
if (characteristicUUID != null
? !characteristicUUID.equals(url.characteristicUUID) :
url.characteristicUUID != null) {
return false;
}
return fieldName != null && url.fieldName != null
? fieldName.toLowerCase().equals(url.fieldName.toLowerCase()) : fieldName == url.fieldName;
}
@Override
public int hashCode() {
int result = protocol != null ? protocol.hashCode() : 0;
result = 31 * result + (adapterAddress != null ? adapterAddress.hashCode() : 0);
result = 31 * result + (deviceAddress != null ? deviceAddress.hashCode() : 0);
result = 31 * result + deviceAttributes.hashCode();
result = 31 * result + (serviceUUID != null ? serviceUUID.hashCode() : 0);
result = 31 * result + (characteristicUUID != null ? characteristicUUID.hashCode() : 0);
result = 31 * result + (fieldName != null ? fieldName.toLowerCase().hashCode() : 0);
return result;
}
@Override
public int compareTo(URL that) {
int result = compareFields(adapterAddress, that.adapterAddress);
if (result != 0) {
return result;
}
result = compareFields(deviceAddress, that.deviceAddress);
if (result != 0) {
return result;
}
//TODO add device attributes comparison
result = compareFields(serviceUUID, that.serviceUUID);
if (result != 0) {
return result;
}
result = compareFields(characteristicUUID, that.characteristicUUID);
if (result != 0) {
return result;
}
return compareFields(fieldName, that.fieldName);
}
private void validate() {
if (fieldName != null && characteristicUUID == null
|| characteristicUUID != null && serviceUUID == null
|| serviceUUID != null && (deviceAddress == null && deviceAttributes.isEmpty())) {
throw new IllegalArgumentException("Invalid url: " + toString());
}
}
private int compareFields(String field1, String field2) {
if (field1 == null && field2 == null) {
return 0;
} else if (field2 == null) {
return 1;
} else if (field1 == null) {
return -1;
} else {
return field1.compareTo(field2);
}
}
private static String toUpperCase(String str) {
return Optional.ofNullable(str).map(String::toUpperCase).orElse(null);
}
private static String toLowerCase(String str) {
return Optional.ofNullable(str).map(String::toLowerCase).orElse(null);
}
}