+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.graph.impl;\r
+\r
+import org.simantics.browsing.ui.common.property.IProperty;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.ResourceArray;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ResourceProperty implements IProperty {\r
+\r
+ protected final Type type;\r
+\r
+ protected final Resource subject;\r
+ protected final Resource predicate;\r
+ protected final Resource value;\r
+ protected final ResourceArray path;\r
+\r
+ public ResourceProperty(Type type, Resource subject, Resource predicate, Resource value, ResourceArray path) {\r
+ this.type = type;\r
+ this.subject = subject;\r
+ this.predicate = predicate;\r
+ this.value = value;\r
+ this.path = path;\r
+ }\r
+\r
+ @Override\r
+ public Type getType() {\r
+ return type;\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public <T> T getData(Class<T> clazz) {\r
+ T t = (T) getAdapter(clazz);\r
+ if (t != null)\r
+ return t;\r
+ throw new ClassCastException("class " + clazz.getCanonicalName() + " not adaptable from " + toString());\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ return getClass().getSimpleName() + "[subject=" + subject + ", predicate=" + predicate + ", value=" + value\r
+ + ", path=" + path + "]";\r
+ }\r
+\r
+ @SuppressWarnings({ "rawtypes", "unchecked" })\r
+ @Override\r
+ public Object getAdapter(Class adapter) {\r
+ return adapt(adapter);\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public <T> T adapt(Class<T> adapter) {\r
+ if (adapter == ResourceArray[].class)\r
+ return (T) new ResourceArray[] { new ResourceArray(subject, predicate, value), path };\r
+ if (adapter == ResourceArray.class)\r
+ return (T) new ResourceArray(subject, predicate, value);\r
+ if (adapter == Resource[].class)\r
+ return (T) new Resource[] { subject, predicate, value };\r
+ if (adapter == Resource.class)\r
+ // Return the object of the specified statement\r
+ return (T) value;\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public int propertyHashCode() {\r
+ final int prime = 31;\r
+ int result = 1;\r
+ result = prime * result + subject.hashCode();\r
+ result = prime * result + predicate.hashCode();\r
+ result = prime * result + path.hashCode();\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public boolean propertyEquals(Object obj) {\r
+ if (this == obj)\r
+ return true;\r
+ if (obj == null)\r
+ return false;\r
+ if (getClass() != obj.getClass())\r
+ return false;\r
+ ResourceProperty other = (ResourceProperty) obj;\r
+ return subject.equals(other.subject) && predicate.equals(other.predicate) && path.equals(other.path);\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ return propertyHashCode() * 31 + value.hashCode();\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (this == obj)\r
+ return true;\r
+ if (!propertyEquals(obj))\r
+ return false;\r
+ ResourceProperty other = (ResourceProperty) obj;\r
+ return value.equals(other.value) && type.equals(other.type);\r
+\r
+ }\r
+\r
+}\r