Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Removed unnecessary equality checks, other cleanup

Christian Bauerad89c6e379f2
diff --git a/container/src/main/java/org/openremote/container/web/WebServiceExceptions.java b/container/src/main/java/org/openremote/container/web/WebServiceExceptions.java
index 44c498226..c78a043cd 100644
--- a/container/src/main/java/org/openremote/container/web/WebServiceExceptions.java
+++ b/container/src/main/java/org/openremote/container/web/WebServiceExceptions.java
@@ -48,6 +48,7 @@ import static javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE;
* debug logging is enabled. We never want to send a stacktrace or some crappy HTML to a
* client in production.
* <p>
+ * TODO: Fine-grained logging for exception, filter which should be logged at INFO or WARN in production
* TODO: Websocket session errors
*/
public class WebServiceExceptions {
diff --git a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java
index 1945262f9..3b62f1fe3 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java
@@ -273,11 +273,10 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpda
sb.append(" from ASSET A cross join (select * from REALM) as R ");
-
if (query.parent != null && !query.parent.noParent) {
sb.append(" cross join ASSET P ");
} else {
- sb.append(" left outer join ASSET P on A.PARENT_ID = P.ID");
+ sb.append(" left outer join ASSET P on A.PARENT_ID = P.ID ");
}
if (query.id != null) {
@@ -383,7 +382,7 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpda
.append(") ");
}
}
- // TODO Implement AssetQuery.DecimalSearch
+ // TODO Implement AssetQuery.AttributeRef
}
return new Pair<>(sb.toString(), binders);
diff --git a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java
index 4a7338de6..aa3b870be 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java
@@ -134,40 +134,35 @@ public class RulesService extends RouteBuilder implements ContainerService, Cons
Attributes oldAttributes = new Attributes((JsonObject)persistenceEvent.getPreviousState()[attributesIndex]);
Attributes newAttributes = new Attributes((JsonObject)persistenceEvent.getCurrentState()[attributesIndex]);
- List<Attribute> oldLinkedAttributes = Arrays.stream(oldAttributes.get())
- .filter(attribute -> {
- MetaItem rulesFact = attribute.firstMetaItem(AssetMeta.RULES_FACT);
- return rulesFact != null && rulesFact.getValueAsBoolean();
- })
+ List<Attribute> oldFactAttributes = Arrays.stream(oldAttributes.get())
+ .filter(Attribute::isRulesFact)
.collect(Collectors.toList());
- List<Attribute> newLinkedAttributes = Arrays.stream(newAttributes.get())
- .filter(attribute -> {
- MetaItem rulesFact = attribute.firstMetaItem(AssetMeta.RULES_FACT);
- return rulesFact != null && rulesFact.getValueAsBoolean();
- })
+ List<Attribute> newFactAttributes = Arrays.stream(newAttributes.get())
+ .filter(Attribute::isRulesFact)
.collect(Collectors.toList());
+ // TODO Compare using our own Json equality routine, jsEquals() is broken
// Compare attributes using JsonObject.jsEquals()
- // Retract facts for attributes that are in oldLinkedAttributes but not in newLinkedAttributes
- oldLinkedAttributes
+ // Retract facts for attributes that are in oldFactAttributes but not in newFactAttributes
+ oldFactAttributes
.stream()
- .filter(oldLink -> newLinkedAttributes
+ .filter(oldFactAttribute -> newFactAttributes
.stream()
- .noneMatch(newLink -> oldLink.getJsonObject().jsEquals(newLink.getJsonObject())))
- .forEach(obsoleteLink -> {
- AssetUpdate update = new AssetUpdate(serverAsset, obsoleteLink);
+ .noneMatch(newFactAttribute -> oldFactAttribute.getJsonObject().jsEquals(newFactAttribute.getJsonObject())))
+ .forEach(obsoleteFactAttribute -> {
+ AssetUpdate update = new AssetUpdate(serverAsset, obsoleteFactAttribute);
retractFact(update);
});
- // Insert facts for attributes that are in newLinkedAttributes but not in oldLinkedAttributes
- newLinkedAttributes
+ // Insert facts for attributes that are in newFactAttributes but not in oldFactAttributes
+ newFactAttributes
.stream()
- .filter(newFact -> oldLinkedAttributes
+ .filter(newFactAttribute -> oldFactAttributes
.stream()
- .noneMatch(oldFact -> newFact.getJsonObject().jsEquals(newFact.getJsonObject())))
- .forEach(newLinkedAttribute -> {
- AssetUpdate update = new AssetUpdate(serverAsset, newLinkedAttribute);
+ .noneMatch(oldFactAttribute -> newFactAttribute.getJsonObject().jsEquals(newFactAttribute.getJsonObject())))
+ .forEach(newFactAttribute -> {
+ AssetUpdate update = new AssetUpdate(serverAsset, newFactAttribute);
// Set the status to completed already so rules cannot interfere with this initial insert
insertFact(update, true);
});
diff --git a/model/src/main/java/org/openremote/model/Attribute.java b/model/src/main/java/org/openremote/model/Attribute.java
index 1e2c7be9a..a0d30aff5 100644
--- a/model/src/main/java/org/openremote/model/Attribute.java
+++ b/model/src/main/java/org/openremote/model/Attribute.java
@@ -26,9 +26,7 @@ import org.openremote.model.asset.AssetMeta;
import java.util.NoSuchElementException;
-import static org.openremote.model.asset.AssetMeta.PROTECTED;
-import static org.openremote.model.asset.AssetMeta.READ_ONLY;
-import static org.openremote.model.asset.AssetMeta.STORE_DATA_POINTS;
+import static org.openremote.model.asset.AssetMeta.*;
/**
* Convenience overlay API for {@link JsonObject}.
@@ -182,6 +180,7 @@ public class Attribute extends AbstractValueTimestampHolder<Attribute> {
/* ############################################################################### */
// The following methods are only applicable to attributes of assets, but they are very convenient here...
+ // TODO Introduce AssetAttribute class
public boolean hasMetaItem(AssetMeta assetMeta) {
return hasMetaItem(assetMeta.getName());
@@ -203,6 +202,10 @@ public class Attribute extends AbstractValueTimestampHolder<Attribute> {
return hasMetaItem(STORE_DATA_POINTS) && firstMetaItem(STORE_DATA_POINTS).isValueTrue();
}
+ public boolean isRulesFact() {
+ return hasMetaItem(RULES_FACT) && firstMetaItem(RULES_FACT).isValueTrue();
+ }
+
/* ############################################################################### */
}
diff --git a/model/src/main/java/org/openremote/model/AttributeEvent.java b/model/src/main/java/org/openremote/model/AttributeEvent.java
index ddcd59392..4c15bd440 100644
--- a/model/src/main/java/org/openremote/model/AttributeEvent.java
+++ b/model/src/main/java/org/openremote/model/AttributeEvent.java
@@ -23,8 +23,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreType;
import elemental.json.JsonValue;
/**
- * A timestamped {@link AttributeState}. Two attribute events
- * are equal if their timestamps and attribute states are equal.
+ * A timestamped {@link AttributeState}.
*/
@JsonIgnoreType
public class AttributeEvent extends Event {
@@ -65,10 +64,6 @@ public class AttributeEvent extends Event {
return getAttributeRef().getEntityId();
}
- public String getEntityName() {
- return getAttributeRef().getEntityName();
- }
-
public String getAttributeName() {
return getAttributeRef().getAttributeName();
}
@@ -77,24 +72,6 @@ public class AttributeEvent extends Event {
return getAttributeState().getValue();
}
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- AttributeEvent that = (AttributeEvent) o;
-
- return this.timestamp == that.timestamp &&
- attributeState.equals(that.attributeState);
- }
-
- @Override
- public int hashCode() {
- int result = new Long(timestamp).intValue();
- result = 31 * result + attributeState.hashCode();
- return result;
- }
-
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
diff --git a/model/src/main/java/org/openremote/model/AttributeRef.java b/model/src/main/java/org/openremote/model/AttributeRef.java
index 724d5a2bf..c7992999c 100644
--- a/model/src/main/java/org/openremote/model/AttributeRef.java
+++ b/model/src/main/java/org/openremote/model/AttributeRef.java
@@ -26,17 +26,16 @@ import elemental.json.JsonArray;
* A reference to an entity and an {@link Attribute}.
* <p>
* The {@link #entityId} and {@link #attributeName} are required to identify
- * an entity's attribute. The {@link #entityName} can be <code>null</code> if the
- * reference has not been resolved.
+ * an entity's attribute.
+ *
+ * Two attribute references are {@link #equals} if they reference the same entity
+ * and attribute.
*/
public class AttributeRef {
protected String entityId;
protected String attributeName;
- // Optional, can be null if reference has not been resolved
- protected String entityName;
-
public AttributeRef() {
}
@@ -57,14 +56,6 @@ public class AttributeRef {
return attributeName;
}
- public String getEntityName() {
- return entityName;
- }
-
- public void setEntityName(String entityName) {
- this.entityName = entityName;
- }
-
public JsonArray asJsonValue() {
JsonArray jsonArray = Json.createArray();
jsonArray.set(0, Json.create(getEntityId()));
@@ -91,7 +82,6 @@ public class AttributeRef {
public String toString() {
return getClass().getSimpleName() + "{" +
"entityId='" + entityId + '\'' +
- ", entityName='" + entityName + '\'' +
", attributeName='" + attributeName + '\'' +
'}';
}
diff --git a/model/src/main/java/org/openremote/model/AttributeState.java b/model/src/main/java/org/openremote/model/AttributeState.java
index 75e18999b..6f76a61ce 100644
--- a/model/src/main/java/org/openremote/model/AttributeState.java
+++ b/model/src/main/java/org/openremote/model/AttributeState.java
@@ -23,8 +23,7 @@ import elemental.json.Json;
import elemental.json.JsonValue;
/**
- * The desired or current or past state of an {@link AttributeRef}. Two attribute
- * states are equal if their attribute references and values are equal.
+ * The desired or current or past state of an {@link AttributeRef}.
*/
public class AttributeState {
@@ -47,24 +46,6 @@ public class AttributeState {
return value;
}
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- AttributeState that = (AttributeState) o;
-
- return getAttributeRef().equals(that.getAttributeRef()) &&
- getValue().jsEquals(that.getValue()); // Note the jsEquals() operation!
- }
-
- @Override
- public int hashCode() {
- int result = getAttributeRef().hashCode();
- result = 31 * result + getValue().hashCode();
- return result;
- }
-
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
diff --git a/model/src/main/java/org/openremote/model/asset/AssetQuery.java b/model/src/main/java/org/openremote/model/asset/AssetQuery.java
index d13b759c5..9a5df2933 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetQuery.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetQuery.java
@@ -170,6 +170,33 @@ public class AssetQuery {
}
}
+ static public class AttributeRef {
+ public String entityId;
+ public String attributeName;
+
+ public AttributeRef() {
+ }
+
+ public AttributeRef(String entityId) {
+ this.entityId = entityId;
+ }
+
+ public AttributeRef(String entityId, String attributeName) {
+ this.entityId = entityId;
+ this.attributeName = attributeName;
+ }
+
+ public AttributeRef entityId(String entityId) {
+ this.entityId = entityId;
+ return this;
+ }
+
+ public AttributeRef attributeName(String attributeName) {
+ this.attributeName = attributeName;
+ return this;
+ }
+ }
+
static public class AttributeMeta {
public StringSearch itemNameSearch;
public Search itemValueSearch;
@@ -272,9 +299,11 @@ public class AssetQuery {
public StringSearch name;
public Parent parent;
+ // TODO Implement descendants restriction
public Realm realm;
public String userId;
public StringSearch type;
+ public AttributeRef attributeRef;
public AttributeMeta attributeMeta;
public OrderBy orderBy = new OrderBy("createdOn");
@@ -320,6 +349,11 @@ public class AssetQuery {
return type(new StringSearch(assetType.getValue()));
}
+ public AssetQuery attributeRef(AttributeRef attributeRef) {
+ this.attributeRef = attributeRef;
+ return this;
+ }
+
public AssetQuery attributeMeta(AttributeMeta attributeMeta) {
this.attributeMeta = attributeMeta;
return this;
diff --git a/model/src/main/java/org/openremote/model/asset/AssetUpdate.java b/model/src/main/java/org/openremote/model/asset/AssetUpdate.java
index eb53419a5..9e1e35107 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetUpdate.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetUpdate.java
@@ -81,7 +81,7 @@ public class AssetUpdate {
final protected String assetRealmId;
- final protected String assetRealmName;
+ final protected String assetTenantRealm;
final protected double[] coordinates;
@@ -110,7 +110,7 @@ public class AssetUpdate {
this.assetParentName = asset.getParentName();
this.assetParentType = asset.getParentType();
this.assetRealmId = asset.getRealmId();
- this.assetRealmName = asset.getTenantRealm();
+ this.assetTenantRealm = asset.getTenantRealm();
this.coordinates = asset.getCoordinates();
this.oldValue = oldValue;
this.oldValueTimestamp = oldValueTimestamp;
@@ -161,8 +161,8 @@ public class AssetUpdate {
return assetRealmId;
}
- public String getAssetRealmName() {
- return assetRealmName;
+ public String getAssetTenantRealm() {
+ return assetTenantRealm;
}
public double[] getCoordinates() {
@@ -221,31 +221,38 @@ public class AssetUpdate {
if (!isCompleted()) {
return attribute;
}
-
- return null;
+ throw new IllegalStateException("Instance is immutable, status '" + getStatus() + "': " + this);
}
public void setValue(JsonValue value) {
if (!isCompleted()) {
attribute.setValue(value);
+ } else {
+ throw new IllegalStateException("Instance is immutable, status '" + getStatus() + "': " + this);
}
}
public void setValueUnchecked(JsonValue value) {
if (!isCompleted()) {
attribute.setValueUnchecked(value);
+ } else {
+ throw new IllegalStateException("Instance is immutable, status '" + getStatus() + "': " + this);
}
}
public void setStatus(Status status) {
if (!isCompleted()) {
this.status = status;
+ } else {
+ throw new IllegalStateException("Instance is immutable, status '" + getStatus() + "': " + this);
}
}
public void setError(Throwable error) {
if (!isCompleted()) {
this.error = error;
+ } else {
+ throw new IllegalStateException("Instance is immutable, status '" + getStatus() + "': " + this);
}
}
@@ -256,6 +263,8 @@ public class AssetUpdate {
AssetUpdate that = (AssetUpdate) o;
+ // TODO Don't use jsEquals(), write own comparison by value
+
return assetId.equals(that.assetId) &&
getAttributeName().equalsIgnoreCase(that.getAttributeName()) &&
getValue().jsEquals(that.getValue()) &&
@@ -266,7 +275,9 @@ public class AssetUpdate {
@Override
public int hashCode() {
- return assetId.hashCode() + getAttributeName().hashCode() + getValue().hashCode() + Long.hashCode(getValueTimestamp()) + (oldValue != null ? oldValue.hashCode() : 0) + Long.hashCode(oldValueTimestamp);
+ return assetId.hashCode() + getAttributeName().hashCode() + getValue().hashCode()
+ + Long.hashCode(getValueTimestamp())
+ + (oldValue != null ? oldValue.hashCode() : 0) + Long.hashCode(oldValueTimestamp);
}
@Override