]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/Adapt.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / Adapt.java
1 package org.simantics.db.common.request;\r
2 \r
3 import org.simantics.db.ReadGraph;\r
4 import org.simantics.db.Resource;\r
5 import org.simantics.db.exception.AdaptionException;\r
6 import org.simantics.db.exception.ServiceException;\r
7 import org.simantics.db.exception.ValidationException;\r
8 \r
9 /**\r
10  * @author Tuukka Lehtonen\r
11  * \r
12  * @param <T> the adaption target interface\r
13  */\r
14 public class Adapt<T> extends ResourceRead<T> {\r
15 \r
16     final protected Class<T> target;\r
17     final protected boolean  allowNull;\r
18     final protected boolean  uniqueResult;\r
19 \r
20     public Adapt(Resource resource, Class<T> target) {\r
21         this(resource, target, false, false);\r
22     }\r
23 \r
24     public Adapt(Resource resource, Class<T> target, boolean allowNull) {\r
25         this(resource, target, allowNull, false);\r
26     }\r
27 \r
28     public Adapt(Resource resource, Class<T> target, boolean allowNull, boolean uniqueResult) {\r
29         super(resource);\r
30         assert target != null;\r
31         this.target = target;\r
32         this.allowNull = allowNull;\r
33         this.uniqueResult = uniqueResult;\r
34     }\r
35 \r
36     @Override\r
37     public T perform(ReadGraph graph) throws AdaptionException, ValidationException, ServiceException {\r
38         if (allowNull) {\r
39             if (uniqueResult)\r
40                 return graph.getPossibleUniqueAdapter(resource, target);\r
41             return graph.getPossibleAdapter(resource, target);\r
42         }\r
43         if (uniqueResult)\r
44             return graph.adaptUnique(resource, target);\r
45         return graph.adapt(resource, target);\r
46     }\r
47 \r
48     @Override\r
49     public int hashCode() {\r
50         final int prime = 31;\r
51         int result = super.hashCode();\r
52         result = prime * result + (allowNull ? 1231 : 1237);\r
53         result = prime * result + (target.hashCode());\r
54         result = prime * result + (uniqueResult ? 1303 : 1319);\r
55         return result;\r
56     }\r
57 \r
58     @Override\r
59     public boolean equals(Object obj) {\r
60         if (obj == null)\r
61             return false;\r
62         if (this == obj)\r
63             return true;\r
64         if (getClass() != obj.getClass())\r
65             return false;\r
66         Adapt<?> other = (Adapt<?>) obj;\r
67         if (!resource.equals(other.resource))\r
68             return false;\r
69         if (allowNull != other.allowNull)\r
70             return false;\r
71         if (!target.equals(other.target))\r
72             return false;\r
73         if (uniqueResult != other.uniqueResult)\r
74             return false;\r
75         return true;\r
76     }\r
77 \r
78 }