]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/predicates/Type.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.layer0.utils / src / org / simantics / layer0 / utils / predicates / Type.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.layer0.utils.predicates;\r
13 \r
14 import java.util.Collection;\r
15 \r
16 import org.simantics.db.Resource;\r
17 import org.simantics.db.ReadGraph;\r
18 import org.simantics.db.WriteGraph;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.layer0.Layer0;\r
21 \r
22 public class Type extends UnaryPredicate {\r
23 \r
24         Resource type;\r
25 \r
26         public Type(Resource type) {\r
27                 this.type = type;\r
28         }\r
29         \r
30         public Resource getType() {\r
31         return type;\r
32     }\r
33 \r
34         @Override\r
35         public Collection<Resource> getResources(ReadGraph g) {\r
36                 throw new UnsupportedOperationException();\r
37         }\r
38 \r
39         @Override\r
40         public boolean has(ReadGraph g, Resource resource) throws DatabaseException {\r
41                 return g.isInstanceOf(resource, type);\r
42         }\r
43 \r
44         @Override\r
45         public boolean supportsUnboundedQuery() {\r
46                 return false;\r
47         }\r
48 \r
49         @Override\r
50         public void add(WriteGraph g, Resource r) throws DatabaseException {\r
51         Layer0 b = Layer0.getInstance(g);\r
52                 if(!g.isInstanceOf(r, type))\r
53                         g.claim(r, b.InstanceOf, null, type);\r
54         }\r
55 \r
56         @Override\r
57         public void remove(WriteGraph g, Resource r) throws DatabaseException {\r
58                 // FIXME not implemented correctly\r
59         Layer0 b = Layer0.getInstance(g);\r
60                 if(g.isInstanceOf(r, type))\r
61                         g.denyStatement(r, b.InstanceOf, type);\r
62         }\r
63 \r
64         @Override\r
65         public boolean supportsAddition() {\r
66                 return true;\r
67         }\r
68 \r
69         @Override\r
70         public boolean supportsRemoval() {\r
71                 return false;\r
72         }\r
73 \r
74         @Override\r
75         public int hashCode() {\r
76                 final int prime = 31;\r
77                 int result = 1;\r
78                 result = prime * result + ((type == null) ? 0 : type.hashCode());\r
79                 return result;\r
80         }\r
81 \r
82         @Override\r
83         public boolean equals(Object obj) {\r
84                 if (this == obj)\r
85                         return true;\r
86                 if (obj == null)\r
87                         return false;\r
88                 if (getClass() != obj.getClass())\r
89                         return false;\r
90                 Type other = (Type) obj;\r
91                 if (type == null) {\r
92                         if (other.type != null)\r
93                                 return false;\r
94                 } else if (!type.equals(other.type))\r
95                         return false;\r
96                 return true;\r
97         }\r
98         \r
99 }\r