]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/chart/property/ObtainedDoubleAdapter.java
Contextual adapters must throw instead of returning null
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / chart / property / ObtainedDoubleAdapter.java
index 7ae1cd835f2058cd0bf1f5ca74d04e985145dc9e..ff257169ca01e1272b57e62ff1dd259d82aa6601 100644 (file)
@@ -1,52 +1,53 @@
-/*******************************************************************************\r
- * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
- * 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.modeling.ui.chart.property;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.RelationContext;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Statement;\r
-import org.simantics.db.common.adaption.SimpleContextualAdapter;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.property.OrderedResource;\r
-import org.simantics.operation.Layer0X;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class ObtainedDoubleAdapter extends SimpleContextualAdapter<Double, RelationContext> {\r
-\r
-    @Override\r
-    public Double adapt(ReadGraph graph, Resource source, RelationContext context) throws DatabaseException {\r
-        Layer0X L0X = Layer0X.getInstance(graph);\r
-        ArrayList<OrderedResource> order = new ArrayList<OrderedResource>();\r
-        for (Statement stm : graph.getStatements(context.getSubject(), L0X.ObtainsProperty)) {\r
-            Integer position = graph.getRelatedValue(stm.getPredicate(), L0X.NaturalNumberOrderRelation,\r
-                    Bindings.INTEGER);\r
-            order.add(new OrderedResource(position, stm.getObject()));\r
-        }\r
-        if (order.size() > 1)\r
-            Collections.sort(order);\r
-        for (OrderedResource or : order) {\r
-            Double result = graph.getPossibleRelatedAdapter(or.r, context.getStatement().getPredicate(), Double.class);\r
-            if (result != null)\r
-                return result;\r
-        }\r
-\r
-        return null;\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.modeling.ui.chart.property;
+
+import java.util.ArrayList;
+import java.util.Collections;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.RelationContext;
+import org.simantics.db.Resource;
+import org.simantics.db.Statement;
+import org.simantics.db.common.adaption.SimpleContextualAdapter;
+import org.simantics.db.exception.AdaptionException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.property.OrderedResource;
+import org.simantics.operation.Layer0X;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class ObtainedDoubleAdapter extends SimpleContextualAdapter<Double, RelationContext> {
+
+    @Override
+    public Double adapt(ReadGraph graph, Resource source, RelationContext context) throws DatabaseException {
+        Layer0X L0X = Layer0X.getInstance(graph);
+        ArrayList<OrderedResource> order = new ArrayList<OrderedResource>();
+        for (Statement stm : graph.getStatements(context.getSubject(), L0X.ObtainsProperty)) {
+            Integer position = graph.getRelatedValue(stm.getPredicate(), L0X.NaturalNumberOrderRelation,
+                    Bindings.INTEGER);
+            order.add(new OrderedResource(position, stm.getObject()));
+        }
+        if (order.size() > 1)
+            Collections.sort(order);
+        for (OrderedResource or : order) {
+            Double result = graph.getPossibleRelatedAdapter(or.r, context.getStatement().getPredicate(), Double.class);
+            if (result != null)
+                return result;
+        }
+
+        throw new AdaptionException("No obtained double available");
+    }
+
+}