[YAML] Enhance removedModel method in all providers (#5341)

The enhancement consists in just getting the UID of the elements to be removed rather than building a full object.

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo
2026-02-14 08:21:06 +01:00
committed by GitHub
parent 5391ba2cbf
commit 3706443006
3 changed files with 17 additions and 15 deletions
@@ -161,11 +161,8 @@ public class YamlItemProvider extends AbstractProvider<Item> implements ItemProv
@Override
public void removedModel(String modelName, Collection<YamlItemDTO> elements) {
List<Item> removed = elements.stream().map(elt -> mapItem(elt)).filter(Objects::nonNull).toList();
Collection<Item> modelItems = itemsMap.getOrDefault(modelName, List.of());
removed.forEach(item -> {
String name = item.getName();
elements.stream().map(elt -> elt.name).forEach(name -> {
modelItems.stream().filter(i -> i.getName().equals(name)).findFirst().ifPresentOrElse(oldItem -> {
modelItems.remove(oldItem);
logger.debug("model {} removed item {}", modelName, name);
@@ -94,14 +94,12 @@ public class YamlSemanticTagProvider extends AbstractProvider<SemanticTag>
@Override
public void removedModel(String modelName, Collection<YamlSemanticTagDTO> elements) {
List<SemanticTag> removed = elements.stream().map(this::mapSemanticTag)
.sorted(Comparator.comparing(SemanticTag::getUID).reversed()).toList();
removed.forEach(t -> {
tags.stream().filter(tag -> tag.getUID().equals(t.getUID())).findFirst().ifPresentOrElse(oldTag -> {
elements.stream().map(elt -> elt.uid).sorted(Comparator.reverseOrder()).forEach(uid -> {
tags.stream().filter(tag -> tag.getUID().equals(uid)).findFirst().ifPresentOrElse(oldTag -> {
tags.remove(oldTag);
logger.debug("model {} removed tag {}", modelName, t.getUID());
logger.debug("model {} removed tag {}", modelName, uid);
notifyListenersAboutRemovedElement(oldTag);
}, () -> logger.debug("model {} tag {} not found", modelName, t.getUID()));
}, () -> logger.debug("model {} tag {} not found", modelName, uid));
});
}
@@ -222,16 +222,15 @@ public class YamlThingProvider extends AbstractProvider<Thing>
@Override
public void removedModel(String modelName, Collection<YamlThingDTO> elements) {
boolean isolated = isIsolatedModel(modelName);
List<Thing> removed = elements.stream().map(t -> mapThing(t, isolated)).filter(Objects::nonNull).toList();
Collection<Thing> modelThings = thingsMap.getOrDefault(modelName, List.of());
removed.forEach(t -> {
modelThings.stream().filter(th -> th.getUID().equals(t.getUID())).findFirst().ifPresentOrElse(oldThing -> {
elements.stream().map(this::buildThingUID).filter(Objects::nonNull).forEach(uid -> {
modelThings.stream().filter(th -> th.getUID().equals(uid)).findFirst().ifPresentOrElse(oldThing -> {
modelThings.remove(oldThing);
logger.debug("model {} removed thing {}", modelName, t.getUID());
logger.debug("model {} removed thing {}", modelName, uid);
if (!isolated) {
notifyListenersAboutRemovedElement(oldThing);
}
}, () -> logger.debug("model {} thing {} not found", modelName, t.getUID()));
}, () -> logger.debug("model {} thing {} not found", modelName, uid));
});
if (modelThings.isEmpty()) {
thingsMap.remove(modelName);
@@ -353,6 +352,14 @@ public class YamlThingProvider extends AbstractProvider<Thing>
return bundle == null ? null : bundle.getSymbolicName();
}
private @Nullable ThingUID buildThingUID(YamlThingDTO thingDto) {
try {
return new ThingUID(thingDto.uid);
} catch (IllegalArgumentException e) {
return null;
}
}
private @Nullable Thing mapThing(YamlThingDTO thingDto, boolean isolatedModel) {
try {
ThingUID thingUID = new ThingUID(thingDto.uid);