Skip to content

Commit

Permalink
Java 21 language features
Browse files Browse the repository at this point in the history
* use getFirst and getLast methods
* replace new Locale by Locale.of
* replace Paths.get by Path.of
* use ThreadLocalRandom.current().nextDouble()
* add @serial annotations

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
  • Loading branch information
holgerfriedrich committed Jan 3, 2025
1 parent 979e4a7 commit 189a07d
Show file tree
Hide file tree
Showing 189 changed files with 592 additions and 441 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected void installFromCache(BundleContext bundleContext, String addonId) thr
"The local cache folder doesn't contain a single file: " + addonPath, null);
}

try (FileInputStream fileInputStream = new FileInputStream(bundleFiles.get(0).toFile())) {
try (FileInputStream fileInputStream = new FileInputStream(bundleFiles.getFirst().toFile())) {
Bundle bundle = bundleContext.installBundle(addonId, fileInputStream);
try {
bundle.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.core.addon.marketplace;

import java.io.Serial;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -24,6 +26,7 @@
@NonNullByDefault
public class MarketplaceHandlerException extends Exception {

@Serial
private static final long serialVersionUID = -5652014141471618161L;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.core.addon.marketplace.internal.community;

import java.io.Serial;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
Expand All @@ -32,6 +33,7 @@
*/
@NonNullByDefault
final class SerializedNameAnnotationIntrospector extends AnnotationIntrospector {
@Serial
private static final long serialVersionUID = 1L;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void testInstalledAddonIsStillPresentAfterRemoteIsDisabledOrMissing() {
// check only the installed addon is present
addons = addonService.getAddons(null);
assertThat(addons, hasSize(1));
assertThat(addons.get(0).getUid(), is(getFullAddonId(TEST_ADDON)));
assertThat(addons.getFirst().getUid(), is(getFullAddonId(TEST_ADDON)));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,26 @@ void testAddonInfoListReader() {
assertNotNull(addons);
List<AddonInfo> addonsInfos = addons.getAddons();
assertEquals(1, addonsInfos.size());
AddonInfo addon = addonsInfos.get(0);
AddonInfo addon = addonsInfos.getFirst();
assertNotNull(addon);
List<AddonDiscoveryMethod> discoveryMethods = addon.getDiscoveryMethods();
assertNotNull(discoveryMethods);
assertEquals(2, discoveryMethods.size());

AddonDiscoveryMethod method = discoveryMethods.get(0);
AddonDiscoveryMethod method = discoveryMethods.getFirst();
assertNotNull(method);
assertEquals("mdns", method.getServiceType());
List<AddonParameter> parameters = method.getParameters();
assertNotNull(parameters);
assertEquals(1, parameters.size());
AddonParameter parameter = parameters.get(0);
AddonParameter parameter = parameters.getFirst();
assertNotNull(parameter);
assertEquals("mdnsServiceType", parameter.getName());
assertEquals("_printer._tcp.local.", parameter.getValue());
List<AddonMatchProperty> matchProperties = method.getMatchProperties();
assertNotNull(matchProperties);
assertEquals(2, matchProperties.size());
AddonMatchProperty property = matchProperties.get(0);
AddonMatchProperty property = matchProperties.getFirst();
assertNotNull(property);
assertEquals("rp", property.getName());
assertEquals(".*", property.getRegex());
Expand All @@ -118,7 +118,7 @@ void testAddonInfoListReader() {
matchProperties = method.getMatchProperties();
assertNotNull(matchProperties);
assertEquals(1, matchProperties.size());
property = matchProperties.get(0);
property = matchProperties.getFirst();
assertNotNull(property);
assertEquals("modelName", property.getName());
assertEquals("Philips hue bridge", property.getRegex());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.core.audio;

import java.io.Serial;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -24,6 +26,7 @@
@NonNullByDefault
public class AudioException extends Exception {

@Serial
private static final long serialVersionUID = 1L;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.core.audio;

import java.io.Serial;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -25,6 +27,7 @@
@NonNullByDefault
public class UnsupportedAudioFormatException extends AudioException {

@Serial
private static final long serialVersionUID = 1L;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.core.audio;

import java.io.Serial;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -24,6 +26,7 @@
@NonNullByDefault
public class UnsupportedAudioStreamException extends AudioException {

@Serial
private static final long serialVersionUID = 1L;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serial;
import java.nio.file.Files;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -72,6 +73,7 @@
@NonNullByDefault
public class AudioServlet extends HttpServlet implements AudioHTTPServer {

@Serial
private static final long serialVersionUID = -3364664035854567854L;

private static final List<String> WAV_MIME_TYPES = List.of("audio/wav", "audio/x-wav", "audio/vnd.wave");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import java.io.File;
import java.math.BigDecimal;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -138,7 +138,7 @@ private ConfigDescriptionParameter getVolumeConfigDescParam(@Nullable Locale loc
*/
private List<ParameterOption> getSoundOptions() {
List<ParameterOption> options = new ArrayList<>();
File soundsDir = Paths.get(OpenHAB.getConfigFolder(), AudioManager.SOUND_DIR).toFile();
File soundsDir = Path.of(OpenHAB.getConfigFolder(), AudioManager.SOUND_DIR).toFile();
if (soundsDir.isDirectory()) {
for (String fileName : soundsDir.list()) {
if (fileName.contains(".") && !fileName.startsWith(".")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private ScriptEngineFactoryHelper() {
public static Map.@Nullable Entry<String, String> getParameterOption(ScriptEngineFactory engineFactory) {
List<String> scriptTypes = engineFactory.getScriptTypes();
if (!scriptTypes.isEmpty()) {
ScriptEngine scriptEngine = engineFactory.createScriptEngine(scriptTypes.get(0));
ScriptEngine scriptEngine = engineFactory.createScriptEngine(scriptTypes.getFirst());
if (scriptEngine != null) {
Map.Entry<String, String> parameterOption = Map.entry(getPreferredMimeType(engineFactory),
getLanguageName(scriptEngine.getFactory()));
Expand All @@ -66,7 +66,7 @@ public static String getPreferredMimeType(ScriptEngineFactory factory) {
}
List<String> mimeTypes = new ArrayList<>(scriptTypes);
mimeTypes.removeIf(mimeType -> !mimeType.contains("application") || "application/python".equals(mimeType));
return mimeTypes.isEmpty() ? scriptTypes.get(0) : mimeTypes.get(0);
return mimeTypes.isEmpty() ? scriptTypes.getFirst() : mimeTypes.getFirst();
}

public static String getLanguageName(javax.script.ScriptEngineFactory factory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void addScriptEngineFactory(ScriptEngineFactory engineFactory) {
}
if (logger.isDebugEnabled()) {
if (!scriptTypes.isEmpty()) {
ScriptEngine scriptEngine = engineFactory.createScriptEngine(scriptTypes.get(0));
ScriptEngine scriptEngine = engineFactory.createScriptEngine(scriptTypes.getFirst());
if (scriptEngine != null) {
javax.script.ScriptEngineFactory factory = scriptEngine.getFactory();
logger.debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ protected void compileScript() throws ScriptException {
Optional<ScriptEngine> engine = getScriptEngine();
if (engine.isPresent()) {
ScriptEngine scriptEngine = engine.get();
if (scriptEngine instanceof Compilable) {
if (scriptEngine instanceof Compilable compilable) {
logger.debug("Pre-compiling script of rule with UID '{}'", ruleUID);
compiledScript = Optional.ofNullable(((Compilable) scriptEngine).compile(script));
compiledScript = Optional.ofNullable(compilable.compile(script));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void setScriptEngineFactory(ScriptEngineFactory engineFactory) {
public void unsetScriptEngineFactory(ScriptEngineFactory engineFactory) {
List<String> scriptTypes = engineFactory.getScriptTypes();
if (!scriptTypes.isEmpty()) {
ScriptEngine scriptEngine = engineFactory.createScriptEngine(scriptTypes.get(0));
ScriptEngine scriptEngine = engineFactory.createScriptEngine(scriptTypes.getFirst());
if (scriptEngine != null) {
parameterOptions.remove(ScriptEngineFactoryHelper.getPreferredMimeType(engineFactory));
logger.trace("ParameterOptions: {}", parameterOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,15 @@ private static List<String> collectRecords(int[] columnWidths, String prop, Coll
if (list != null && !list.isEmpty()) {
for (Object element : list) {
if (element instanceof String string) {
res.add(Utils.getColumn(columnWidths[0], values.get(0)) + string);
res.add(Utils.getColumn(columnWidths[0], values.getFirst()) + string);
if (isFirst) {
isFirst = false;
values.set(0, "");
}
} else if (element instanceof Module module) {
List<String> moduleRecords = getModuleRecords(module);
for (String elementRecord : moduleRecords) {
res.add(Utils.getColumn(columnWidths[0], values.get(0)) + elementRecord);
res.add(Utils.getColumn(columnWidths[0], values.getFirst()) + elementRecord);
if (isFirst) {
isFirst = false;
values.set(0, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.core.automation.internal.module.exception;

import java.io.Serial;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
Expand All @@ -22,5 +24,6 @@
@NonNullByDefault
public class UncomparableException extends Exception {

@Serial
private static final long serialVersionUID = 4891205711357448390L;
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public void testMultiServiceAnnotationActions() {
List<ParameterOption> parameterOptions = cdp.getOptions();
assertEquals(1, parameterOptions.size());

ParameterOption po = parameterOptions.get(0);
ParameterOption po = parameterOptions.getFirst();
assertEquals("conf2", po.getValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ moduleTypeI18nServiceMock, new AnnotationActionModuleTypeHelper(actionInputsHelp
List<ParameterOption> parameterOptions = cdp.getOptions();
assertEquals(1, parameterOptions.size());

ParameterOption po = parameterOptions.get(0);
ParameterOption po = parameterOptions.getFirst();
assertEquals("binding:thing-type:test2", po.getValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void testMapActionInputsToConfigDescriptionParametersWhenOk() {
List<ConfigDescriptionParameter> params = helper
.mapActionInputsToConfigDescriptionParameters(List.of(input1, input2));
assertThat(params.size(), is(2));
checkParameter(params.get(0), "Boolean", ConfigDescriptionParameter.Type.BOOLEAN, PARAM_LABEL,
checkParameter(params.getFirst(), "Boolean", ConfigDescriptionParameter.Type.BOOLEAN, PARAM_LABEL,
PARAM_DESCRIPTION, true, "false", null, null, null);
checkParameter(params.get(1), "String", ConfigDescriptionParameter.Type.TEXT, PARAM_LABEL, PARAM_DESCRIPTION,
false, null, null, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private static Collection<Object> normalizeCollection(Collection<@NonNull ?> col
for (final Object it : collection) {
final Object normalized = normalizeType(it, null);
lst.add(normalized);
if (normalized.getClass() != lst.get(0).getClass()) {
if (normalized.getClass() != lst.getFirst().getClass()) {
throw new IllegalArgumentException(
"Invalid configuration property. Heterogeneous collection value!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.core.config.core.validation;

import java.io.Serial;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -35,6 +36,7 @@
*/
public final class ConfigValidationException extends RuntimeException {

@Serial
private static final long serialVersionUID = 1L;

private final Logger logger = LoggerFactory.getLogger(ConfigValidationException.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void testWithOneParameter() {
assertThat(configDescription.getUID(), is(CONFIG_URI));
assertThat(configDescription.getParameterGroups(), hasSize(0));
assertThat(configDescription.getParameters(), hasSize(1));
assertThat(configDescription.getParameters().get(0), is(PARAM1));
assertThat(configDescription.getParameters().getFirst(), is(PARAM1));
}

@Test
Expand All @@ -81,7 +81,7 @@ public void testWithOneParameterGroup() {
ConfigDescription configDescription = builder.withParameterGroup(GROUP1).build();
assertThat(configDescription.getUID(), is(CONFIG_URI));
assertThat(configDescription.getParameterGroups(), hasSize(1));
assertThat(configDescription.getParameterGroups().get(0), is(GROUP1));
assertThat(configDescription.getParameterGroups().getFirst(), is(GROUP1));
assertThat(configDescription.getParameters(), hasSize(0));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ public void testGetConfigDescriptions() throws Exception {
assertThat(configDescriptionRegistry.getConfigDescriptions().size(), is(1));

List<ConfigDescription> configDescriptions = new ArrayList<>(configDescriptionRegistry.getConfigDescriptions());
assertThat(configDescriptions.get(0).getUID(), is(equalTo(uriDummy)));
assertThat(configDescriptions.get(0).toParametersMap().size(), is(1));
assertThat(configDescriptions.get(0).toParametersMap().get("param1"), notNullValue());
assertThat(configDescriptions.getFirst().getUID(), is(equalTo(uriDummy)));
assertThat(configDescriptions.getFirst().toParametersMap().size(), is(1));
assertThat(configDescriptions.getFirst().toParametersMap().get("param1"), notNullValue());

configDescriptionRegistry.addConfigDescriptionProvider(configDescriptionProviderMock1);
assertThat(configDescriptionRegistry.getConfigDescriptions().size(), is(2));
Expand All @@ -146,11 +146,11 @@ public void testGetConfigDescriptionsOptions() throws Exception {
assertThat(configDescriptionRegistry.getConfigDescriptions().size(), is(1));

List<ConfigDescription> configDescriptions = new ArrayList<>(configDescriptionRegistry.getConfigDescriptions());
assertThat(configDescriptions.get(0).getUID(), is(equalTo(uriDummy)));
assertThat(configDescriptions.getFirst().getUID(), is(equalTo(uriDummy)));

assertThat(configDescriptions.get(0).getParameters().size(), is(2));
assertThat(configDescriptions.get(0).getParameters().get(0).getName(), is(equalTo("param1")));
assertThat(configDescriptions.get(0).getParameters().get(1).getName(), is(equalTo("param2")));
assertThat(configDescriptions.getFirst().getParameters().size(), is(2));
assertThat(configDescriptions.getFirst().getParameters().getFirst().getName(), is(equalTo("param1")));
assertThat(configDescriptions.getFirst().getParameters().get(1).getName(), is(equalTo("param2")));

configDescriptionRegistry.removeConfigDescriptionProvider(configDescriptionProviderMock);
assertThat(configDescriptionRegistry.getConfigDescriptions().size(), is(1));
Expand All @@ -168,8 +168,8 @@ public void testGetConfigDescriptionsAliasedOptions() throws Exception {
configDescriptionRegistry.addConfigOptionProvider(configOptionsProviderMockAliased);

ConfigDescription res = requireNonNull(configDescriptionRegistry.getConfigDescription(uriAliases));
assertThat(res.getParameters().get(0).getOptions().size(), is(1));
assertThat(res.getParameters().get(0).getOptions().get(0).getLabel(), is("Aliased"));
assertThat(res.getParameters().getFirst().getOptions().size(), is(1));
assertThat(res.getParameters().getFirst().getOptions().getFirst().getLabel(), is("Aliased"));
assertThat(res.getUID(), is(uriAliases));

configDescriptionRegistry.removeConfigDescriptionProvider(configDescriptionProviderMock);
Expand All @@ -186,8 +186,8 @@ public void testGetConfigDescriptionsAliasedOptionsOriginalWins() throws Excepti
configDescriptionRegistry.addConfigOptionProvider(configOptionsProviderMockAliased);

ConfigDescription res = requireNonNull(configDescriptionRegistry.getConfigDescription(uriAliases));
assertThat(res.getParameters().get(0).getOptions().size(), is(1));
assertThat(res.getParameters().get(0).getOptions().get(0).getLabel(), is("Aliased"));
assertThat(res.getParameters().getFirst().getOptions().size(), is(1));
assertThat(res.getParameters().getFirst().getOptions().getFirst().getLabel(), is("Aliased"));
assertThat(res.getUID(), is(uriAliases));

configDescriptionRegistry.removeConfigDescriptionProvider(configDescriptionProviderMock);
Expand All @@ -203,8 +203,8 @@ public void testGetConfigDescriptionsNonAliasOptions() throws Exception {
configDescriptionRegistry.addConfigOptionProvider(configOptionsProviderMock);

ConfigDescription res = requireNonNull(configDescriptionRegistry.getConfigDescription(uriAliases));
assertThat(res.getParameters().get(0).getOptions().size(), is(1));
assertThat(res.getParameters().get(0).getOptions().get(0).getLabel(), is("Original"));
assertThat(res.getParameters().getFirst().getOptions().size(), is(1));
assertThat(res.getParameters().getFirst().getOptions().getFirst().getLabel(), is("Original"));
assertThat(res.getUID(), is(uriAliases));

configDescriptionRegistry.removeConfigDescriptionProvider(configDescriptionProviderMock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void assertConfigAllowsNullValues() {
configuration.put("stringField", "someValue");
configuration.put("additionalField", "");
assertThat(props.get("stringField"), is(nullValue()));
assertThat(values.get(0), is(nullValue()));
assertThat(values.getFirst(), is(nullValue()));
assertThat(values.get(1), is(nullValue()));
assertThat(values.size(), is(2));
assertThat(keys.size(), is(2));
Expand Down
Loading

0 comments on commit 189a07d

Please sign in to comment.