]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/BaseRequest.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / BaseRequest.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.diagram.adapter;
13
14 import org.simantics.db.request.Read;
15 import org.simantics.g2d.canvas.ICanvasContext;
16
17 /**
18  * A base for synchronous requests performed by
19  * {@link GraphToDiagramSynchronizer}.
20  * 
21  * <p>
22  * Takes care of making this request unique for this
23  * {@link GraphToDiagramSynchronizer} instances by making the active
24  * {@link ICanvasContext} a part of the requests identity.
25  * </p>
26  * 
27  * @param <T> type of stored data element
28  * @param <Result> query result type
29  */
30 abstract class BaseRequest<T, Result> implements Read<Result> {
31
32     final protected T    data;
33
34     final int            hash;
35
36     final ICanvasContext canvas;
37
38     public BaseRequest(ICanvasContext canvas, T data) {
39         this.canvas = canvas;
40         assert canvas != null;
41         assert data != null;
42         this.data = data;
43         this.hash = data.hashCode() + 31 * canvas.hashCode();
44     }
45
46     @Override
47     public int hashCode() {
48         return hash;
49     }
50
51     @Override
52     public boolean equals(Object other) {
53         if (this == other)
54             return true;
55         if (other == null || getClass() != other.getClass())
56             return false;
57         BaseRequest<?, ?> o = (BaseRequest<?, ?>) other;
58         return data.equals(o.data) && canvas.equals(o.canvas);
59     }
60
61     @Override
62     public String toString() {
63         return getClass().getSimpleName() + "[" + data + " - " + canvas.hashCode() + "]";
64     }
65
66 }