1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.constraintlayout.core.scout;
18 
19 /**
20  * Possible directions for a connection
21  */
22 public enum Direction {
23     NORTH(0),
24     SOUTH(1),
25     WEST(2),
26     EAST(3),
27     BASE(4);
28     private final int mDirection;
29 
30     static final int ORIENTATION_VERTICAL = 0;
31     static final int ORIENTATION_HORIZONTAL = 1;
32 
33     private static Direction[] sAllDirections = Direction.values();
34     private static Direction[] sVertical = {NORTH, SOUTH, BASE};
35     private static Direction[] sHorizontal = {WEST, EAST};
36 
Direction(int n)37     Direction(int n) {
38         mDirection = n;
39     }
40 
41     /**
42      * Get an array of all directions
43      *
44      * @return array of all directions
45      */
getAllDirections()46     static Direction[] getAllDirections() {
47         return sAllDirections;
48     }
49 
50     /**
51      * get a String representing the direction integer
52      *
53      * @param directionInteger direction as an integer
54      * @return single letter string to describe the direction
55      */
toString(int directionInteger)56     static String toString(int directionInteger) {
57         return Direction.get(directionInteger).toString();
58     }
59 
60     @Override
toString()61     public String toString() {
62         switch (this) {
63             case NORTH:
64                 return "N";
65             case SOUTH:
66                 return "S";
67             case EAST:
68                 return "E";
69             case WEST:
70                 return "W";
71             case BASE:
72                 return "B";
73         }
74         return "?";
75     }
76 
77     /**
78      * get the direction as an integer
79      *
80      * @return direction as an integer
81      */
getDirection()82     int getDirection() {
83         return mDirection;
84     }
85 
86     /**
87      * gets the opposite direction
88      *
89      * @return the opposite direction
90      */
getOpposite()91     Direction getOpposite() {
92         switch (this) {
93             case NORTH:
94                 return SOUTH;
95             case SOUTH:
96                 return NORTH;
97             case EAST:
98                 return WEST;
99             case WEST:
100                 return EAST;
101             case BASE:
102                 return BASE;
103             default:
104                 return BASE;
105         }
106     }
107 
108     /**
109      * convert from an ordinal of direction to actual direction
110      *
111      * @return Enum member equivalent to integer
112      */
get(int directionInteger)113     static Direction get(int directionInteger) {
114         return sAllDirections[directionInteger];
115     }
116 
117     /**
118      * Directions can be a positive or negative (right and down) being positive
119      * reverse indicates the direction is negative
120      *
121      * @return true for north and east
122      */
reverse()123     boolean reverse() {
124         return (this == NORTH || this == WEST);
125     }
126 
127     /**
128      * gets the viable directions for horizontal or vertical
129      *
130      * @param orientation 0 = vertical 1 = horizontal
131      * @return array of directions for vertical or horizontal
132      */
getDirections(int orientation)133     static Direction[] getDirections(int orientation) {
134         if (orientation == ORIENTATION_VERTICAL) {
135             return sVertical;
136         }
137         return sHorizontal;
138     }
139 
140     /**
141      * Return the number of connection types support by this direction
142      *
143      * @return number of types allowed for this connection
144      */
connectTypes()145     public int connectTypes() {
146         switch (this) {
147             case NORTH:
148             case SOUTH:
149                 return 2;
150             case EAST:
151             case WEST:
152                 return 2;
153             case BASE:
154                 return 1;
155         }
156         return 1;
157     }
158 }
159