]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/ResourceProperty.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / ResourceProperty.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.browsing.ui.graph.impl;\r
13 \r
14 import org.simantics.browsing.ui.common.property.IProperty;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.common.ResourceArray;\r
17 \r
18 /**\r
19  * @author Tuukka Lehtonen\r
20  */\r
21 public class ResourceProperty implements IProperty {\r
22 \r
23     protected final Type          type;\r
24 \r
25     protected final Resource      subject;\r
26     protected final Resource      predicate;\r
27     protected final Resource      value;\r
28     protected final ResourceArray path;\r
29 \r
30     public ResourceProperty(Type type, Resource subject, Resource predicate, Resource value, ResourceArray path) {\r
31         this.type = type;\r
32         this.subject = subject;\r
33         this.predicate = predicate;\r
34         this.value = value;\r
35         this.path = path;\r
36     }\r
37 \r
38     @Override\r
39     public Type getType() {\r
40         return type;\r
41     }\r
42 \r
43     @SuppressWarnings("unchecked")\r
44     @Override\r
45     public <T> T getData(Class<T> clazz) {\r
46         T t = (T) getAdapter(clazz);\r
47         if (t != null)\r
48             return t;\r
49         throw new ClassCastException("class " + clazz.getCanonicalName() + " not adaptable from " + toString());\r
50     }\r
51 \r
52     @Override\r
53     public String toString() {\r
54         return getClass().getSimpleName() + "[subject=" + subject + ", predicate=" + predicate + ", value=" + value\r
55         + ", path=" + path + "]";\r
56     }\r
57 \r
58     @SuppressWarnings({ "rawtypes", "unchecked" })\r
59     @Override\r
60     public Object getAdapter(Class adapter) {\r
61         return adapt(adapter);\r
62     }\r
63 \r
64     @SuppressWarnings("unchecked")\r
65     @Override\r
66     public <T> T adapt(Class<T> adapter) {\r
67         if (adapter == ResourceArray[].class)\r
68             return (T) new ResourceArray[] { new ResourceArray(subject, predicate, value), path };\r
69         if (adapter == ResourceArray.class)\r
70             return (T) new ResourceArray(subject, predicate, value);\r
71         if (adapter == Resource[].class)\r
72             return (T) new Resource[] { subject, predicate, value };\r
73         if (adapter == Resource.class)\r
74             // Return the object of the specified statement\r
75             return (T) value;\r
76         return null;\r
77     }\r
78 \r
79     @Override\r
80     public int propertyHashCode() {\r
81         final int prime = 31;\r
82         int result = 1;\r
83         result = prime * result + subject.hashCode();\r
84         result = prime * result + predicate.hashCode();\r
85         result = prime * result + path.hashCode();\r
86         return result;\r
87     }\r
88 \r
89     @Override\r
90     public boolean propertyEquals(Object obj) {\r
91         if (this == obj)\r
92             return true;\r
93         if (obj == null)\r
94             return false;\r
95         if (getClass() != obj.getClass())\r
96             return false;\r
97         ResourceProperty other = (ResourceProperty) obj;\r
98         return subject.equals(other.subject) && predicate.equals(other.predicate) && path.equals(other.path);\r
99     }\r
100 \r
101     @Override\r
102     public int hashCode() {\r
103         return propertyHashCode() * 31 + value.hashCode();\r
104     }\r
105 \r
106     @Override\r
107     public boolean equals(Object obj) {\r
108         if (this == obj)\r
109             return true;\r
110         if (!propertyEquals(obj))\r
111             return false;\r
112         ResourceProperty other = (ResourceProperty) obj;\r
113         return value.equals(other.value) && type.equals(other.type);\r
114 \r
115     }\r
116 \r
117 }\r