Java 17 features (H-M) (#15520)

- add missing @override
- Java style array syntax
- remove redundant modifiers
- always move String constants to left side in comparisons
- simplify lambda expressions and return statements
- use replace instead of replaceAll w/o regex
- instanceof matching and multiline strings
- remove null check before instanceof

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
Holger Friedrich
2023-09-08 14:36:59 +02:00
committed by GitHub
parent 3751fd0646
commit edaf17b345
519 changed files with 2703 additions and 2660 deletions

View File

@@ -190,7 +190,6 @@ public class MycroftConnection {
listeners.getOrDefault(mycroftMessage.type, new HashSet<>()).stream()).forEach(listener -> {
listener.baseMessageReceived(finalMessage);
});
} catch (RuntimeException e) {
// we need to catch all processing exceptions, otherwise they could affect the connection
logger.debug("{} encountered an error while processing the message {}: {}", socketName, message,
@@ -200,7 +199,6 @@ public class MycroftConnection {
@OnWebSocketError
public void onError(@Nullable Session session, Throwable cause) {
if (session == null || !session.equals(this.session)) {
handleWrongSession(session, "Connection error: " + cause.getMessage());
return;

View File

@@ -69,26 +69,26 @@ public class AudioPlayerChannel extends MycroftChannel<State> {
@Override
public void handleCommand(Command command) {
if (command instanceof PlayPauseType) {
if (((PlayPauseType) command) == PlayPauseType.PAUSE) {
if (command instanceof PlayPauseType playPauseCommand) {
if (playPauseCommand == PlayPauseType.PAUSE) {
if (handler.sendMessage(new MessageAudioPause())) {
updateMyState(PlayPauseType.PAUSE);
}
}
if (((PlayPauseType) command) == PlayPauseType.PLAY) {
if (playPauseCommand == PlayPauseType.PLAY) {
handler.sendMessage(new MessageAudioPlay());
if (handler.sendMessage(new MessageAudioResume())) {
updateMyState(PlayPauseType.PLAY);
}
}
}
if (command instanceof NextPreviousType) {
if (((NextPreviousType) command) == NextPreviousType.NEXT) {
if (command instanceof NextPreviousType nextPreviousCommand) {
if (nextPreviousCommand == NextPreviousType.NEXT) {
if (handler.sendMessage(new MessageAudioNext())) {
updateMyState(PlayPauseType.PLAY);
}
}
if (((NextPreviousType) command) == NextPreviousType.PREVIOUS) {
if (nextPreviousCommand == NextPreviousType.PREVIOUS) {
if (handler.sendMessage(new MessageAudioPrev())) {
updateMyState(PlayPauseType.PLAY);
}

View File

@@ -72,8 +72,7 @@ public class MuteChannel extends MycroftChannel<OnOffType> {
}
private boolean sendVolumeSetMessage(float volume) {
String messageToSend = VolumeChannel.VOLUME_SETTER_MESSAGE.replaceAll("\\$\\$VOLUME",
Float.valueOf(volume).toString());
String messageToSend = VolumeChannel.VOLUME_SETTER_MESSAGE.replaceAll("\\$\\$VOLUME", Float.toString(volume));
return handler.sendMessage(messageToSend);
}

View File

@@ -68,7 +68,6 @@ public class VolumeChannel extends MycroftChannel<State> {
@Override
public void messageReceived(BaseMessage message) {
if (message.type == MessageType.mycroft_volume_get_response) {
float volumeGet = ((MessageVolumeGetResponse) message).data.percent;
updateAndSaveMyState(normalizeVolume(volumeGet));
@@ -87,10 +86,10 @@ public class VolumeChannel extends MycroftChannel<State> {
}
protected final void updateAndSaveMyState(State state) {
if (state instanceof PercentType) {
this.lastVolume = ((PercentType) state);
if (((PercentType) state).intValue() > 0) {
this.lastNonZeroVolume = ((PercentType) state);
if (state instanceof PercentType volume) {
this.lastVolume = volume;
if (volume.intValue() > 0) {
this.lastNonZeroVolume = volume;
}
}
super.updateMyState(state);
@@ -140,7 +139,7 @@ public class VolumeChannel extends MycroftChannel<State> {
}
private boolean sendSetMessage(float volume) {
String messageToSend = VOLUME_SETTER_MESSAGE.replaceAll("\\$\\$VOLUME", Float.valueOf(volume).toString());
String messageToSend = VOLUME_SETTER_MESSAGE.replaceAll("\\$\\$VOLUME", Float.toString(volume));
return handler.sendMessage(messageToSend);
}
@@ -167,9 +166,9 @@ public class VolumeChannel extends MycroftChannel<State> {
handler.sendMessage(new MessageVolumeDecrease());
updateAndSaveMyState(computeNewVolume(-10));
}
} else if (command instanceof PercentType) {
sendSetMessage(toMycroftVolume((PercentType) command));
updateAndSaveMyState((PercentType) command);
} else if (command instanceof PercentType volume) {
sendSetMessage(toMycroftVolume(volume));
updateAndSaveMyState(volume);
} else if (command instanceof RefreshType) {
handler.sendMessage(new MessageVolumeGet());
}

View File

@@ -48,7 +48,6 @@ public class MycroftConnectionTest {
@Test
public void testConnectionOK() throws IOException {
MycroftConnection mycroftConnection = new MycroftConnection(mycroftConnectionListener, new WebSocketClient());
Mockito.when(sessionMock.getRemoteAddress()).thenReturn(new InetSocketAddress(1234));
mycroftConnection.onConnect(sessionMock);
@@ -85,7 +84,6 @@ public class MycroftConnectionTest {
@Test
public void testSpeakListener() throws IOException {
MycroftConnection mycroftConnection = new MycroftConnection(mycroftConnectionListener, new WebSocketClient());
Mockito.when(sessionMock.getRemoteAddress()).thenReturn(new InetSocketAddress(1234));