]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/connection/TerminalKeyOf.java
Two rendering glitch fixes for time series charts
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / connection / TerminalKeyOf.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.g2d.connection;
13
14 import org.simantics.g2d.diagram.handler.Topology.Terminal;
15 import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;
16 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
17
18 /**
19  * This hint key is used for representing a terminal within an element that is
20  * connected to an edge. The key is identified by two things: the connected
21  * terminal and a secondary data object that can be used to make the key unique
22  * for an edge in cases where several connections to the same terminal are
23  * possible. A good option is to use the back-end object of the edge element
24  * itself, which naturally makes the key unique for that particular edge.
25  * 
26  * <p>
27  * This key has a counterpart, {@link EndKeyOf} that is used in edge elements to
28  * connect both the {@link EdgeEnd#Begin} and {@link EdgeEnd#End} ends of the
29  * edge to element terminals. To actually represent the connection, you should
30  * give the same value object for this hint pair.
31  * </p>
32  * 
33  * @author Tuukka Lehtonen
34  */
35 public class TerminalKeyOf extends KeyOf {
36
37     private final Terminal terminal;
38     private final Object   data;
39     private int            hash;
40
41     /**
42      * @param t the terminal this connector hint represents
43      * @param data a secondary data object for making the key unique for a
44      *        single edge connected to the specified terminal. Use a
45      *        <code>null</code> value only if you consider the terminal to allow
46      *        only one connected edge.
47      * @param clazz the allowed class of the value object.
48      */
49     public TerminalKeyOf(Terminal t, Object data, Class<?> clazz) {
50         super(clazz);
51         this.terminal = t;
52         this.data = data;
53         this.hash = t.hashCode() ^ clazz.hashCode();
54         if (data != null)
55             this.hash = this.hash * 31 + data.hashCode();
56     }
57
58     public Terminal getTerminal() {
59         return terminal;
60     }
61
62     public Object getData() {
63         return data;
64     }
65
66     @Override
67     public int hashCode() {
68         return hash;
69     }
70
71     @Override
72     public boolean equals(Object obj) {
73         if (this == obj)
74             return true;
75         if (!(obj instanceof TerminalKeyOf))
76             return false;
77         TerminalKeyOf other = (TerminalKeyOf) obj;
78         if (!terminal.equals(other.terminal))
79             return false;
80         return (data == null) ? other.data == null : data.equals(other.data);
81     }
82
83     @Override
84     public String toString() {
85         return getClass().getSimpleName() + "[terminal=" + terminal + ", data=" + data + "]";
86     }
87
88 }