Skip to content

Commit bc8bd0b

Browse files
authored
Merge pull request #591 from NordicSemiconductor/improvement/java17
Migration `instanceof` to pattern matching from Java 17
2 parents b923e37 + b7c841f commit bc8bd0b

File tree

1 file changed

+57
-59
lines changed

1 file changed

+57
-59
lines changed

ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,18 +1671,18 @@ final void cancelCurrent() {
16711671
return;
16721672

16731673
log(Log.WARN, () -> "Request cancelled");
1674-
if (request instanceof TimeoutableRequest) {
1675-
request.notifyFail(device, FailCallback.REASON_CANCELLED);
1674+
if (request instanceof final TimeoutableRequest r) {
1675+
r.notifyFail(device, FailCallback.REASON_CANCELLED);
16761676
}
16771677
if (awaitingRequest != null) {
16781678
awaitingRequest.notifyFail(device, FailCallback.REASON_CANCELLED);
16791679
awaitingRequest = null;
16801680
}
1681-
if (requestQueue instanceof ReliableWriteRequest) {
1681+
if (requestQueue instanceof final ReliableWriteRequest rwr) {
16821682
// Cancelling a Reliable Write request requires sending Abort command.
16831683
// Instead of notifying failure, we will remove all enqueued tasks and
16841684
// let the nextRequest to sent Abort command.
1685-
requestQueue.notifyAndCancelQueue(device);
1685+
rwr.notifyAndCancelQueue(device);
16861686
} else if (requestQueue != null) {
16871687
requestQueue.notifyFail(device, FailCallback.REASON_CANCELLED);
16881688
requestQueue = null;
@@ -1692,13 +1692,13 @@ final void cancelCurrent() {
16921692

16931693
@Override
16941694
final void onRequestTimeout(@NonNull final BluetoothDevice device, @NonNull final TimeoutableRequest tr) {
1695-
if (tr instanceof SleepRequest) {
1696-
tr.notifySuccess(device);
1695+
if (tr instanceof final SleepRequest sr) {
1696+
sr.notifySuccess(device);
16971697
} else {
16981698
log(Log.WARN, () -> "Request timed out");
16991699
}
1700-
if (request instanceof TimeoutableRequest) {
1701-
request.notifyFail(device, FailCallback.REASON_TIMEOUT);
1700+
if (request instanceof final TimeoutableRequest r) {
1701+
r.notifyFail(device, FailCallback.REASON_TIMEOUT);
17021702
}
17031703
if (awaitingRequest != null) {
17041704
awaitingRequest.notifyFail(device, FailCallback.REASON_TIMEOUT);
@@ -2523,8 +2523,8 @@ public void onCharacteristicRead(@NonNull final BluetoothGatt gatt,
25232523
return;
25242524
} else {
25252525
Log.e(TAG, "onCharacteristicRead error " + status);
2526-
if (request instanceof ReadRequest) {
2527-
request.notifyFail(gatt.getDevice(), status);
2526+
if (request instanceof final ReadRequest rr) {
2527+
rr.notifyFail(gatt.getDevice(), status);
25282528
}
25292529
awaitingRequest = null;
25302530
onError(gatt.getDevice(), ERROR_READ_CHARACTERISTIC, status);
@@ -2548,9 +2548,9 @@ public void onCharacteristicWrite(final BluetoothGatt gatt,
25482548
// This method also compares the data written with the data received in the callback
25492549
// if the write type is WRITE_TYPE_DEFAULT.
25502550
final boolean valid = wr.notifyPacketSent(gatt.getDevice(), characteristic.getValue());
2551-
if (!valid && requestQueue instanceof ReliableWriteRequest) {
2551+
if (!valid && requestQueue instanceof final ReliableWriteRequest rwr) {
25522552
wr.notifyFail(gatt.getDevice(), FailCallback.REASON_VALIDATION);
2553-
requestQueue.notifyAndCancelQueue(gatt.getDevice());
2553+
rwr.notifyAndCancelQueue(gatt.getDevice());
25542554
} else if (wr.hasMore()) {
25552555
enqueueFirst(wr);
25562556
} else {
@@ -2570,11 +2570,11 @@ public void onCharacteristicWrite(final BluetoothGatt gatt,
25702570
return;
25712571
} else {
25722572
Log.e(TAG, "onCharacteristicWrite error " + status);
2573-
if (request instanceof WriteRequest) {
2574-
request.notifyFail(gatt.getDevice(), status);
2573+
if (request instanceof final WriteRequest wr) {
2574+
wr.notifyFail(gatt.getDevice(), status);
25752575
// Automatically abort Reliable Write when write error happen
2576-
if (requestQueue instanceof ReliableWriteRequest)
2577-
requestQueue.notifyAndCancelQueue(gatt.getDevice());
2576+
if (requestQueue instanceof final ReliableWriteRequest rwr)
2577+
rwr.notifyAndCancelQueue(gatt.getDevice());
25782578
}
25792579
awaitingRequest = null;
25802580
onError(gatt.getDevice(), ERROR_WRITE_CHARACTERISTIC, status);
@@ -2615,13 +2615,12 @@ public void onDescriptorRead(final BluetoothGatt gatt, final BluetoothGattDescri
26152615
", value: " + ParserUtils.parse(data));
26162616

26172617
BleManagerHandler.this.onDescriptorRead(gatt, descriptor);
2618-
if (request instanceof ReadRequest) {
2619-
final ReadRequest request = (ReadRequest) BleManagerHandler.this.request;
2620-
request.notifyValueChanged(gatt.getDevice(), data);
2621-
if (request.hasMore()) {
2622-
enqueueFirst(request);
2618+
if (request instanceof final ReadRequest rr) {
2619+
rr.notifyValueChanged(gatt.getDevice(), data);
2620+
if (rr.hasMore()) {
2621+
enqueueFirst(rr);
26232622
} else {
2624-
request.notifySuccess(gatt.getDevice());
2623+
rr.notifySuccess(gatt.getDevice());
26252624
}
26262625
}
26272626
} else if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION
@@ -2637,8 +2636,8 @@ public void onDescriptorRead(final BluetoothGatt gatt, final BluetoothGattDescri
26372636
return;
26382637
} else {
26392638
Log.e(TAG, "onDescriptorRead error " + status);
2640-
if (request instanceof ReadRequest) {
2641-
request.notifyFail(gatt.getDevice(), status);
2639+
if (request instanceof final ReadRequest rr) {
2640+
rr.notifyFail(gatt.getDevice(), status);
26422641
}
26432642
awaitingRequest = null;
26442643
onError(gatt.getDevice(), ERROR_READ_DESCRIPTOR, status);
@@ -2672,9 +2671,9 @@ public void onDescriptorWrite(final BluetoothGatt gatt,
26722671
}
26732672
if (request instanceof final WriteRequest wr) {
26742673
final boolean valid = wr.notifyPacketSent(gatt.getDevice(), data);
2675-
if (!valid && requestQueue instanceof ReliableWriteRequest) {
2674+
if (!valid && requestQueue instanceof final ReliableWriteRequest rwr) {
26762675
wr.notifyFail(gatt.getDevice(), FailCallback.REASON_VALIDATION);
2677-
requestQueue.notifyAndCancelQueue(gatt.getDevice());
2676+
rwr.notifyAndCancelQueue(gatt.getDevice());
26782677
} else if (wr.hasMore()) {
26792678
enqueueFirst(wr);
26802679
} else {
@@ -2694,11 +2693,11 @@ public void onDescriptorWrite(final BluetoothGatt gatt,
26942693
return;
26952694
} else {
26962695
Log.e(TAG, "onDescriptorWrite error " + status);
2697-
if (request instanceof WriteRequest) {
2698-
request.notifyFail(gatt.getDevice(), status);
2696+
if (request instanceof final WriteRequest wr) {
2697+
wr.notifyFail(gatt.getDevice(), status);
26992698
// Automatically abort Reliable Write when write error happen
2700-
if (requestQueue instanceof ReliableWriteRequest)
2701-
requestQueue.notifyAndCancelQueue(gatt.getDevice());
2699+
if (requestQueue instanceof final ReliableWriteRequest rwr)
2700+
rwr.notifyAndCancelQueue(gatt.getDevice());
27022701
}
27032702
awaitingRequest = null;
27042703
onError(gatt.getDevice(), ERROR_WRITE_DESCRIPTOR, status);
@@ -2803,14 +2802,14 @@ public void onMtuChanged(@NonNull final BluetoothGatt gatt,
28032802
log(Log.INFO, () -> "MTU changed to: " + mtu);
28042803
BleManagerHandler.this.mtu = Math.min(515, mtu);
28052804
BleManagerHandler.this.onMtuChanged(gatt, BleManagerHandler.this.mtu);
2806-
if (request instanceof MtuRequest) {
2807-
((MtuRequest) request).notifyMtuChanged(gatt.getDevice(), BleManagerHandler.this.mtu);
2808-
request.notifySuccess(gatt.getDevice());
2805+
if (request instanceof final MtuRequest mr) {
2806+
mr.notifyMtuChanged(gatt.getDevice(), BleManagerHandler.this.mtu);
2807+
mr.notifySuccess(gatt.getDevice());
28092808
}
28102809
} else {
28112810
Log.e(TAG, "onMtuChanged error: " + status + ", mtu: " + mtu);
2812-
if (request instanceof MtuRequest) {
2813-
request.notifyFail(gatt.getDevice(), status);
2811+
if (request instanceof final MtuRequest mr) {
2812+
mr.notifyFail(gatt.getDevice(), status);
28142813
awaitingRequest = null;
28152814
}
28162815
onError(gatt.getDevice(), ERROR_MTU_REQUEST, status);
@@ -2862,10 +2861,9 @@ public void onConnectionUpdated(@NonNull final BluetoothGatt gatt,
28622861
cpuc.onConnectionUpdated(gatt.getDevice(), interval, latency, timeout);
28632862
}
28642863
// This callback may be called af any time, also when some other request is executed
2865-
if (request instanceof ConnectionPriorityRequest) {
2866-
((ConnectionPriorityRequest) request)
2867-
.notifyConnectionPriorityChanged(gatt.getDevice(), interval, latency, timeout);
2868-
request.notifySuccess(gatt.getDevice());
2864+
if (request instanceof final ConnectionPriorityRequest cpr) {
2865+
cpr.notifyConnectionPriorityChanged(gatt.getDevice(), interval, latency, timeout);
2866+
cpr.notifySuccess(gatt.getDevice());
28692867
}
28702868
} else if (status == 0x3b) { // HCI_ERR_UNACCEPT_CONN_INTERVAL
28712869
Log.e(TAG, "onConnectionUpdated received status: Unacceptable connection interval, " +
@@ -2876,8 +2874,8 @@ public void onConnectionUpdated(@NonNull final BluetoothGatt gatt,
28762874
"latency: " + latency + ", timeout: " + (timeout * 10) + "ms)");
28772875

28782876
// This callback may be called af any time, also when some other request is executed
2879-
if (request instanceof ConnectionPriorityRequest) {
2880-
request.notifyFail(gatt.getDevice(), status);
2877+
if (request instanceof final ConnectionPriorityRequest cpr) {
2878+
cpr.notifyFail(gatt.getDevice(), status);
28812879
awaitingRequest = null;
28822880
}
28832881
} else {
@@ -2889,8 +2887,8 @@ public void onConnectionUpdated(@NonNull final BluetoothGatt gatt,
28892887
"latency: " + latency + ", timeout: " + (timeout * 10) + "ms)");
28902888

28912889
// This callback may be called af any time, also when some other request is executed
2892-
if (request instanceof ConnectionPriorityRequest) {
2893-
request.notifyFail(gatt.getDevice(), status);
2890+
if (request instanceof final ConnectionPriorityRequest cpr) {
2891+
cpr.notifyFail(gatt.getDevice(), status);
28942892
awaitingRequest = null;
28952893
}
28962894
postCallback(c -> c.onError(gatt.getDevice(), ERROR_CONNECTION_PRIORITY_REQUEST, status));
@@ -2911,14 +2909,14 @@ public void onPhyUpdate(@NonNull final BluetoothGatt gatt,
29112909
log(Log.INFO, () ->
29122910
"PHY updated (TX: " + ParserUtils.phyToString(txPhy) +
29132911
", RX: " + ParserUtils.phyToString(rxPhy) + ")");
2914-
if (request instanceof PhyRequest) {
2915-
((PhyRequest) request).notifyPhyChanged(gatt.getDevice(), txPhy, rxPhy);
2916-
request.notifySuccess(gatt.getDevice());
2912+
if (request instanceof final PhyRequest pr) {
2913+
pr.notifyPhyChanged(gatt.getDevice(), txPhy, rxPhy);
2914+
pr.notifySuccess(gatt.getDevice());
29172915
}
29182916
} else {
29192917
log(Log.WARN, () -> "PHY updated failed with status " + status);
2920-
if (request instanceof PhyRequest) {
2921-
request.notifyFail(gatt.getDevice(), status);
2918+
if (request instanceof final PhyRequest pr) {
2919+
pr.notifyFail(gatt.getDevice(), status);
29222920
awaitingRequest = null;
29232921
}
29242922
postCallback(c -> c.onError(gatt.getDevice(), ERROR_PHY_UPDATE, status));
@@ -2939,14 +2937,14 @@ public void onPhyRead(@NonNull final BluetoothGatt gatt,
29392937
log(Log.INFO, () ->
29402938
"PHY read (TX: " + ParserUtils.phyToString(txPhy) +
29412939
", RX: " + ParserUtils.phyToString(rxPhy) + ")");
2942-
if (request instanceof PhyRequest) {
2943-
((PhyRequest) request).notifyPhyChanged(gatt.getDevice(), txPhy, rxPhy);
2940+
if (request instanceof final PhyRequest pr) {
2941+
pr.notifyPhyChanged(gatt.getDevice(), txPhy, rxPhy);
29442942
request.notifySuccess(gatt.getDevice());
29452943
}
29462944
} else {
29472945
log(Log.WARN, () -> "PHY read failed with status " + status);
2948-
if (request instanceof PhyRequest) {
2949-
request.notifyFail(gatt.getDevice(), status);
2946+
if (request instanceof final PhyRequest pr) {
2947+
pr.notifyFail(gatt.getDevice(), status);
29502948
}
29512949
awaitingRequest = null;
29522950
postCallback(c -> c.onError(gatt.getDevice(), ERROR_READ_PHY, status));
@@ -2961,14 +2959,14 @@ public void onReadRemoteRssi(@NonNull final BluetoothGatt gatt,
29612959
final int status) {
29622960
if (status == BluetoothGatt.GATT_SUCCESS) {
29632961
log(Log.INFO, () -> "Remote RSSI received: " + rssi + " dBm");
2964-
if (request instanceof ReadRssiRequest) {
2965-
((ReadRssiRequest) request).notifyRssiRead(gatt.getDevice(), rssi);
2966-
request.notifySuccess(gatt.getDevice());
2962+
if (request instanceof final ReadRssiRequest rrr) {
2963+
rrr.notifyRssiRead(gatt.getDevice(), rssi);
2964+
rrr.notifySuccess(gatt.getDevice());
29672965
}
29682966
} else {
29692967
log(Log.WARN, () -> "Reading remote RSSI failed with status " + status);
2970-
if (request instanceof ReadRssiRequest) {
2971-
request.notifyFail(gatt.getDevice(), status);
2968+
if (request instanceof final ReadRssiRequest rrr) {
2969+
rrr.notifyFail(gatt.getDevice(), status);
29722970
}
29732971
awaitingRequest = null;
29742972
postCallback(c -> c.onError(gatt.getDevice(), ERROR_READ_RSSI, status));
@@ -3274,8 +3272,8 @@ final void onNotificationSent(@NonNull final BluetoothGattServer server,
32743272
notifyNotificationSent(device);
32753273
} else {
32763274
Log.e(TAG, "onNotificationSent error " + status);
3277-
if (request instanceof WriteRequest) {
3278-
request.notifyFail(device, status);
3275+
if (request instanceof final WriteRequest wr) {
3276+
wr.notifyFail(device, status);
32793277
}
32803278
awaitingRequest = null;
32813279
onError(device, ERROR_NOTIFY, status);

0 commit comments

Comments
 (0)