Some more code cleanup (#4021)

This cleanup includes:

* Use enhanced for loops
* Use text blocks
* Use Objects.equals
* Fix some typos
* Remove redundant variable initialization
* Remove redundant null checks with instanceof
* Remove redundant thrown Exceptions
* Remove redundant empty String concatenation

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born
2024-02-04 11:17:55 +01:00
committed by GitHub
parent 2cca87a026
commit 85056d9d7b
75 changed files with 250 additions and 294 deletions
@@ -665,7 +665,7 @@ public class AutomationIntegrationTest extends JavaOSGiTest {
TemplateRegistry<?> templateRegistry = getService(TemplateRegistry.class);
assertThat(templateRegistry, is(notNullValue()));
waitForAssert(() -> {
Template template = null;
Template template;
template = templateRegistry.get("SimpleTestTemplate");
assertThat(template, is(notNullValue()));
assertThat(template.getTags(), is(notNullValue()));
@@ -713,7 +713,7 @@ public class AutomationIntegrationTest extends JavaOSGiTest {
TemplateRegistry<?> templateRegistry = getService(TemplateRegistry.class);
assertThat(templateRegistry, is(notNullValue()));
waitForAssert(() -> {
Template template = null;
Template template;
template = templateRegistry.get("TestTemplateWithCompositeModules");
assertThat(template, is(notNullValue()));
assertThat(template.getTags(), is(notNullValue()));
@@ -22,6 +22,8 @@ import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openhab.core.automation.Action;
import org.openhab.core.automation.Condition;
import org.openhab.core.automation.Rule;
@@ -95,7 +97,8 @@ public class ScriptRuleOSGiTest extends JavaOSGiTest {
registerService(eventSubscriber);
}
// ignore - wip @Test
@Test
@Disabled("WIP")
public void testPredefinedRule() throws ItemNotFoundException {
EventPublisher eventPublisher = getService(EventPublisher.class);
ItemRegistry itemRegistry = getService(ItemRegistry.class);
@@ -926,15 +926,15 @@ public class ConfigDispatcherOSGiTest extends JavaOSGiTest {
}
private @Nullable Configuration getConfigurationWithContext(String pidWithContext) {
String pid = null;
String configContext = null;
String pid;
String configContext;
if (pidWithContext.contains(OpenHAB.SERVICE_CONTEXT_MARKER)) {
pid = pidWithContext.split(OpenHAB.SERVICE_CONTEXT_MARKER)[0];
configContext = pidWithContext.split(OpenHAB.SERVICE_CONTEXT_MARKER)[1];
} else {
throw new IllegalArgumentException("PID does not have a context");
}
Configuration[] configs = null;
Configuration[] configs;
try {
configs = configAdmin.listConfigurations(
"(&(service.factoryPid=" + pid + ")(" + OpenHAB.SERVICE_CONTEXT + "=" + configContext + "))");
@@ -23,7 +23,6 @@ import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
@@ -158,12 +157,14 @@ public class GenericItemProviderTest extends JavaOSGiTest {
Collection<Item> items = itemRegistry.getAll();
assertThat(items, hasSize(0));
String model = "Group Weather [TAG1]\n" + "Group Weather_Chart (Weather)\n"
+ "Number Weather_Temperature \"Outside Temperature [%.1f °C]\" <temperature> (Weather_Chart) [TAG1, TAG2] { channel=\"acmeweather:weather:berlin:temperature\" }\n"
+ "Number Weather_Temp_Max \"Todays Maximum [%.1f °C]\" <temperature> (Weather_Chart)\n"
+ "Number Weather_Temp_Min \"Todays Minimum [%.1f °C]\" <temperature> (Weather_Chart)\n"
+ "Number Weather_Chart_Period \"Chart Period\"\n"
+ "DateTime Weather_LastUpdate \"Last Update [%1$ta %1$tR]\" <clock> [TAG1, TAG2, TAG3]";
String model = """
Group Weather [TAG1]
Group Weather_Chart (Weather)
Number Weather_Temperature "Outside Temperature [%.1f °C]" <temperature> (Weather_Chart) [TAG1, TAG2] { channel="acmeweather:weather:berlin:temperature" }
Number Weather_Temp_Max "Todays Maximum [%.1f °C]" <temperature> (Weather_Chart)
Number Weather_Temp_Min "Todays Minimum [%.1f °C]" <temperature> (Weather_Chart)
Number Weather_Chart_Period "Chart Period"
DateTime Weather_LastUpdate "Last Update [%1$ta %1$tR]" <clock> [TAG1, TAG2, TAG3]""";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
Collection<Item> actualItems = itemRegistry.getAll();
@@ -368,9 +369,10 @@ public class GenericItemProviderTest extends JavaOSGiTest {
public void assertThatTheItemRegistryGetsTheSameInstanceOnItemUpdatesWithoutChanges() throws Exception {
assertThat(itemRegistry.getAll(), hasSize(0));
String model = "String test1 \"Test Item [%s]\" { channel=\"test:test:test:test\" }\n"
+ "String test2 \"Test Item [%s]\" { channel=\"test:test:test:test\" }\n"
+ "String test3 \"Test Item [%s]\" { channel=\"test:test:test:test\" }";
String model = """
String test1 "Test Item [%s]" { channel="test:test:test:test" }
String test2 "Test Item [%s]" { channel="test:test:test:test" }
String test3 "Test Item [%s]" { channel="test:test:test:test" }""";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
waitForAssert(() -> assertThat(itemRegistry.getAll(), hasSize(3)));
@@ -420,9 +422,7 @@ public class GenericItemProviderTest extends JavaOSGiTest {
assertThat(groupItem, is(notNullValue()));
int number = 0;
Iterator<Item> it = groupItem.getMembers().iterator();
while (it.hasNext()) {
Item item = it.next();
for (Item item : groupItem.getMembers()) {
assertThat(item.getName(), is("number" + (++number)));
}
}
@@ -459,9 +459,7 @@ public class GenericItemProviderTest extends JavaOSGiTest {
assertThat(groupItem, is(notNullValue()));
int number = 0;
Iterator<Item> it = groupItem.getMembers().iterator();
while (it.hasNext()) {
Item item = it.next();
for (Item item : groupItem.getMembers()) {
assertThat(item.getName(), is("number" + (++number)));
if (number == 7) {
assertThat(item.getLabel(), is("Number Seven"));
@@ -419,8 +419,10 @@ public class GenericThingProviderTest extends JavaOSGiTest {
public void assertThatStandaloneThingsCanHaveBridgesInLongNotation() {
assertThat(thingRegistry.getAll().size(), is(0));
String model = "Bridge hue:bridge:myBridge @ \"basement\" [ ip = \"1.2.3.4\", username = \"123\" ]\n"
+ "hue:LCT001:bulb1 (hue:bridge:myBridge) [ lightId = \"1\" ] { Switch : notification }\n";
String model = """
Bridge hue:bridge:myBridge @ "basement" [ ip = "1.2.3.4", username = "123" ]
hue:LCT001:bulb1 (hue:bridge:myBridge) [ lightId = "1" ] { Switch : notification }
""";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
Collection<Thing> actualThings = thingRegistry.getAll();
@@ -462,8 +464,10 @@ public class GenericThingProviderTest extends JavaOSGiTest {
public void assertThatStandaloneThingWithoutAbridgeDoesNotWorkInShortNotation() {
assertThat(thingRegistry.getAll().size(), is(0));
String model = "LCT001 bulb1 [ lightId = \"1\" ] { Switch : notification }\n"
+ "hue:LCT001:bulb2 (hue:bridge:myBridge) [ lightId = \"2\" ] { Switch : notification }\n";
String model = """
LCT001 bulb1 [ lightId = "1" ] { Switch : notification }
hue:LCT001:bulb2 (hue:bridge:myBridge) [ lightId = "2" ] { Switch : notification }
""";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
Collection<Thing> actualThings = thingRegistry.getAll();
@@ -66,19 +66,21 @@ public class GenericThingProviderTest2 extends JavaOSGiTest {
Collection<Thing> things = thingRegistry.getAll();
assertThat(things.size(), is(0));
String model = "Bridge Xhue:Xbridge:myBridge [ XipAddress = \"1.2.3.4\", XuserName = \"123\" ] {" + //
" XLCT001 bulb1 [ XlightId = \"1\" ] { Switch : notification }" + //
" Bridge Xbridge myBridge2 [ ] {" + //
" XLCT001 bulb2 [ ]" + //
" }" + //
"}" + //
"Xhue:XTEST:bulb4 [ XlightId = \"5\"]{" + //
" Switch : notification [ duration = \"5\" ]" + //
"}" + //
"" + //
"Xhue:XLCT001:bulb3 [ XlightId = \"4\" ] {" + //
" Switch : notification [ duration = \"5\" ]" + //
"}";
String model = """
Bridge Xhue:Xbridge:myBridge [ XipAddress = "1.2.3.4", XuserName = "123" ] {
XLCT001 bulb1 [ XlightId = "1" ] { Switch : notification }
Bridge Xbridge myBridge2 [ ] {
XLCT001 bulb2 [ ]
}
}
Xhue:XTEST:bulb4 [ XlightId = "5"] {
Switch : notification [ duration = "5" ]
}
Xhue:XLCT001:bulb3 [ XlightId = "4" ] {
Switch : notification [ duration = "5" ]
}
""";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
Collection<Thing> actualThings = thingRegistry.getAll();
@@ -87,7 +89,6 @@ public class GenericThingProviderTest2 extends JavaOSGiTest {
registerService(hueThingHandlerFactory, ThingHandlerFactory.class.getName());
actualThings = thingRegistry.getAll();
assertThat(thingRegistry.getAll().size(), is(6));
unregisterService(hueThingHandlerFactory);
@@ -114,7 +114,7 @@ public class JsonStorageServiceOSGiTest extends JavaOSGiTest {
@Test
public void testOverride() {
PersistedItem pItem = null;
PersistedItem pItem;
assertThat(storage.getKeys().size(), is(0));
@@ -295,7 +295,7 @@ public class ChannelLinkNotifierOSGiTest extends JavaOSGiTest {
}
private void forEachThingChannelUID(Thing thing, Consumer<ChannelUID> consumer) {
thing.getChannels().stream().map(Channel::getUID).forEach(consumer::accept);
thing.getChannels().stream().map(Channel::getUID).forEach(consumer);
}
private void addItemsAndLinks(Thing thing, String itemSuffix) {