Skip to content

Commit 65f1a96

Browse files
authored
Refactor removal of accents for Thing ID normalization (openhab#17698)
Signed-off-by: Jacob Laursen <[email protected]>
1 parent cd099cc commit 65f1a96

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

bundles/org.openhab.binding.fmiweather/src/main/java/org/openhab/binding/fmiweather/internal/discovery/FMIWeatherDiscoveryService.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import static org.openhab.binding.fmiweather.internal.BindingConstants.*;
1616
import static org.openhab.binding.fmiweather.internal.discovery.CitiesOfFinland.CITIES_OF_FINLAND;
1717

18+
import java.text.Normalizer;
1819
import java.util.Collections;
1920
import java.util.LinkedList;
2021
import java.util.List;
@@ -140,9 +141,9 @@ public void createResults(@Nullable PointType location) {
140141
private void createForecastForCurrentLocation(@Nullable PointType currentLocation) {
141142
if (currentLocation != null) {
142143
DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(UID_LOCAL_FORECAST)
143-
.withLabel(String.format("FMI local weather forecast"))
144+
.withLabel("FMI local weather forecast")
144145
.withProperty(LOCATION,
145-
String.format("%s,%s", currentLocation.getLatitude(), currentLocation.getLongitude()))
146+
"%s,%s".formatted(currentLocation.getLatitude(), currentLocation.getLongitude()))
146147
.withRepresentationProperty(LOCATION).build();
147148
thingDiscovered(discoveryResult);
148149
}
@@ -151,11 +152,11 @@ private void createForecastForCurrentLocation(@Nullable PointType currentLocatio
151152
private void createForecastsForCities(@Nullable PointType currentLocation) {
152153
CITIES_OF_FINLAND.stream().filter(location2 -> isClose(currentLocation, location2)).forEach(city -> {
153154
DiscoveryResult discoveryResult = DiscoveryResultBuilder
154-
.create(new ThingUID(THING_TYPE_FORECAST, cleanId(String.format("city_%s", city.name))))
155+
.create(new ThingUID(THING_TYPE_FORECAST, cleanId("city_%s".formatted(city.name))))
155156
.withProperty(LOCATION,
156-
String.format("%s,%s", city.latitude.toPlainString(), city.longitude.toPlainString()))
157-
.withLabel(String.format("FMI weather forecast for %s", city.name))
158-
.withRepresentationProperty(LOCATION).build();
157+
"%s,%s".formatted(city.latitude.toPlainString(), city.longitude.toPlainString()))
158+
.withLabel("FMI weather forecast for %s".formatted(city.name)).withRepresentationProperty(LOCATION)
159+
.build();
159160
thingDiscovered(discoveryResult);
160161
});
161162
}
@@ -174,25 +175,24 @@ private void createObservationsForStations(@Nullable PointType location) {
174175
}).forEach(station -> {
175176
DiscoveryResult discoveryResult = DiscoveryResultBuilder
176177
.create(new ThingUID(THING_TYPE_OBSERVATION,
177-
cleanId(String.format("station_%s_%s", station.id, station.name))))
178-
.withLabel(String.format("FMI weather observation for %s", station.name))
178+
cleanId("station_%s_%s".formatted(station.id, station.name))))
179+
.withLabel("FMI weather observation for %s".formatted(station.name))
179180
.withProperty(BindingConstants.FMISID, station.id)
180181
.withRepresentationProperty(BindingConstants.FMISID).build();
181182
thingDiscovered(discoveryResult);
182183
});
183184
if (logger.isDebugEnabled()) {
184185
logger.debug("Candidate stations: {}",
185-
candidateStations.stream().map(station -> String.format("%s (%s)", station.name, station.id))
186+
candidateStations.stream().map(station -> "%s (%s)".formatted(station.name, station.id))
186187
.collect(Collectors.toCollection(TreeSet<String>::new)));
187188
logger.debug("Filtered stations: {}",
188-
filteredStations.stream().map(station -> String.format("%s (%s)", station.name, station.id))
189+
filteredStations.stream().map(station -> "%s (%s)".formatted(station.name, station.id))
189190
.collect(Collectors.toCollection(TreeSet<String>::new)));
190191
}
191192
}
192193

193194
private static String cleanId(String id) {
194-
return id.replace("ä", "a").replace("ö", "o").replace("å", "a").replace("Ä", "A").replace("Ö", "O")
195-
.replace("Å", "a").replaceAll("[^a-zA-Z0-9_]", "_");
195+
return Normalizer.normalize(id, Normalizer.Form.NFKD).replaceAll("\\p{M}", "").replaceAll("[^a-zA-Z0-9_]", "_");
196196
}
197197

198198
private static boolean isClose(@Nullable PointType location, Location location2) {

0 commit comments

Comments
 (0)