Skip to content

Commit

Permalink
defer processing until stopConfigurationUpdate is called
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
  • Loading branch information
jimtng committed Jul 23, 2024
1 parent 234fdb8 commit 8d3c0dc
Showing 1 changed file with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
package org.openhab.core.model.thing.internal;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -51,6 +50,8 @@ public class GenericItemChannelLinkProvider extends AbstractProvider<ItemChannel
/** caches binding configurations. maps itemNames to {@link ItemChannelLink}s */
protected Map<String, Map<ChannelUID, ItemChannelLink>> itemChannelLinkMap = new ConcurrentHashMap<>();

private Map<String, Set<ChannelUID>> addedItemChannels = new ConcurrentHashMap<>();

/**
* stores information about the context of items. The map has this content
* structure: context -> Set of Item names
Expand Down Expand Up @@ -80,18 +81,6 @@ public void processBindingConfiguration(String context, String itemType, String
for (String uid : uids) {
createItemChannelLink(context, itemName, uid.trim(), configuration);
}
// clean up removed links
Map<ChannelUID, ItemChannelLink> links = itemChannelLinkMap.get(itemName);
if (links != null) {
links.keySet().removeIf(channelUID -> {
if (Arrays.stream(uids).anyMatch(channelUID.toString()::equals)) {
return false;
}
ItemChannelLink removedLink = links.get(channelUID);
notifyListenersAboutRemovedElement(removedLink);
return true;
});
}
}

private void createItemChannelLink(String context, String itemName, String channelUID, Configuration configuration)
Expand Down Expand Up @@ -133,6 +122,7 @@ private void createItemChannelLink(String context, String itemName, String chann
} else {
notifyListenersAboutUpdatedElement(oldLink, itemChannelLink);
}
addedItemChannels.computeIfAbsent(itemName, k -> new HashSet<>()).add(channelUIDObject);
}

@Override
Expand All @@ -159,6 +149,17 @@ public void stopConfigurationUpdate(String context) {
}
}
Optional.ofNullable(contextMap.get(context)).ifPresent(ctx -> ctx.removeAll(previousItemNames));

addedItemChannels.forEach((itemName, channelUID) -> {
Map<ChannelUID, ItemChannelLink> links = Objects.requireNonNull(itemChannelLinkMap.get(itemName));
Set<ChannelUID> removedChannelUIDs = new HashSet<>(links.keySet());
removedChannelUIDs.removeAll(channelUID);
removedChannelUIDs.forEach(removedChannelUID -> {
ItemChannelLink link = links.remove(removedChannelUID);
notifyListenersAboutRemovedElement(link);
});
});
addedItemChannels.clear();
}

@Override
Expand Down

0 comments on commit 8d3c0dc

Please sign in to comment.