1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.client;
19
20 import com.google.protobuf.InvalidProtocolBufferException;
21 import org.apache.hadoop.hbase.TableName;
22 import org.apache.hadoop.hbase.classification.InterfaceAudience;
23 import org.apache.hadoop.hbase.classification.InterfaceStability;
24 import org.apache.hadoop.hbase.exceptions.DeserializationException;
25 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
26
27
28
29
30 @InterfaceAudience.Private
31 public class TableState {
32
33 @InterfaceAudience.Public
34 @InterfaceStability.Evolving
35 public static enum State {
36 ENABLED,
37 DISABLED,
38 DISABLING,
39 ENABLING;
40
41
42
43
44
45
46
47 public static State convert(HBaseProtos.TableState.State state) {
48 State ret;
49 switch (state) {
50 case ENABLED:
51 ret = State.ENABLED;
52 break;
53 case DISABLED:
54 ret = State.DISABLED;
55 break;
56 case DISABLING:
57 ret = State.DISABLING;
58 break;
59 case ENABLING:
60 ret = State.ENABLING;
61 break;
62 default:
63 throw new IllegalStateException(state.toString());
64 }
65 return ret;
66 }
67
68
69
70
71
72
73 public HBaseProtos.TableState.State convert() {
74 HBaseProtos.TableState.State state;
75 switch (this) {
76 case ENABLED:
77 state = HBaseProtos.TableState.State.ENABLED;
78 break;
79 case DISABLED:
80 state = HBaseProtos.TableState.State.DISABLED;
81 break;
82 case DISABLING:
83 state = HBaseProtos.TableState.State.DISABLING;
84 break;
85 case ENABLING:
86 state = HBaseProtos.TableState.State.ENABLING;
87 break;
88 default:
89 throw new IllegalStateException(this.toString());
90 }
91 return state;
92 }
93
94 }
95
96 private final TableName tableName;
97 private final State state;
98
99
100
101
102
103
104 public TableState(TableName tableName, State state) {
105 this.tableName = tableName;
106 this.state = state;
107 }
108
109
110
111
112 public State getState() {
113 return state;
114 }
115
116
117
118
119
120
121 public TableName getTableName() {
122 return tableName;
123 }
124
125
126
127
128
129
130 public boolean inStates(State state) {
131 return this.state.equals(state);
132 }
133
134
135
136
137
138
139 public boolean inStates(State... states) {
140 for (State s : states) {
141 if (s.equals(this.state))
142 return true;
143 }
144 return false;
145 }
146
147
148
149
150
151
152 public HBaseProtos.TableState convert() {
153 return HBaseProtos.TableState.newBuilder()
154 .setState(this.state.convert()).build();
155 }
156
157
158
159
160
161
162
163
164 public static TableState convert(TableName tableName, HBaseProtos.TableState tableState) {
165 TableState.State state = State.convert(tableState.getState());
166 return new TableState(tableName, state);
167 }
168
169 public static TableState parseFrom(TableName tableName, byte[] bytes)
170 throws DeserializationException {
171 try {
172 return convert(tableName, HBaseProtos.TableState.parseFrom(bytes));
173 } catch (InvalidProtocolBufferException e) {
174 throw new DeserializationException(e);
175 }
176 }
177
178
179
180
181
182
183
184 public static boolean isInStates(State state, State... target) {
185 for (State tableState : target) {
186 if (state.equals(tableState))
187 return true;
188 }
189 return false;
190 }
191
192 @Override
193 public boolean equals(Object o) {
194 if (this == o) return true;
195 if (o == null || getClass() != o.getClass()) return false;
196
197 TableState that = (TableState) o;
198
199 if (state != that.state) return false;
200 if (tableName != null ? !tableName.equals(that.tableName) : that.tableName != null)
201 return false;
202
203 return true;
204 }
205
206 @Override
207 public int hashCode() {
208 int result = (tableName != null ? tableName.hashCode() : 0);
209 result = 31 * result + (state != null ? state.hashCode() : 0);
210 return result;
211 }
212
213 @Override
214 public String toString() {
215 return "TableState{" +
216 ", tableName=" + tableName +
217 ", state=" + state +
218 '}';
219 }
220 }