]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PropertyInfo.java
Implement equals/hashCode for PropertyInfo
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / PropertyInfo.java
1 /*******************************************************************************
2  * Copyright (c) 2019 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.request;
13
14 import java.util.Collection;
15 import java.util.Map;
16 import java.util.Set;
17
18 import org.simantics.databoard.Bindings;
19 import org.simantics.databoard.accessor.reference.ChildReference;
20 import org.simantics.databoard.binding.Binding;
21 import org.simantics.databoard.type.Datatype;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.variable.ValueAccessor;
26 import org.simantics.db.layer0.variable.VariableBuilder;
27 import org.simantics.layer0.Layer0;
28 import org.simantics.utils.datastructures.Pair;
29
30 public class PropertyInfo {
31     public final Resource predicate;
32     public final String name;
33     public final boolean isImmutable;
34     public final boolean isHasProperty;
35     public final boolean isFunctional;
36     public final Set<String> classifications;
37     public final VariableBuilder builder;
38     public final Resource literalRange;
39     public final Datatype requiredDatatype;
40     public final String requiredValueType;
41     public final String definedUnit;
42     public final Binding defaultBinding;
43     public final Map<String,Pair<Resource, ChildReference>> subliteralPredicates;
44     public final ValueAccessor valueAccessor;
45     public final boolean hasEnumerationRange;
46     public PropertyInfo(Resource predicate, String name, boolean isImmutable, boolean isFunctional, boolean isHasProperty, Set<String> classifications, VariableBuilder builder, Resource literalRange, Datatype requiredDatatype, String definedUnit, String requiredValueType, Binding defaultBinding, Map<String,Pair<Resource, ChildReference>> subliteralPredicates, ValueAccessor valueAccessor, boolean hasEnumerationRange) {
47         this.predicate = predicate;
48         this.name = name;
49         this.isImmutable = isImmutable;
50         this.isFunctional = isFunctional;
51         this.isHasProperty = isHasProperty;
52         this.classifications = classifications;
53         this.builder = builder;
54         this.literalRange = literalRange;
55         this.requiredDatatype = requiredDatatype;
56         this.definedUnit = definedUnit;
57         this.requiredValueType = requiredValueType;
58         this.defaultBinding = defaultBinding;
59         this.subliteralPredicates = subliteralPredicates;
60         this.valueAccessor = valueAccessor;
61         this.hasEnumerationRange = hasEnumerationRange;
62     }
63     public static PropertyInfo make(ReadGraph graph, Resource predicate, String name, boolean isFunctional, boolean isHasProperty, Set<String> classifications, VariableBuilder builder, Resource literalRange, Datatype requiredDatatype, String definedUnit, String requiredValueType, Map<String,Pair<Resource, ChildReference>> subliteralPredicates, ValueAccessor valueAccessor, boolean hasEnumerationRange) throws DatabaseException {
64         Layer0 L0 = Layer0.getInstance(graph);
65         if(literalRange != null) {
66             Collection<Resource> dts = graph.getAssertedObjects(literalRange, L0.HasDataType);
67             if(dts.size() == 1) {
68                 Datatype dt = graph.getPossibleValue(dts.iterator().next(), Bindings.DATATYPE);
69                 if(requiredDatatype == null) requiredDatatype = dt;
70             }
71         }
72         Binding defaultBinding = requiredDatatype != null ? Bindings.getBinding(requiredDatatype) : null;
73         return new PropertyInfo(predicate, name, graph.isImmutable(predicate), isFunctional, isHasProperty, classifications, builder, literalRange, requiredDatatype, definedUnit, requiredValueType, defaultBinding, subliteralPredicates, valueAccessor, hasEnumerationRange);
74     }
75     public boolean hasClassification(String classification) {
76         return classifications.contains(classification);
77     }
78     @Override
79     public String toString() {
80         StringBuilder sb = new StringBuilder();
81         sb.append("PropertyInfo [")
82         .append(name)
83         .append(" : ")
84         .append(requiredDatatype)
85         .append(" :: ")
86         .append(requiredValueType)
87         .append(", predicate=")
88         .append(predicate)
89         .append(", isFunctional=")
90         .append(isFunctional)
91         .append(", isHasProperty=")
92         .append(isHasProperty)
93         .append(", hasEnumerationRange=")
94         .append(hasEnumerationRange)
95         .append(", definedUnit=")
96         .append(definedUnit != null ? definedUnit : "<none>")
97         .append(", defaultBinding=")
98         .append(defaultBinding)
99         .append(", valueAccessor=")
100         .append(valueAccessor)
101         .append("]");
102         return sb.toString();
103     }
104     @Override
105     public int hashCode() {
106         final int prime = 31;
107         int result = 1;
108         result = prime * result + ((predicate == null) ? 0 : predicate.hashCode());
109         if (!isImmutable) {
110             result = prime * result + ((builder == null) ? 0 : builder.hashCode());
111             result = prime * result + ((classifications == null) ? 0 : classifications.hashCode());
112             result = prime * result + ((defaultBinding == null) ? 0 : defaultBinding.hashCode());
113             result = prime * result + ((definedUnit == null) ? 0 : definedUnit.hashCode());
114             result = prime * result + (hasEnumerationRange ? 1231 : 1237);
115             result = prime * result + (isFunctional ? 1231 : 1237);
116             result = prime * result + (isHasProperty ? 1231 : 1237);
117             result = prime * result + ((literalRange == null) ? 0 : literalRange.hashCode());
118             result = prime * result + ((name == null) ? 0 : name.hashCode());
119             result = prime * result + ((requiredDatatype == null) ? 0 : requiredDatatype.hashCode());
120             result = prime * result + ((requiredValueType == null) ? 0 : requiredValueType.hashCode());
121             result = prime * result + ((subliteralPredicates == null) ? 0 : subliteralPredicates.hashCode());
122             result = prime * result + ((valueAccessor == null) ? 0 : valueAccessor.hashCode());
123         }
124         return result;
125     }
126     @Override
127     public boolean equals(Object obj) {
128         if (this == obj)
129             return true;
130         if (obj == null)
131             return false;
132         if (getClass() != obj.getClass())
133             return false;
134         PropertyInfo other = (PropertyInfo) obj;
135         if (predicate == null) {
136             if (other.predicate != null)
137                 return false;
138         } else if (!predicate.equals(other.predicate))
139             return false;
140         if(isImmutable)
141             return true;
142         if (builder == null) {
143             if (other.builder != null)
144                 return false;
145         } else if (!builder.equals(other.builder))
146             return false;
147         if (classifications == null) {
148             if (other.classifications != null)
149                 return false;
150         } else if (!classifications.equals(other.classifications))
151             return false;
152         if (defaultBinding == null) {
153             if (other.defaultBinding != null)
154                 return false;
155         } else if (!defaultBinding.equals(other.defaultBinding))
156             return false;
157         if (definedUnit == null) {
158             if (other.definedUnit != null)
159                 return false;
160         } else if (!definedUnit.equals(other.definedUnit))
161             return false;
162         if (hasEnumerationRange != other.hasEnumerationRange)
163             return false;
164         if (isFunctional != other.isFunctional)
165             return false;
166         if (isHasProperty != other.isHasProperty)
167             return false;
168         if (literalRange == null) {
169             if (other.literalRange != null)
170                 return false;
171         } else if (!literalRange.equals(other.literalRange))
172             return false;
173         if (name == null) {
174             if (other.name != null)
175                 return false;
176         } else if (!name.equals(other.name))
177             return false;
178         if (requiredDatatype == null) {
179             if (other.requiredDatatype != null)
180                 return false;
181         } else if (!requiredDatatype.equals(other.requiredDatatype))
182             return false;
183         if (requiredValueType == null) {
184             if (other.requiredValueType != null)
185                 return false;
186         } else if (!requiredValueType.equals(other.requiredValueType))
187             return false;
188         if (subliteralPredicates == null) {
189             if (other.subliteralPredicates != null)
190                 return false;
191         } else if (!subliteralPredicates.equals(other.subliteralPredicates))
192             return false;
193         if (valueAccessor == null) {
194             if (other.valueAccessor != null)
195                 return false;
196         } else if (!valueAccessor.equals(other.valueAccessor))
197             return false;
198         return true;
199     }
200 }