Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More code cleanups #3975

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static AudioFormat parseWavFormat(InputStream inputStream) throws IOExcep
*/
public static void removeFMT(InputStream data) throws IOException {
DataInputStream dataInputStream = new DataInputStream(data);
Integer nextInt = dataInputStream.readInt();
int nextInt = dataInputStream.readInt();
int i = 0;
while (nextInt != DATA_MAGIC && i < 200) {
nextInt = dataInputStream.readInt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ private Collection<RuleTemplate> getTemplateByFilter(Map<String, String> list) {
return templates;
} else {
for (String templateUID : list.keySet()) {
if (templateUID.indexOf(id) != -1) {
if (templateUID.contains(id)) {
templates.add(autoCommands.getTemplate(templateUID, locale));
}
}
Expand Down Expand Up @@ -363,7 +363,7 @@ private Collection<ModuleType> getModuleTypeByFilter(Map<String, String> list) {
return moduleTypes;
} else {
for (String typeUID : list.values()) {
if (typeUID.indexOf(id) != -1) {
if (typeUID.contains(id)) {
moduleTypes.add(autoCommands.getModuleType(typeUID, locale));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void testMultiServiceAnnotationActions() {
assertTrue(types.contains(TEST_ACTION_TYPE_ID));

ModuleType mt = prov.getModuleType(TEST_ACTION_TYPE_ID, null);
assertTrue(mt instanceof ActionType);
assertInstanceOf(ActionType.class, mt);

ActionType at = (ActionType) mt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void testMultiServiceAnnotationActions() {
assertTrue(types.contains(TEST_ACTION_TYPE_ID));

ModuleType mt = prov.getModuleType(TEST_ACTION_TYPE_ID, null);
assertTrue(mt instanceof ActionType);
assertInstanceOf(ActionType.class, mt);

ActionType at = (ActionType) mt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,10 @@ private void scan() {
.configureBlocking(false);

byte[] requestArray = buildRequestArray(channel, Objects.toString(request));
logger.trace("{}: {}", candidate.getUID(),
HexFormat.of().withDelimiter(" ").formatHex(requestArray));
if (logger.isTraceEnabled()) {
logger.trace("{}: {}", candidate.getUID(),
HexFormat.of().withDelimiter(" ").formatHex(requestArray));
}

channel.send(ByteBuffer.wrap(requestArray),
new InetSocketAddress(destIp, destPort));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import static org.openhab.core.config.discovery.addon.AddonFinderConstants.ADDON_SUGGESTION_FINDER;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
Expand All @@ -28,7 +27,6 @@
import org.openhab.core.addon.AddonMatchProperty;
import org.openhab.core.config.discovery.addon.AddonFinder;
import org.openhab.core.config.discovery.addon.BaseAddonFinder;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -51,10 +49,6 @@ public class ProcessAddonFinder extends BaseAddonFinder {

private final Logger logger = LoggerFactory.getLogger(ProcessAddonFinder.class);

@Activate
public ProcessAddonFinder() {
}

// get list of running processes visible to openHAB,
// also tries to mitigate differences on different operating systems
String getProcessCommandProcess(ProcessHandle h) {
Expand All @@ -77,7 +71,7 @@ String getProcessCommandProcess(ProcessHandle h) {
public Set<AddonInfo> getSuggestedAddons() {
logger.trace("ProcessAddonFinder::getSuggestedAddons");
Set<AddonInfo> result = new HashSet<>();
Set<String> processList = Collections.emptySet();
Set<String> processList;
try {
processList = ProcessHandle.allProcesses().map(this::getProcessCommandProcess)
.filter(Predicate.not(String::isEmpty)).collect(Collectors.toUnmodifiableSet());
Expand All @@ -92,7 +86,7 @@ public Set<AddonInfo> getSuggestedAddons() {

List<AddonMatchProperty> matchProperties = method.getMatchProperties();
List<AddonMatchProperty> commands = matchProperties.stream()
.filter(amp -> COMMAND.equals(amp.getName())).collect(Collectors.toUnmodifiableList());
.filter(amp -> COMMAND.equals(amp.getName())).toList();

if (matchProperties.size() != commands.size()) {
logger.warn("Add-on '{}' addon.xml file contains unsupported 'match-property'", candidate.getUID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class UpnpAddonFinder extends BaseAddonFinder implements RegistryListener

private final Logger logger = LoggerFactory.getLogger(UpnpAddonFinder.class);
private final Map<String, RemoteDevice> devices = new ConcurrentHashMap<>();
private UpnpService upnpService;
private final UpnpService upnpService;

@Activate
public UpnpAddonFinder(@Reference UpnpService upnpService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ public void testConfigUpdateNormalizationWithConfigDescription() throws URISynta
when(thingRegistryMock.get(eq(THING_UID))).thenReturn(thing);
when(thingProviderMock.get(eq(THING_UID))).thenReturn(thing);

assertTrue(thing.getConfiguration().get("foo") instanceof String);
assertInstanceOf(String.class, thing.getConfiguration().get("foo"));

inbox.activate();
inbox.add(DiscoveryResultBuilder.create(THING_UID).withProperty("foo", 3).build());

assertTrue(thing.getConfiguration().get("foo") instanceof String);
assertInstanceOf(String.class, thing.getConfiguration().get("foo"));
// thing updated if managed
assertEquals("3", thing.getConfiguration().get("foo"));
}
Expand All @@ -131,12 +131,12 @@ public void testConfigUpdateNormalizationWithConfigDescriptionUnanagedThing() {
configureConfigDescriptionRegistryMock("foo", Type.TEXT);
when(thingRegistryMock.get(eq(THING_UID))).thenReturn(thing);

assertTrue(thing.getConfiguration().get("foo") instanceof String);
assertInstanceOf(String.class, thing.getConfiguration().get("foo"));

inbox.activate();
inbox.add(DiscoveryResultBuilder.create(THING_UID).withProperty("foo", 3).build());

assertTrue(thing.getConfiguration().get("foo") instanceof String);
assertInstanceOf(String.class, thing.getConfiguration().get("foo"));
// thing not updated if unmanaged
assertEquals("1", thing.getConfiguration().get("foo"));
}
Expand All @@ -151,7 +151,7 @@ public void testApproveNormalization() {
inbox.approve(THING_UID, "Test", null);

assertEquals(THING_UID, lastAddedThing.getUID());
assertTrue(lastAddedThing.getConfiguration().get("foo") instanceof String);
assertInstanceOf(String.class, lastAddedThing.getConfiguration().get("foo"));
assertEquals("3", lastAddedThing.getConfiguration().get("foo"));
}

Expand All @@ -165,7 +165,7 @@ public void testApproveWithThingId() {
inbox.approve(THING_UID, "Test", THING_OTHER_ID);

assertEquals(THING_OTHER_UID, lastAddedThing.getUID());
assertTrue(lastAddedThing.getConfiguration().get("foo") instanceof String);
assertInstanceOf(String.class, lastAddedThing.getConfiguration().get("foo"));
assertEquals("3", lastAddedThing.getConfiguration().get("foo"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ private void unregisterCommunicationInterface(ModbusCommunicationInterface commI

private void maybeCloseConnections(ModbusSlaveEndpoint endpoint) {
boolean lastCommWithThisEndpointWasRemoved = communicationInterfaces.stream()
.filter(comm -> comm.endpoint.equals(endpoint)).count() == 0L;
.noneMatch(comm -> comm.endpoint.equals(endpoint));
if (lastCommWithThisEndpointWasRemoved) {
// Since last communication interface pointing to this endpoint was closed, we can clean up resources
// and disconnect connections.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void testSlaveReadErrorResponse() throws Exception {

assertThat(okCount.get(), is(equalTo(0)));
assertThat(errorCount.get(), is(equalTo(1)));
assertTrue(lastError.get() instanceof ModbusSlaveErrorResponseException, lastError.toString());
assertInstanceOf(ModbusSlaveErrorResponseException.class, lastError.get(), lastError.toString());
}
}

Expand Down Expand Up @@ -206,7 +206,7 @@ public void testSlaveConnectionError() throws Exception {

assertThat(okCount.get(), is(equalTo(0)));
assertThat(errorCount.get(), is(equalTo(1)));
assertTrue(lastError.get() instanceof ModbusConnectionException, lastError.toString());
assertInstanceOf(ModbusConnectionException.class, lastError.get(), lastError.toString());
}
}

Expand Down Expand Up @@ -239,7 +239,7 @@ public void testIOError() throws Exception {
assertTrue(callbackCalled.await(15, TimeUnit.SECONDS));
assertThat(okCount.get(), is(equalTo(0)));
assertThat(lastError.toString(), errorCount.get(), is(equalTo(1)));
assertTrue(lastError.get() instanceof ModbusSlaveIOException, lastError.toString());
assertInstanceOf(ModbusSlaveIOException.class, lastError.get(), lastError.toString());
}
}

Expand Down Expand Up @@ -478,7 +478,7 @@ public void testOneOffWriteMultipleCoilError() throws Exception {
assertTrue(callbackCalled.await(60, TimeUnit.SECONDS));

assertThat(unexpectedCount.get(), is(equalTo(0)));
assertTrue(lastError.get() instanceof ModbusSlaveErrorResponseException, lastError.toString());
assertInstanceOf(ModbusSlaveErrorResponseException.class, lastError.get(), lastError.toString());

assertThat(modbustRequestCaptor.getAllReturnValues().size(), is(equalTo(1)));
ModbusRequest request = modbustRequestCaptor.getAllReturnValues().get(0);
Expand Down Expand Up @@ -555,7 +555,7 @@ public void testOneOffWriteSingleCoilError() throws Exception {
assertTrue(callbackCalled.await(60, TimeUnit.SECONDS));

assertThat(unexpectedCount.get(), is(equalTo(0)));
assertTrue(lastError.get() instanceof ModbusSlaveErrorResponseException, lastError.toString());
assertInstanceOf(ModbusSlaveErrorResponseException.class, lastError.get(), lastError.toString());

assertThat(modbustRequestCaptor.getAllReturnValues().size(), is(equalTo(1)));
ModbusRequest request = modbustRequestCaptor.getAllReturnValues().get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void reconnectPolicyDefault() throws ConfigurationException, MqttExceptio
"MqttBrokerConnectionTests");

// Check if the default policy is set and that the broker within the policy is set.
assertTrue(connection.getReconnectStrategy() instanceof PeriodicReconnectStrategy);
assertInstanceOf(PeriodicReconnectStrategy.class, connection.getReconnectStrategy());
AbstractReconnectStrategy p = connection.getReconnectStrategy();
assertThat(p.getBrokerConnection(), equalTo(connection));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ public Collection<SerialPortProvider> getPortProvidersForPortName(URI portName)
final Predicate<SerialPortProvider> filter;
if (scheme != null) {
// Get port providers which accept exactly the port with its scheme.
filter = provider -> provider.getAcceptedProtocols().filter(prot -> prot.getScheme().equals(scheme))
.count() > 0;
filter = provider -> provider.getAcceptedProtocols().anyMatch(prot -> prot.getScheme().equals(scheme));
} else {
// Get port providers which accept the same type (local, net)
filter = provider -> provider.getAcceptedProtocols().filter(prot -> prot.getPathType().equals(pathType))
.count() > 0;
filter = provider -> provider.getAcceptedProtocols().anyMatch(prot -> prot.getPathType().equals(pathType));
}

return portCreators.stream().filter(filter).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ protected void onAddElement(SemanticTag tag) throws IllegalArgumentException {
+ "': only Equipment, Location, Point and Property are allowed as root tags.");
}
type = uid;
className = newTag.getClass().getName();
className = newTag.getName();
} else {
String name = uid.substring(lastSeparator + 1);
String parentId = uid.substring(0, lastSeparator);
Expand Down Expand Up @@ -252,7 +252,7 @@ protected void onRemoveElement(SemanticTag tag) {
private void addTagSet(String tagId, Class<? extends Tag> tagSet) {
logger.trace("addTagSet {}", tagId);
String id = tagId;
while (id.indexOf("_") != -1) {
while (id.contains("_")) {
SemanticTags.addTagSet(id, tagSet);
id = id.substring(id.indexOf("_") + 1);
}
Expand All @@ -266,7 +266,7 @@ private void removeTagSet(String tagId) {
return;
}
String id = tagId;
while (id.indexOf("_") != -1) {
while (id.contains("_")) {
SemanticTags.removeTagSet(id, tagSet);
id = id.substring(id.indexOf("_") + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ public void allInsertedNumbersAreLoadedAsBigDecimalFromCache() {
DummyObject dummy = objectStorage.get("DummyObject");

assertNotNull(dummy);
assertTrue(dummy.configuration.get("testShort") instanceof BigDecimal);
assertTrue(dummy.configuration.get("testInt") instanceof BigDecimal);
assertTrue(dummy.configuration.get("testLong") instanceof BigDecimal);
assertTrue(dummy.configuration.get("testDouble") instanceof BigDecimal);
assertTrue(dummy.configuration.get("testFloat") instanceof BigDecimal);
assertTrue(dummy.configuration.get("testBigDecimal") instanceof BigDecimal);
assertTrue(dummy.configuration.get("testBoolean") instanceof Boolean);
assertTrue(dummy.configuration.get("testString") instanceof String);
assertInstanceOf(BigDecimal.class, dummy.configuration.get("testShort"));
assertInstanceOf(BigDecimal.class, dummy.configuration.get("testInt"));
assertInstanceOf(BigDecimal.class, dummy.configuration.get("testLong"));
assertInstanceOf(BigDecimal.class, dummy.configuration.get("testDouble"));
assertInstanceOf(BigDecimal.class, dummy.configuration.get("testFloat"));
assertInstanceOf(BigDecimal.class, dummy.configuration.get("testBigDecimal"));
assertInstanceOf(Boolean.class, dummy.configuration.get("testBoolean"));
assertInstanceOf(String.class, dummy.configuration.get("testString"));
}

@Test
Expand All @@ -91,14 +91,14 @@ public void allInsertedNumbersAreLoadedAsBigDecimalFromDisk() {
DummyObject dummy = objectStorage.get("DummyObject");

assertNotNull(dummy);
assertTrue(dummy.configuration.get("testShort") instanceof BigDecimal);
assertTrue(dummy.configuration.get("testInt") instanceof BigDecimal);
assertTrue(dummy.configuration.get("testLong") instanceof BigDecimal);
assertTrue(dummy.configuration.get("testDouble") instanceof BigDecimal);
assertTrue(dummy.configuration.get("testFloat") instanceof BigDecimal);
assertTrue(dummy.configuration.get("testBigDecimal") instanceof BigDecimal);
assertTrue(dummy.configuration.get("testBoolean") instanceof Boolean);
assertTrue(dummy.configuration.get("testString") instanceof String);
assertInstanceOf(BigDecimal.class, dummy.configuration.get("testShort"));
assertInstanceOf(BigDecimal.class, dummy.configuration.get("testInt"));
assertInstanceOf(BigDecimal.class, dummy.configuration.get("testLong"));
assertInstanceOf(BigDecimal.class, dummy.configuration.get("testDouble"));
assertInstanceOf(BigDecimal.class, dummy.configuration.get("testFloat"));
assertInstanceOf(BigDecimal.class, dummy.configuration.get("testBigDecimal"));
assertInstanceOf(Boolean.class, dummy.configuration.get("testBoolean"));
assertInstanceOf(String.class, dummy.configuration.get("testString"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void setCommandOptionsPublishesEvent() {
verify(eventPublisherMock, times(1)).post(capture.capture());

Event event = capture.getValue();
assertTrue(event instanceof ChannelDescriptionChangedEvent);
assertInstanceOf(ChannelDescriptionChangedEvent.class, event);
ChannelDescriptionChangedEvent cdce = (ChannelDescriptionChangedEvent) event;
assertEquals(CommonChannelDescriptionField.COMMAND_OPTIONS, cdce.getField());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void setStatePatternPublishesEvent() {
verify(eventPublisherMock, times(1)).post(capture.capture());

Event event = capture.getValue();
assertTrue(event instanceof ChannelDescriptionChangedEvent);
assertInstanceOf(ChannelDescriptionChangedEvent.class, event);
ChannelDescriptionChangedEvent cdce = (ChannelDescriptionChangedEvent) event;
assertEquals(CommonChannelDescriptionField.PATTERN, cdce.getField());

Expand All @@ -104,7 +104,7 @@ public void setStateOptionsPublishesEvent() {
verify(eventPublisherMock, times(1)).post(capture.capture());

Event event = capture.getValue();
assertTrue(event instanceof ChannelDescriptionChangedEvent);
assertInstanceOf(ChannelDescriptionChangedEvent.class, event);
ChannelDescriptionChangedEvent cdce = (ChannelDescriptionChangedEvent) event;
assertEquals(CommonChannelDescriptionField.STATE_OPTIONS, cdce.getField());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private synchronized WatchService getWatchService() throws TransformationExcepti
}

private void watchSubDirectory(String subDirectory, final WatchService watchService) {
if (watchedDirectories.indexOf(subDirectory) == -1) {
if (!watchedDirectories.contains(subDirectory)) {
String watchedDirectory = getSourcePath() + subDirectory;
Path transformFilePath = Paths.get(watchedDirectory);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private double convertData(State state) {
} else if (state instanceof OpenClosedType) {
return state == OpenClosedType.CLOSED ? 0 : 1;
} else {
logger.debug("Unsupported item type in chart: {}", state.getClass().toString());
logger.debug("Unsupported item type in chart: {}", state.getClass());
return 0;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ public void testStateConversionForSliderWidgetThroughGetState() throws ItemNotFo

State stateForSlider = uiRegistry.getState(sliderWidget);

assertTrue(stateForSlider instanceof PercentType);
assertInstanceOf(PercentType.class, stateForSlider);

PercentType pt = (PercentType) stateForSlider;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void removeItemMetadata(String name) {

@Override
public Collection<Metadata> getAll() {
return super.getAll().stream().map(this::normalizeMetadata).collect(Collectors.toUnmodifiableList());
return super.getAll().stream().map(this::normalizeMetadata).toList();
}

@Override
Expand Down
Loading