From 4b6820a8ec78410eb22f90e87e3e36572b9ba1f5 Mon Sep 17 00:00:00 2001 From: Antti Villberg Date: Sat, 11 Jan 2020 13:09:28 +0200 Subject: [PATCH] Implement equals/hashCode for PropertyInfo Take immutability into account. gitlab #442 Change-Id: I0c2fe55c691537e91af6449ec544a4eec824092f --- .../db/layer0/request/PropertyInfo.java | 50 ++++++++++--------- .../StandardGraphPropertyVariable.java | 25 +++------- 2 files changed, 32 insertions(+), 43 deletions(-) diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PropertyInfo.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PropertyInfo.java index 6ecfb8ead..0baa50462 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PropertyInfo.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PropertyInfo.java @@ -30,6 +30,7 @@ import org.simantics.utils.datastructures.Pair; public class PropertyInfo { public final Resource predicate; public final String name; + public final boolean isImmutable; public final boolean isHasProperty; public final boolean isFunctional; public final Set classifications; @@ -42,9 +43,10 @@ public class PropertyInfo { public final Map> subliteralPredicates; public final ValueAccessor valueAccessor; public final boolean hasEnumerationRange; - public PropertyInfo(Resource predicate, String name, boolean isFunctional, boolean isHasProperty, Set classifications, VariableBuilder builder, Resource literalRange, Datatype requiredDatatype, String definedUnit, String requiredValueType, Binding defaultBinding, Map> subliteralPredicates, ValueAccessor valueAccessor, boolean hasEnumerationRange) { + public PropertyInfo(Resource predicate, String name, boolean isImmutable, boolean isFunctional, boolean isHasProperty, Set classifications, VariableBuilder builder, Resource literalRange, Datatype requiredDatatype, String definedUnit, String requiredValueType, Binding defaultBinding, Map> subliteralPredicates, ValueAccessor valueAccessor, boolean hasEnumerationRange) { this.predicate = predicate; this.name = name; + this.isImmutable = isImmutable; this.isFunctional = isFunctional; this.isHasProperty = isHasProperty; this.classifications = classifications; @@ -59,7 +61,6 @@ public class PropertyInfo { this.hasEnumerationRange = hasEnumerationRange; } public static PropertyInfo make(ReadGraph graph, Resource predicate, String name, boolean isFunctional, boolean isHasProperty, Set classifications, VariableBuilder builder, Resource literalRange, Datatype requiredDatatype, String definedUnit, String requiredValueType, Map> subliteralPredicates, ValueAccessor valueAccessor, boolean hasEnumerationRange) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); if(literalRange != null) { Collection dts = graph.getAssertedObjects(literalRange, L0.HasDataType); @@ -68,11 +69,8 @@ public class PropertyInfo { if(requiredDatatype == null) requiredDatatype = dt; } } - Binding defaultBinding = requiredDatatype != null ? Bindings.getBinding(requiredDatatype) : null; - - return new PropertyInfo(predicate, name, isFunctional, isHasProperty, classifications, builder, literalRange, requiredDatatype, definedUnit, requiredValueType, defaultBinding, subliteralPredicates, valueAccessor, hasEnumerationRange); - + return new PropertyInfo(predicate, name, graph.isImmutable(predicate), isFunctional, isHasProperty, classifications, builder, literalRange, requiredDatatype, definedUnit, requiredValueType, defaultBinding, subliteralPredicates, valueAccessor, hasEnumerationRange); } public boolean hasClassification(String classification) { return classifications.contains(classification); @@ -107,20 +105,22 @@ public class PropertyInfo { public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + ((builder == null) ? 0 : builder.hashCode()); - result = prime * result + ((classifications == null) ? 0 : classifications.hashCode()); - result = prime * result + ((defaultBinding == null) ? 0 : defaultBinding.hashCode()); - result = prime * result + ((definedUnit == null) ? 0 : definedUnit.hashCode()); - result = prime * result + (hasEnumerationRange ? 1231 : 1237); - result = prime * result + (isFunctional ? 1231 : 1237); - result = prime * result + (isHasProperty ? 1231 : 1237); - result = prime * result + ((literalRange == null) ? 0 : literalRange.hashCode()); - result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((predicate == null) ? 0 : predicate.hashCode()); - result = prime * result + ((requiredDatatype == null) ? 0 : requiredDatatype.hashCode()); - result = prime * result + ((requiredValueType == null) ? 0 : requiredValueType.hashCode()); - result = prime * result + ((subliteralPredicates == null) ? 0 : subliteralPredicates.hashCode()); - result = prime * result + ((valueAccessor == null) ? 0 : valueAccessor.hashCode()); + if (!isImmutable) { + result = prime * result + ((builder == null) ? 0 : builder.hashCode()); + result = prime * result + ((classifications == null) ? 0 : classifications.hashCode()); + result = prime * result + ((defaultBinding == null) ? 0 : defaultBinding.hashCode()); + result = prime * result + ((definedUnit == null) ? 0 : definedUnit.hashCode()); + result = prime * result + (hasEnumerationRange ? 1231 : 1237); + result = prime * result + (isFunctional ? 1231 : 1237); + result = prime * result + (isHasProperty ? 1231 : 1237); + result = prime * result + ((literalRange == null) ? 0 : literalRange.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((requiredDatatype == null) ? 0 : requiredDatatype.hashCode()); + result = prime * result + ((requiredValueType == null) ? 0 : requiredValueType.hashCode()); + result = prime * result + ((subliteralPredicates == null) ? 0 : subliteralPredicates.hashCode()); + result = prime * result + ((valueAccessor == null) ? 0 : valueAccessor.hashCode()); + } return result; } @Override @@ -132,6 +132,13 @@ public class PropertyInfo { if (getClass() != obj.getClass()) return false; PropertyInfo other = (PropertyInfo) obj; + if (predicate == null) { + if (other.predicate != null) + return false; + } else if (!predicate.equals(other.predicate)) + return false; + if(isImmutable) + return true; if (builder == null) { if (other.builder != null) return false; @@ -168,11 +175,6 @@ public class PropertyInfo { return false; } else if (!name.equals(other.name)) return false; - if (predicate == null) { - if (other.predicate != null) - return false; - } else if (!predicate.equals(other.predicate)) - return false; if (requiredDatatype == null) { if (other.requiredDatatype != null) return false; diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/StandardGraphPropertyVariable.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/StandardGraphPropertyVariable.java index 7927614cd..b7eaf075e 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/StandardGraphPropertyVariable.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/StandardGraphPropertyVariable.java @@ -35,12 +35,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class StandardGraphPropertyVariable extends AbstractPropertyVariable { + private static final Logger LOGGER = LoggerFactory.getLogger(StandardGraphPropertyVariable.class); - - protected static final PropertyInfo NO_PROPERTY = new PropertyInfo(null, null, - false, false, Collections. emptySet(), null, null, null, null, null, null, - Collections.> emptyMap(), - null, false); + + protected static final PropertyInfo NO_PROPERTY = new PropertyInfo(null, null, true, + false, false, Collections. emptySet(), null, null, null, null, null, null, + Collections.> emptyMap(), + null, false); final public Variable parent; final public Resource parentResource; @@ -170,13 +171,10 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable { if(represents == null) throw new InvalidVariableException("Variable is not represented by any resource (URI=" + getPossibleURI(graph) + ")."); return represents; -// return graph.getSingleObject(parentResource, property.predicate); } @Override public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException { -// if(parentResource == null) return null; -// return graph.getPossibleObject(parentResource, property.predicate); return represents; } @@ -226,16 +224,6 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable { @Override public Datatype getDatatype(ReadGraph graph) throws DatabaseException { -//<<<<<<< .mine -// -// Binding binding = getPossibleDefaultBinding(graph); -// if(binding != null) return binding.type(); -// -// Variant vt = getVariantValue(graph); -// binding = vt.getBinding(); -// if(binding == null) throw new DatabaseException("No binding in Variant with value " + vt.getValue()); -// return binding.type(); -//======= Datatype type; try { type = getValueAccessor(graph).getDatatype(graph, this); @@ -252,7 +240,6 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable { } return type; -//>>>>>>> .r30794 } -- 2.47.1