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