mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-29 12:34:22 +02:00
[YAML] Use Objects.equals for Map + adjust hashCode methods (#5335)
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
+1
-3
@@ -25,7 +25,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.openhab.core.common.registry.AbstractProvider;
|
||||
import org.openhab.core.config.core.Configuration;
|
||||
import org.openhab.core.items.ItemProvider;
|
||||
import org.openhab.core.model.yaml.internal.util.YamlElementUtils;
|
||||
import org.openhab.core.thing.ChannelUID;
|
||||
import org.openhab.core.thing.link.ItemChannelLink;
|
||||
import org.openhab.core.thing.link.ItemChannelLinkProvider;
|
||||
@@ -107,8 +106,7 @@ public class YamlChannelLinkProvider extends AbstractProvider<ItemChannelLink> i
|
||||
if (!isIsolatedModel(modelName)) {
|
||||
notifyListenersAboutAddedElement(itemChannelLink);
|
||||
}
|
||||
} else if (!YamlElementUtils.equalsConfig(configuration.getProperties(),
|
||||
oldLink.getConfiguration().getProperties())) {
|
||||
} else if (!Objects.equals(configuration.getProperties(), oldLink.getConfiguration().getProperties())) {
|
||||
links.put(channelUIDObject, itemChannelLink);
|
||||
logger.debug("model {} updated channel link {}", modelName, itemChannelLink.getUID());
|
||||
if (!isIsolatedModel(modelName)) {
|
||||
|
||||
+2
-29
@@ -274,7 +274,7 @@ public class YamlItemDTO implements YamlElement, Cloneable {
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name, getType(), group, label, icon, format, unit, expire, autoupdate, getGroups(),
|
||||
getTags(), channel);
|
||||
getTags(), channel, channels, metadata);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -291,33 +291,6 @@ public class YamlItemDTO implements YamlElement, Cloneable {
|
||||
&& Objects.equals(unit, other.unit) && Objects.equals(expire, other.expire)
|
||||
&& Objects.equals(autoupdate, other.autoupdate) && Objects.equals(getGroups(), other.getGroups())
|
||||
&& Objects.equals(getTags(), other.getTags()) && Objects.equals(channel, other.channel)
|
||||
&& equalsChannels(channels, other.channels) && equalsMetadata(metadata, other.metadata);
|
||||
}
|
||||
|
||||
private boolean equalsChannels(@Nullable Map<@NonNull String, @NonNull Map<@NonNull String, @NonNull Object>> first,
|
||||
@Nullable Map<@NonNull String, @NonNull Map<@NonNull String, @NonNull Object>> second) {
|
||||
if (first != null && second != null) {
|
||||
if (first.size() != second.size()) {
|
||||
return false;
|
||||
} else {
|
||||
return first.entrySet().stream()
|
||||
.allMatch(e -> YamlElementUtils.equalsConfig(e.getValue(), second.get(e.getKey())));
|
||||
}
|
||||
} else {
|
||||
return first == null && second == null;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean equalsMetadata(@Nullable Map<@NonNull String, @NonNull YamlMetadataDTO> first,
|
||||
@Nullable Map<@NonNull String, @NonNull YamlMetadataDTO> second) {
|
||||
if (first != null && second != null) {
|
||||
if (first.size() != second.size()) {
|
||||
return false;
|
||||
} else {
|
||||
return first.entrySet().stream().allMatch(e -> e.getValue().equals(second.get(e.getKey())));
|
||||
}
|
||||
} else {
|
||||
return first == null && second == null;
|
||||
}
|
||||
&& Objects.equals(channels, other.channels) && Objects.equals(metadata, other.metadata);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-3
@@ -17,7 +17,6 @@ import java.util.Objects;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.core.model.yaml.internal.util.YamlElementUtils;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
@@ -45,7 +44,7 @@ public class YamlMetadataDTO {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getValue());
|
||||
return Objects.hash(getValue(), config);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -56,6 +55,6 @@ public class YamlMetadataDTO {
|
||||
return false;
|
||||
}
|
||||
YamlMetadataDTO other = (YamlMetadataDTO) obj;
|
||||
return Objects.equals(getValue(), other.getValue()) && YamlElementUtils.equalsConfig(config, other.config);
|
||||
return Objects.equals(getValue(), other.getValue()) && Objects.equals(config, other.config);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-2
@@ -27,7 +27,6 @@ import org.openhab.core.items.ItemProvider;
|
||||
import org.openhab.core.items.Metadata;
|
||||
import org.openhab.core.items.MetadataKey;
|
||||
import org.openhab.core.items.MetadataProvider;
|
||||
import org.openhab.core.model.yaml.internal.util.YamlElementUtils;
|
||||
import org.osgi.service.component.annotations.Component;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -85,7 +84,7 @@ public class YamlMetadataProvider extends AbstractProvider<Metadata> implements
|
||||
notifyListenersAboutAddedElement(md);
|
||||
}
|
||||
} else if (!md.getValue().equals(oldMd.getValue())
|
||||
|| !YamlElementUtils.equalsConfig(md.getConfiguration(), oldMd.getConfiguration())) {
|
||||
|| !Objects.equals(md.getConfiguration(), oldMd.getConfiguration())) {
|
||||
namespacesMetadataMap.put(namespace, md);
|
||||
logger.debug("model {} updated metadata {}", modelName, namespace);
|
||||
if (!isIsolatedModel(modelName)) {
|
||||
|
||||
+2
-3
@@ -108,7 +108,7 @@ public class YamlChannelDTO {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(type, getKind(), getItemType(), label, description);
|
||||
return Objects.hash(type, getKind(), getItemType(), label, description, config);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -121,7 +121,6 @@ public class YamlChannelDTO {
|
||||
YamlChannelDTO other = (YamlChannelDTO) obj;
|
||||
return Objects.equals(type, other.type) && getKind() == other.getKind()
|
||||
&& Objects.equals(getItemType(), other.getItemType()) && Objects.equals(label, other.label)
|
||||
&& Objects.equals(description, other.description)
|
||||
&& YamlElementUtils.equalsConfig(config, other.config);
|
||||
&& Objects.equals(description, other.description) && Objects.equals(config, other.config);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-16
@@ -22,7 +22,6 @@ import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.core.config.core.Configuration;
|
||||
import org.openhab.core.model.yaml.YamlElement;
|
||||
import org.openhab.core.model.yaml.YamlElementName;
|
||||
import org.openhab.core.model.yaml.internal.util.YamlElementUtils;
|
||||
import org.openhab.core.thing.ChannelUID;
|
||||
import org.openhab.core.thing.ThingUID;
|
||||
|
||||
@@ -143,7 +142,7 @@ public class YamlThingDTO implements YamlElement, Cloneable {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(uid, isBridge(), bridge, label, location);
|
||||
return Objects.hash(uid, isBridge(), bridge, label, location, config, channels);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -156,19 +155,6 @@ public class YamlThingDTO implements YamlElement, Cloneable {
|
||||
YamlThingDTO other = (YamlThingDTO) obj;
|
||||
return Objects.equals(uid, other.uid) && isBridge() == other.isBridge() && Objects.equals(bridge, other.bridge)
|
||||
&& Objects.equals(label, other.label) && Objects.equals(location, other.location)
|
||||
&& YamlElementUtils.equalsConfig(config, other.config) && equalsChannels(channels, other.channels);
|
||||
}
|
||||
|
||||
private boolean equalsChannels(@Nullable Map<@NonNull String, @NonNull YamlChannelDTO> first,
|
||||
@Nullable Map<@NonNull String, @NonNull YamlChannelDTO> second) {
|
||||
if (first != null && second != null) {
|
||||
if (first.size() != second.size()) {
|
||||
return false;
|
||||
} else {
|
||||
return first.entrySet().stream().allMatch(e -> e.getValue().equals(second.get(e.getKey())));
|
||||
}
|
||||
} else {
|
||||
return first == null && second == null;
|
||||
}
|
||||
&& Objects.equals(config, other.config) && Objects.equals(channels, other.channels);
|
||||
}
|
||||
}
|
||||
|
||||
-19
@@ -12,10 +12,6 @@
|
||||
*/
|
||||
package org.openhab.core.model.yaml.internal.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.core.library.CoreItemFactory;
|
||||
@@ -30,21 +26,6 @@ import org.openhab.core.util.StringUtils;
|
||||
@NonNullByDefault
|
||||
public class YamlElementUtils {
|
||||
|
||||
public static boolean equalsConfig(@Nullable Map<String, Object> first, @Nullable Map<String, Object> second) {
|
||||
if (first != null && second != null) {
|
||||
return first.size() != second.size() ? false
|
||||
: first.entrySet().stream().allMatch(e -> equalsConfigValue(e.getValue(), second.get(e.getKey())));
|
||||
} else {
|
||||
return first == null && second == null;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean equalsConfigValue(Object first, @Nullable Object second) {
|
||||
return (first instanceof List firstList && second instanceof List secondList)
|
||||
? Arrays.equals(firstList.toArray(), secondList.toArray())
|
||||
: first.equals(second);
|
||||
}
|
||||
|
||||
public static @Nullable String getAdjustedItemType(@Nullable String type) {
|
||||
return type == null ? null : StringUtils.capitalize(type);
|
||||
}
|
||||
|
||||
+14
-5
@@ -524,6 +524,15 @@ public class YamlItemDTOTest {
|
||||
item2.channels = Map.of("binding:type:uid:channelid2", Map.of());
|
||||
assertTrue(item1.equals(item2));
|
||||
assertEquals(item1.hashCode(), item2.hashCode());
|
||||
item2.channels = Map.of("binding:type:uid:channelid2", Map.of(), "binding:type:uid:channelid3", Map.of());
|
||||
assertFalse(item1.equals(item2));
|
||||
item1.channels = Map.of("binding:type:uid:channelid2", Map.of(), "binding:type:uid:channelid3", Map.of());
|
||||
item2.channels = Map.of("binding:type:uid:channelid2", Map.of(), "binding:type:uid:channelid3", Map.of());
|
||||
assertTrue(item1.equals(item2));
|
||||
assertEquals(item1.hashCode(), item2.hashCode());
|
||||
item2.channels = Map.of("binding:type:uid:channelid3", Map.of(), "binding:type:uid:channelid2", Map.of());
|
||||
assertTrue(item1.equals(item2));
|
||||
assertEquals(item1.hashCode(), item2.hashCode());
|
||||
item1.channels = Map.of("binding:type:uid:channelid2",
|
||||
Map.of("profile", "anyprofile", "param", "profile param"));
|
||||
item2.channels = Map.of("binding:type:uid:channelid2", Map.of());
|
||||
@@ -532,11 +541,11 @@ public class YamlItemDTOTest {
|
||||
Map.of("profile", "anyprofile", "param", "profile param"));
|
||||
assertTrue(item1.equals(item2));
|
||||
assertEquals(item1.hashCode(), item2.hashCode());
|
||||
item1.channels = Map.of("binding:type:uid:channelid2", Map.of());
|
||||
item2.channels = Map.of("binding:type:uid:channelid2", Map.of(), "binding:type:uid:channelid3", Map.of());
|
||||
assertFalse(item1.equals(item2));
|
||||
item1.channels = Map.of("binding:type:uid:channelid2", Map.of(), "binding:type:uid:channelid3", Map.of());
|
||||
item2.channels = Map.of("binding:type:uid:channelid2", Map.of());
|
||||
item2.channels = Map.of("binding:type:uid:channelid2",
|
||||
Map.of("param", "profile param", "profile", "anyprofile"));
|
||||
assertTrue(item1.equals(item2));
|
||||
assertEquals(item1.hashCode(), item2.hashCode());
|
||||
item2.channels = Map.of("binding:type:uid:channelid2", Map.of("profile", "anyprofile"));
|
||||
assertFalse(item1.equals(item2));
|
||||
}
|
||||
|
||||
|
||||
+7
@@ -81,6 +81,10 @@ public class YamlMetadataDTOTest {
|
||||
md2.config = null;
|
||||
assertTrue(md1.equals(md2));
|
||||
assertEquals(md1.hashCode(), md2.hashCode());
|
||||
md1.config = Map.of();
|
||||
md2.config = Map.of();
|
||||
assertTrue(md1.equals(md2));
|
||||
assertEquals(md1.hashCode(), md2.hashCode());
|
||||
md1.config = Map.of("param1", "value", "param2", 50, "param3", true);
|
||||
md2.config = null;
|
||||
assertFalse(md1.equals(md2));
|
||||
@@ -99,5 +103,8 @@ public class YamlMetadataDTOTest {
|
||||
md2.config = Map.of("param1", "value", "param2", 50, "param3", true);
|
||||
assertTrue(md1.equals(md2));
|
||||
assertEquals(md1.hashCode(), md2.hashCode());
|
||||
md2.config = Map.of("param3", true, "param2", 50, "param1", "value");
|
||||
assertTrue(md1.equals(md2));
|
||||
assertEquals(md1.hashCode(), md2.hashCode());
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -281,6 +281,10 @@ public class YamlChannelDTOTest {
|
||||
ch2.config = null;
|
||||
assertTrue(ch1.equals(ch2));
|
||||
assertEquals(ch1.hashCode(), ch2.hashCode());
|
||||
ch1.config = Map.of();
|
||||
ch2.config = Map.of();
|
||||
assertTrue(ch1.equals(ch2));
|
||||
assertEquals(ch1.hashCode(), ch2.hashCode());
|
||||
ch1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
ch2.config = null;
|
||||
assertFalse(ch1.equals(ch2));
|
||||
@@ -290,27 +294,23 @@ public class YamlChannelDTOTest {
|
||||
ch1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
ch2.config = Map.of("param1", "other value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
assertFalse(ch1.equals(ch2));
|
||||
ch1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
ch2.config = Map.of("param1", "value", "param2", 25, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
assertFalse(ch1.equals(ch2));
|
||||
ch1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
ch2.config = Map.of("param1", "value", "param2", 50, "param3", false, "param4", List.of("val 1", "val 2"));
|
||||
assertFalse(ch1.equals(ch2));
|
||||
ch1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
ch2.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "value 2"));
|
||||
assertFalse(ch1.equals(ch2));
|
||||
ch1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
ch2.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1"));
|
||||
assertFalse(ch1.equals(ch2));
|
||||
ch1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
ch2.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", 75);
|
||||
assertFalse(ch1.equals(ch2));
|
||||
ch1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
ch2.config = Map.of("param1", "value", "param2", 50, "param3", true);
|
||||
assertFalse(ch1.equals(ch2));
|
||||
ch1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
ch2.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
assertTrue(ch1.equals(ch2));
|
||||
assertEquals(ch1.hashCode(), ch2.hashCode());
|
||||
ch2.config = Map.of("param4", List.of("val 1", "val 2"), "param3", true, "param2", 50, "param1", "value");
|
||||
assertTrue(ch1.equals(ch2));
|
||||
assertEquals(ch1.hashCode(), ch2.hashCode());
|
||||
}
|
||||
}
|
||||
|
||||
+18
-7
@@ -261,6 +261,10 @@ public class YamlThingDTOTest {
|
||||
th2.config = null;
|
||||
assertTrue(th1.equals(th2));
|
||||
assertEquals(th1.hashCode(), th2.hashCode());
|
||||
th1.config = Map.of();
|
||||
th2.config = Map.of();
|
||||
assertTrue(th1.equals(th2));
|
||||
assertEquals(th1.hashCode(), th2.hashCode());
|
||||
th1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
th2.config = null;
|
||||
assertFalse(th1.equals(th2));
|
||||
@@ -270,28 +274,24 @@ public class YamlThingDTOTest {
|
||||
th1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
th2.config = Map.of("param1", "other value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
assertFalse(th1.equals(th2));
|
||||
th1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
th2.config = Map.of("param1", "value", "param2", 25, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
assertFalse(th1.equals(th2));
|
||||
th1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
th2.config = Map.of("param1", "value", "param2", 50, "param3", false, "param4", List.of("val 1", "val 2"));
|
||||
assertFalse(th1.equals(th2));
|
||||
th1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
th2.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "value 2"));
|
||||
assertFalse(th1.equals(th2));
|
||||
th1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
th2.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1"));
|
||||
assertFalse(th1.equals(th2));
|
||||
th1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
th2.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", 75);
|
||||
assertFalse(th1.equals(th2));
|
||||
th1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
th2.config = Map.of("param1", "value", "param2", 50, "param3", true);
|
||||
assertFalse(th1.equals(th2));
|
||||
th1.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
th2.config = Map.of("param1", "value", "param2", 50, "param3", true, "param4", List.of("val 1", "val 2"));
|
||||
assertTrue(th1.equals(th2));
|
||||
assertEquals(th1.hashCode(), th2.hashCode());
|
||||
th2.config = Map.of("param4", List.of("val 1", "val 2"), "param3", true, "param2", 50, "param1", "value");
|
||||
assertTrue(th1.equals(th2));
|
||||
assertEquals(th1.hashCode(), th2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -320,13 +320,24 @@ public class YamlThingDTOTest {
|
||||
assertTrue(th1.equals(th2));
|
||||
assertEquals(th1.hashCode(), th2.hashCode());
|
||||
|
||||
th1.channels = Map.of("channel1", ch1, "channel2", ch2);
|
||||
th2.channels = Map.of("channel2", ch4, "channel1", ch3);
|
||||
assertTrue(th1.equals(th2));
|
||||
assertEquals(th1.hashCode(), th2.hashCode());
|
||||
|
||||
th1.channels = Map.of("channel1", ch1);
|
||||
th2.channels = null;
|
||||
assertFalse(th1.equals(th2));
|
||||
th1.channels = Map.of("channel1", ch1);
|
||||
th2.channels = Map.of();
|
||||
assertFalse(th1.equals(th2));
|
||||
|
||||
th1.channels = null;
|
||||
th2.channels = Map.of("channel1", ch3);
|
||||
assertFalse(th1.equals(th2));
|
||||
th1.channels = Map.of();
|
||||
th2.channels = Map.of("channel1", ch3);
|
||||
assertFalse(th1.equals(th2));
|
||||
|
||||
th1.channels = Map.of("channel1", ch1, "channel2", ch2);
|
||||
th2.channels = Map.of("channel1", ch3);
|
||||
|
||||
Reference in New Issue
Block a user