Skip to content

Commit

Permalink
Normalize Metadata configuration from managed provider (#2958)
Browse files Browse the repository at this point in the history
* Normalize Metadata configuration from managed provider

Signed-off-by: Jan N. Klug <github@klug.nrw>
  • Loading branch information
J-N-K authored Sep 20, 2022
1 parent 4a84b80 commit e3b90e3
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*/
package org.openhab.core.internal.items;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.common.registry.AbstractManagedProvider;
Expand Down Expand Up @@ -76,4 +81,36 @@ public void removeItemMetadata(String name) {
logger.debug("Removing all metadata for item {}", name);
getAll().stream().filter(MetadataPredicates.ofItem(name)).map(Metadata::getUID).forEach(this::remove);
}

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

@Override
public @Nullable Metadata get(MetadataKey key) {
Metadata metadata = super.get(key);
if (metadata != null) {
return normalizeMetadata(metadata);
}
return null;
}

private Metadata normalizeMetadata(Metadata metadata) {
return new Metadata(metadata.getUID(), metadata.getValue(), metadata.getConfiguration().entrySet().stream()
.map(this::normalizeConfigEntry).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
}

private Map.Entry<String, Object> normalizeConfigEntry(Map.Entry<String, Object> entry) {
Object value = entry.getValue();
if (value instanceof Integer) {
BigDecimal newValue = new BigDecimal(value.toString());
newValue.setScale(0);
return Map.entry(entry.getKey(), newValue);
} else if (value instanceof Number) {
return Map.entry(entry.getKey(), new BigDecimal(value.toString()));
}

return entry;
}
}

0 comments on commit e3b90e3

Please sign in to comment.