]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/StructuralResource.java
Merge "Fixed invalid comparisons which were identified by Eclipse IDE 2018-09"
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / structural / StructuralResource.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
3  * 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  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.objmap.structural;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.layer0.Layer0;
21
22 /**
23  * An object representing structural Resource.
24  * 
25  * 
26  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
27  *
28  */
29 public class StructuralResource  {
30
31         private Resource resource;
32         private List<Resource> context = new ArrayList<Resource>(1);
33         
34         private Resource typeResource = null;
35         
36         public StructuralResource(Resource resource) {
37                 assert(resource != null);
38                 this.resource = resource;
39         }
40         public StructuralResource(ReadGraph g, Resource resource, Resource context) throws DatabaseException {
41                 assert(resource != null);
42                 this.resource = resource;
43                 this.context.add(context);
44                 resolveType(g);
45         }
46         
47         public StructuralResource(ReadGraph g, Resource resource, Resource... context) throws DatabaseException {
48                 assert(resource != null);
49                 this.resource = resource;
50                 for (Resource r : context)
51                         this.context.add(r);
52                 resolveType(g);
53         }
54         
55         public StructuralResource(ReadGraph g, Resource resource, List<Resource> context) throws DatabaseException {
56                 assert(resource != null);
57                 this.resource = resource;
58                 for (Resource r : context)
59                         this.context.add(r);
60                 resolveType(g); 
61         }
62         public StructuralResource(ReadGraph g, Resource resource, List<Resource> context, Resource context2) throws DatabaseException {
63                 assert(resource != null);
64                 this.resource = resource;
65                 for (Resource r : context)
66                         this.context.add(r);
67                 this.context.add(context2);
68                 resolveType(g);
69         }
70         
71         private void resolveType(ReadGraph g) throws DatabaseException {
72                 if (this.context.contains(resource)) {
73                         Layer0 l0 = Layer0.getInstance(g);
74                         typeResource = g.getSingleObject(resource, l0.InstanceOf);
75                 } 
76         }
77         
78         
79         /**
80          * The Resource in the DB.
81          * @return
82          */
83         public Resource getResource() {
84                 return resource;
85         }
86         
87         /**
88          * Context in which this resource is accessed. Each context resource represents a structural model instance. 
89          * @return
90          */
91         public List<Resource> getContext() {
92                 return context;
93         }
94         
95         /**
96          * If the resource is structural model instance, this returns the type Resource. Otherwise returns null.
97          * @return
98          */
99         public Resource getTypeResource() {
100                 return typeResource;
101         }
102         
103         /**
104          * Returns true, if the resource is structural, 
105          * @return
106          */
107         public boolean isStructural() {
108                 return context.size() > 0;
109         }
110         
111         /**
112          * Returns true is the Resource is root of Structural Model instance.
113          * In this case the resource instance is editable.
114          * 
115          * @return
116          */
117         public boolean isStructuralRoot() {
118                 return (context.size() == 1 && context.get(0).equals(resource));
119         }
120         
121         /**
122          * Returns true,  the resource is structural model instance.
123          * @return
124          */
125         public boolean isStructuralInstance() {
126                 return typeResource != null;
127         }
128         
129         @Override
130         public int hashCode() {
131                 int hashCode = resource.hashCode();
132                 for (Resource ctx : context)
133                         hashCode += ctx.hashCode();
134                 return hashCode;
135         }
136         
137         @Override
138         public boolean equals(Object obj) {
139                 if (obj == this)
140                         return true;
141                 if (obj == null)
142                         return false;
143                 if (obj.getClass() != getClass())
144                         return false;
145                 StructuralResource other = (StructuralResource)obj;
146                 if (!resource.equals(other.resource))
147                         return false;
148                 if (context.size() != other.context.size())
149                         return false;
150                 for (int i = 0; i < context.size(); i++) {
151                         if (!context.get(i).equals(other.context.get(i)))
152                                 return false;
153                 }
154                 return true;
155         }
156         
157         @Override
158         public String toString() {
159                 String s = "Res: " + resource + " Context:";
160                 for (Resource ctx : context) 
161                         s+= " "+ ctx;
162                 if (typeResource != null)
163                         s+= " Type: " + typeResource;
164                 return s;
165         }
166
167 }