From 0cd54539bf579448239e4e1a1896f14d7e857762 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 19 Oct 2022 10:53:57 +0200 Subject: [PATCH] [New] Updated implementations of equals()/hashCode() for state variables --- .../model/timevalue/impl/BooleanValue.java | 46 ++++++----------- .../model/timevalue/impl/FloatListValue.java | 45 ++++++----------- .../model/timevalue/impl/FloatValue.java | 46 ++++++----------- .../timevalue/impl/IntegerListValue.java | 50 ++++++------------- .../model/timevalue/impl/IntegerValue.java | 46 ++++++----------- .../model/timevalue/impl/LongValue.java | 46 ++++++----------- .../model/timevalue/impl/StringSetValue.java | 20 ++++++-- .../model/timevalue/impl/TimeValue.java | 39 ++++----------- .../model/timevalue/impl/ValueChange.java | 36 +++---------- 9 files changed, 128 insertions(+), 246 deletions(-) diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/BooleanValue.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/BooleanValue.java index 3ec946658..3d755b67e 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/BooleanValue.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/BooleanValue.java @@ -16,6 +16,7 @@ package li.strolch.model.timevalue.impl; import java.io.Serializable; +import java.util.Objects; import li.strolch.model.StrolchValueType; import li.strolch.model.timevalue.ITimeValue; @@ -86,38 +87,23 @@ public class BooleanValue implements IValue, Serializable { return new BooleanValue(this.value); } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((this.value == null) ? 0 : this.value.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - BooleanValue other = (BooleanValue) obj; - if (this.value == null) { - if (other.value != null) { - return false; - } - } else if (!this.value.equals(other.value)) { - return false; - } - return true; - } - @Override public int compareTo(IValue o) { return Boolean.compare(this.value, o.getValue()); } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + BooleanValue that = (BooleanValue) o; + return Objects.equals(value, that.value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } } diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/FloatListValue.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/FloatListValue.java index 9d826e96a..735858e2e 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/FloatListValue.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/FloatListValue.java @@ -123,36 +123,6 @@ public class FloatListValue implements IValue>, Serializable { return new FloatListValue(this.value); } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((this.value == null) ? 0 : this.value.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - FloatListValue other = (FloatListValue) obj; - if (this.value == null) { - if (other.value != null) { - return false; - } - } else if (!this.value.equals(other.value)) { - return false; - } - return true; - } - @Override public int compareTo(IValue> o) { List otherValues = o.getValue(); @@ -160,4 +130,19 @@ public class FloatListValue implements IValue>, Serializable { return 0; return Integer.compare(this.value.size(), otherValues.size()); } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + FloatListValue that = (FloatListValue) o; + return Objects.equals(value, that.value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } } diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/FloatValue.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/FloatValue.java index bb8c9bb06..c0bd57f5c 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/FloatValue.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/FloatValue.java @@ -16,6 +16,7 @@ package li.strolch.model.timevalue.impl; import java.io.Serializable; +import java.util.Objects; import li.strolch.model.StrolchValueType; import li.strolch.model.timevalue.ITimeValue; @@ -95,38 +96,23 @@ public class FloatValue implements IValue, Serializable { return new FloatValue(this.value); } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((this.value == null) ? 0 : this.value.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - FloatValue other = (FloatValue) obj; - if (this.value == null) { - if (other.value != null) { - return false; - } - } else if (!this.value.equals(other.value)) { - return false; - } - return true; - } - @Override public int compareTo(IValue o) { return Double.compare(this.value, o.getValue()); } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + FloatValue that = (FloatValue) o; + return Objects.equals(value, that.value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } } diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/IntegerListValue.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/IntegerListValue.java index 105b47412..7f6281b9a 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/IntegerListValue.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/IntegerListValue.java @@ -18,10 +18,7 @@ package li.strolch.model.timevalue.impl; import static li.strolch.model.parameter.ListParameter.VALUE_SEPARATOR2; import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; +import java.util.*; import li.strolch.model.StrolchValueType; import li.strolch.model.timevalue.ITimeValue; @@ -126,36 +123,6 @@ public class IntegerListValue implements IValue>, Serializable { return new IntegerListValue(this.value); } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((this.value == null) ? 0 : this.value.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - IntegerListValue other = (IntegerListValue) obj; - if (this.value == null) { - if (other.value != null) { - return false; - } - } else if (!this.value.equals(other.value)) { - return false; - } - return true; - } - @Override public int compareTo(IValue> o) { List otherValues = o.getValue(); @@ -163,4 +130,19 @@ public class IntegerListValue implements IValue>, Serializable { return 0; return Integer.compare(this.value.size(), otherValues.size()); } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + IntegerListValue that = (IntegerListValue) o; + return Objects.equals(value, that.value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } } diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/IntegerValue.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/IntegerValue.java index 1d691e614..f8e4dcca3 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/IntegerValue.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/IntegerValue.java @@ -16,6 +16,7 @@ package li.strolch.model.timevalue.impl; import java.io.Serializable; +import java.util.Objects; import li.strolch.model.StrolchValueType; import li.strolch.model.timevalue.ITimeValue; @@ -86,38 +87,23 @@ public class IntegerValue implements IValue, Serializable { return new IntegerValue(this.value); } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((this.value == null) ? 0 : this.value.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - IntegerValue other = (IntegerValue) obj; - if (this.value == null) { - if (other.value != null) { - return false; - } - } else if (!this.value.equals(other.value)) { - return false; - } - return true; - } - @Override public int compareTo(IValue o) { return Integer.compare(this.value, o.getValue()); } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + IntegerValue that = (IntegerValue) o; + return Objects.equals(value, that.value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } } diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/LongValue.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/LongValue.java index 53b682f43..8f3d1310e 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/LongValue.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/LongValue.java @@ -16,6 +16,7 @@ package li.strolch.model.timevalue.impl; import java.io.Serializable; +import java.util.Objects; import li.strolch.model.StrolchValueType; import li.strolch.model.timevalue.ITimeValue; @@ -86,38 +87,23 @@ public class LongValue implements IValue, Serializable { return new LongValue(this.value); } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((this.value == null) ? 0 : this.value.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - LongValue other = (LongValue) obj; - if (this.value == null) { - if (other.value != null) { - return false; - } - } else if (!this.value.equals(other.value)) { - return false; - } - return true; - } - @Override public int compareTo(IValue o) { return Long.compare(this.value, o.getValue()); } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + LongValue longValue = (LongValue) o; + return Objects.equals(value, longValue.value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } } diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/StringSetValue.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/StringSetValue.java index be101b41c..fce10c458 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/StringSetValue.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/StringSetValue.java @@ -16,10 +16,7 @@ package li.strolch.model.timevalue.impl; import java.io.Serializable; -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; +import java.util.*; import li.strolch.exception.StrolchException; import li.strolch.model.StrolchValueType; @@ -149,4 +146,19 @@ public class StringSetValue implements IValue>, Serializable { return 0; return Integer.compare(this.aStrings.size(), otherValues.size()); } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + StringSetValue that = (StringSetValue) o; + return Objects.equals(aStrings, that.aStrings); + } + + @Override + public int hashCode() { + return Objects.hash(aStrings); + } } diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/TimeValue.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/TimeValue.java index 0a51d6a8c..307f8d93e 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/TimeValue.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/TimeValue.java @@ -16,6 +16,7 @@ package li.strolch.model.timevalue.impl; import java.io.Serializable; +import java.util.Objects; import li.strolch.model.timevalue.ITimeValue; import li.strolch.model.timevalue.IValue; @@ -89,37 +90,17 @@ public class TimeValue implements ITimeValue, Serializable } @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + Long.hashCode(this.time); - result = prime * result + ((this.value == null) ? 0 : this.value.hashCode()); - return result; + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + TimeValue timeValue = (TimeValue) o; + return time == timeValue.time && Objects.equals(value, timeValue.value); } @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - @SuppressWarnings("unchecked") - TimeValue other = (TimeValue) obj; - if (this.time != other.time) { - return false; - } - if (this.value == null) { - if (other.value != null) { - return false; - } - } else if (!this.value.equals(other.value)) { - return false; - } - return true; + public int hashCode() { + return Objects.hash(time, value); } } diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/ValueChange.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/ValueChange.java index 6e8381f54..8b96df57c 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/ValueChange.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/ValueChange.java @@ -18,6 +18,7 @@ package li.strolch.model.timevalue.impl; import static li.strolch.utils.helper.StringHelper.trimOrEmpty; import java.io.Serializable; +import java.util.Objects; import li.strolch.model.timevalue.IValue; import li.strolch.model.timevalue.IValueChange; @@ -86,41 +87,18 @@ public class ValueChange implements IValueChange, Serializa } @Override - public boolean equals(Object obj) { - if (this == obj) { + public boolean equals(Object o) { + if (this == o) return true; - } - if (obj == null) { + if (o == null || getClass() != o.getClass()) return false; - } - if (getClass() != obj.getClass()) { - return false; - } - ValueChange other = (ValueChange) obj; - if (this.time == null) { - if (other.time != null) { - return false; - } - } else if (!this.time.equals(other.time)) { - return false; - } - if (this.value == null) { - if (other.value != null) { - return false; - } - } else if (!this.value.equals(other.value)) { - return false; - } - return true; + ValueChange that = (ValueChange) o; + return Objects.equals(time, that.time) && Objects.equals(value, that.value); } @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((this.time == null) ? 0 : this.time.hashCode()); - result = prime * result + ((this.value == null) ? 0 : this.value.hashCode()); - return result; + return Objects.hash(time, value); } @SuppressWarnings("nls")