]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/connection/TerminalKeyOf.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / connection / TerminalKeyOf.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.connection;\r
13 \r
14 import org.simantics.g2d.diagram.handler.Topology.Terminal;\r
15 import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;\r
16 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
17 \r
18 /**\r
19  * This hint key is used for representing a terminal within an element that is\r
20  * connected to an edge. The key is identified by two things: the connected\r
21  * terminal and a secondary data object that can be used to make the key unique\r
22  * for an edge in cases where several connections to the same terminal are\r
23  * possible. A good option is to use the back-end object of the edge element\r
24  * itself, which naturally makes the key unique for that particular edge.\r
25  * \r
26  * <p>\r
27  * This key has a counterpart, {@link EndKeyOf} that is used in edge elements to\r
28  * connect both the {@link EdgeEnd#Begin} and {@link EdgeEnd#End} ends of the\r
29  * edge to element terminals. To actually represent the connection, you should\r
30  * give the same value object for this hint pair.\r
31  * </p>\r
32  * \r
33  * @author Tuukka Lehtonen\r
34  */\r
35 public class TerminalKeyOf extends KeyOf {\r
36 \r
37     private final Terminal terminal;\r
38     private final Object   data;\r
39     private int            hash;\r
40 \r
41     /**\r
42      * @param t the terminal this connector hint represents\r
43      * @param data a secondary data object for making the key unique for a\r
44      *        single edge connected to the specified terminal. Use a\r
45      *        <code>null</code> value only if you consider the terminal to allow\r
46      *        only one connected edge.\r
47      * @param clazz the allowed class of the value object.\r
48      */\r
49     public TerminalKeyOf(Terminal t, Object data, Class<?> clazz) {\r
50         super(clazz);\r
51         this.terminal = t;\r
52         this.data = data;\r
53         this.hash = t.hashCode() ^ clazz.hashCode();\r
54         if (data != null)\r
55             this.hash = this.hash * 31 + data.hashCode();\r
56     }\r
57 \r
58     public Terminal getTerminal() {\r
59         return terminal;\r
60     }\r
61 \r
62     public Object getData() {\r
63         return data;\r
64     }\r
65 \r
66     @Override\r
67     public int hashCode() {\r
68         return hash;\r
69     }\r
70 \r
71     @Override\r
72     public boolean equals(Object obj) {\r
73         if (this == obj)\r
74             return true;\r
75         if (!(obj instanceof TerminalKeyOf))\r
76             return false;\r
77         TerminalKeyOf other = (TerminalKeyOf) obj;\r
78         if (!terminal.equals(other.terminal))\r
79             return false;\r
80         return (data == null) ? other.data == null : data.equals(other.data);\r
81     }\r
82 \r
83     @Override\r
84     public String toString() {\r
85         return getClass().getSimpleName() + "[terminal=" + terminal + ", data=" + data + "]";\r
86     }\r
87 \r
88 }