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