/
PortSide.java
199 lines (182 loc) · 6.69 KB
/
PortSide.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*******************************************************************************
* Copyright (c) 2009, 2015 Kiel University and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.elk.core.options;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;
import com.google.common.collect.Sets;
/**
* Definition of port sides on a node. Besides defining the actual available port sides, this enumeration
* also defines convenience methods as well as constants with all possible port side combinations. The
* latter are named in a clockwise manner, starting at the northern side.
*
* @author msp
* @author cds
*/
public enum PortSide {
/** the side is undefined. */
UNDEFINED,
/** top side. */
NORTH,
/** right side. */
EAST,
/** bottom side. */
SOUTH,
/** left side. */
WEST;
//////////////////////////////////////////////////////////////////////////////////////////////
// Port Side Combinations
/** Immutable set containing the given port sides. */
public static final Set<PortSide> SIDES_NONE = Collections.unmodifiableSet(
EnumSet.noneOf(PortSide.class));
/** Immutable set containing the given port sides. */
public static final Set<PortSide> SIDES_NORTH = Sets.immutableEnumSet(
PortSide.NORTH);
/** Immutable set containing the given port sides. */
public static final Set<PortSide> SIDES_EAST = Sets.immutableEnumSet(
PortSide.EAST);
/** Immutable set containing the given port sides. */
public static final Set<PortSide> SIDES_SOUTH = Sets.immutableEnumSet(
PortSide.SOUTH);
/** Immutable set containing the given port sides. */
public static final Set<PortSide> SIDES_WEST = Sets.immutableEnumSet(
PortSide.WEST);
/** Immutable set containing the given port sides. */
public static final Set<PortSide> SIDES_NORTH_SOUTH = Sets.immutableEnumSet(
PortSide.NORTH, PortSide.SOUTH);
/** Immutable set containing the given port sides. */
public static final Set<PortSide> SIDES_EAST_WEST = Sets.immutableEnumSet(
PortSide.EAST, PortSide.WEST);
/** Immutable set containing the given port sides. */
public static final Set<PortSide> SIDES_NORTH_WEST = Sets.immutableEnumSet(
PortSide.NORTH, PortSide.WEST);
/** Immutable set containing the given port sides. */
public static final Set<PortSide> SIDES_NORTH_EAST = Sets.immutableEnumSet(
PortSide.NORTH, PortSide.EAST);
/** Immutable set containing the given port sides. */
public static final Set<PortSide> SIDES_SOUTH_WEST = Sets.immutableEnumSet(
PortSide.SOUTH, PortSide.WEST);
/** Immutable set containing the given port sides. */
public static final Set<PortSide> SIDES_EAST_SOUTH = Sets.immutableEnumSet(
PortSide.EAST, PortSide.SOUTH);
/** Immutable set containing the given port sides. */
public static final Set<PortSide> SIDES_NORTH_EAST_WEST = Sets.immutableEnumSet(
PortSide.NORTH, PortSide.EAST, PortSide.WEST);
/** Immutable set containing the given port sides. */
public static final Set<PortSide> SIDES_EAST_SOUTH_WEST = Sets.immutableEnumSet(
PortSide.EAST, PortSide.SOUTH, PortSide.WEST);
/** Immutable set containing the given port sides. */
public static final Set<PortSide> SIDES_NORTH_SOUTH_WEST = Sets.immutableEnumSet(
PortSide.NORTH, PortSide.SOUTH, PortSide.WEST);
/** Immutable set containing the given port sides. */
public static final Set<PortSide> SIDES_NORTH_EAST_SOUTH = Sets.immutableEnumSet(
PortSide.NORTH, PortSide.EAST, PortSide.SOUTH);
/** Immutable set containing the given port sides. */
public static final Set<PortSide> SIDES_NORTH_EAST_SOUTH_WEST = Sets.immutableEnumSet(
PortSide.NORTH, PortSide.EAST, PortSide.SOUTH, PortSide.WEST);
//////////////////////////////////////////////////////////////////////////////////////////////
// Convenience Methods
/**
* Returns the next side in clockwise order.
*
* @return the next side in clockwise order
*/
public PortSide right() {
switch (this) {
case NORTH:
return EAST;
case EAST:
return SOUTH;
case SOUTH:
return WEST;
case WEST:
return NORTH;
default:
return UNDEFINED;
}
}
/**
* Returns the next side in counter-clockwise order.
*
* @return the next side in counter-clockwise order
*/
public PortSide left() {
switch (this) {
case NORTH:
return WEST;
case EAST:
return NORTH;
case SOUTH:
return EAST;
case WEST:
return SOUTH;
default:
return UNDEFINED;
}
}
/**
* Returns the opposed side.
*
* @return the opposed side
*/
public PortSide opposed() {
switch (this) {
case NORTH:
return SOUTH;
case EAST:
return WEST;
case SOUTH:
return NORTH;
case WEST:
return EAST;
default:
return UNDEFINED;
}
}
/**
* Returns whether this and the given port side are adjacent port sides in that they share a corner of their node.
*
* @param other the port side to check with.
* @return {@code true} if the two sides share a corner.
*/
public boolean areAdjacent(final PortSide other) {
if (this == UNDEFINED) {
return false;
} else {
return this.left() == other || this.right() == other;
}
}
/**
* Get the port side that corresponds to the given direction.
*
* @param direction a direction
* @return the corresponding port side
*/
public static PortSide fromDirection(final Direction direction) {
switch (direction) {
case UP:
return NORTH;
case RIGHT:
return EAST;
case DOWN:
return SOUTH;
case LEFT:
return WEST;
default:
return UNDEFINED;
}
}
public static boolean isVertical(final PortSide side) {
return side == NORTH || side == SOUTH;
}
public static boolean isHorizontal(final PortSide side) {
return side == WEST || side == EAST;
}
}