]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/Terminals.java
Make Write-interfaces as @FunctionalInterface for lambdas
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / handler / impl / Terminals.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.element.handler.impl;
13
14 import gnu.trove.map.hash.THashMap;
15
16 import java.awt.Shape;
17 import java.awt.geom.AffineTransform;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Map;
21
22 import org.simantics.g2d.diagram.handler.Topology.Terminal;
23 import org.simantics.g2d.element.IElement;
24 import org.simantics.g2d.element.handler.TerminalLayout;
25 import org.simantics.g2d.element.handler.TerminalTopology;
26 import org.simantics.g2d.utils.geom.DirectionSet;
27
28 /**
29  * @author Tuukka Lehtonen
30  */
31 public class Terminals implements TerminalLayout, TerminalTopology {
32
33     private static final long             serialVersionUID = -6532093690907028016L;
34
35     private final Map<Terminal, ObjectTerminal> terminalMap      = new THashMap<Terminal, ObjectTerminal>();
36     private final ArrayList<Terminal>           terminals        = new ArrayList<Terminal>();
37
38     public Terminals(Collection<ObjectTerminal> ts) {
39         for (ObjectTerminal ti : ts) {
40             terminals.add(ti);
41             terminalMap.put(ti, ti);
42         }
43     }
44
45     @Override
46     public AffineTransform getTerminalPosition(IElement node, Terminal t) {
47         ObjectTerminal ti = terminalMap.get(t);
48         if (ti == null)
49             return null;
50         return new AffineTransform(ti.getTransform());
51     }
52
53     @Override
54     public void getTerminals(IElement e, Collection<Terminal> result) {
55         result.addAll(terminals);
56     }
57
58     @Override
59     public boolean getTerminalDirection(IElement node, Terminal t, DirectionSet directions) {
60         ObjectTerminal ti = terminalMap.get(t);
61         if (ti == null)
62             return false;
63         directions.addAll(ti.getDirections());
64         return true;
65     }
66
67     @Override
68     public Shape getTerminalShape(IElement node, Terminal t) {
69         ObjectTerminal ti = terminalMap.get(t);
70         if (ti == null)
71             return null;
72         return ti.getShape();
73     }
74
75     @Override
76     public int hashCode() {
77         return terminalMap.hashCode();
78     }
79
80     @Override
81     public boolean equals(Object obj) {
82         if (this == obj)
83             return true;
84         if (obj == null)
85             return false;
86         if (getClass() != obj.getClass())
87             return false;
88         Terminals other = (Terminals) obj;
89         return terminalMap.equals(other.terminalMap);
90     }
91
92 }