2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.client
;
20 import org
.apache
.hbase
.thirdparty
.com
.google
.protobuf
.InvalidProtocolBufferException
;
21 import org
.apache
.hadoop
.hbase
.TableName
;
22 import org
.apache
.yetus
.audience
.InterfaceAudience
;
23 import org
.apache
.yetus
.audience
.InterfaceStability
;
24 import org
.apache
.hadoop
.hbase
.exceptions
.DeserializationException
;
25 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.HBaseProtos
;
28 * Represents table state.
30 @InterfaceAudience.Private
31 public class TableState
{
33 @InterfaceAudience.Private
34 @InterfaceStability.Evolving
35 public static enum State
{
42 * Covert from PB version of State
44 * @param state convert from
47 public static State
convert(HBaseProtos
.TableState
.State state
) {
57 ret
= State
.DISABLING
;
63 throw new IllegalStateException(state
.toString());
69 * Covert to PB version of State
73 public HBaseProtos
.TableState
.State
convert() {
74 HBaseProtos
.TableState
.State state
;
77 state
= HBaseProtos
.TableState
.State
.ENABLED
;
80 state
= HBaseProtos
.TableState
.State
.DISABLED
;
83 state
= HBaseProtos
.TableState
.State
.DISABLING
;
86 state
= HBaseProtos
.TableState
.State
.ENABLING
;
89 throw new IllegalStateException(this.toString());
96 private final TableName tableName
;
97 private final State state
;
100 * @return True if table is {@link State#ENABLED}.
102 public boolean isEnabled() {
103 return isInStates(State
.ENABLED
);
107 * @return True if table is {@link State#ENABLING}.
109 public boolean isEnabling() {
110 return isInStates(State
.ENABLING
);
114 * @return True if {@link State#ENABLED} or {@link State#ENABLING}
116 public boolean isEnabledOrEnabling() {
117 return isInStates(State
.ENABLED
, State
.ENABLING
);
121 * @return True if table is disabled.
123 public boolean isDisabled() {
124 return isInStates(State
.DISABLED
);
128 * @return True if table is disabling.
130 public boolean isDisabling() {
131 return isInStates(State
.DISABLING
);
135 * @return True if {@link State#DISABLED} or {@link State#DISABLED}
137 public boolean isDisabledOrDisabling() {
138 return isInStates(State
.DISABLED
, State
.DISABLING
);
142 * Create instance of TableState.
143 * @param tableName name of the table
144 * @param state table state
146 public TableState(TableName tableName
, State state
) {
147 this.tableName
= tableName
;
152 * @return table state
154 public State
getState() {
159 * Table name for state
161 * @return milliseconds
163 public TableName
getTableName() {
168 * Check that table in given states
170 * @return true if satisfies
172 public boolean inStates(State state
) {
173 return this.state
.equals(state
);
177 * Check that table in given states
178 * @param states state list
179 * @return true if satisfies
181 public boolean inStates(State
... states
) {
182 for (State s
: states
) {
183 if (s
.equals(this.state
))
191 * Covert to PB version of TableState
194 public HBaseProtos
.TableState
convert() {
195 return HBaseProtos
.TableState
.newBuilder()
196 .setState(this.state
.convert()).build();
200 * Covert from PB version of TableState
202 * @param tableName table this state of
203 * @param tableState convert from
206 public static TableState
convert(TableName tableName
, HBaseProtos
.TableState tableState
) {
207 TableState
.State state
= State
.convert(tableState
.getState());
208 return new TableState(tableName
, state
);
211 public static TableState
parseFrom(TableName tableName
, byte[] bytes
)
212 throws DeserializationException
{
214 return convert(tableName
, HBaseProtos
.TableState
.parseFrom(bytes
));
215 } catch (InvalidProtocolBufferException e
) {
216 throw new DeserializationException(e
);
221 * Static version of state checker
222 * @param target equals to any of
223 * @return true if satisfies
225 public boolean isInStates(State
... target
) {
226 for (State tableState
: target
) {
227 if (this.state
.equals(tableState
)) {
235 public boolean equals(Object o
) {
236 if (this == o
) return true;
237 if (o
== null || getClass() != o
.getClass()) return false;
239 TableState that
= (TableState
) o
;
241 if (state
!= that
.state
) return false;
242 if (tableName
!= null ?
!tableName
.equals(that
.tableName
) : that
.tableName
!= null)
249 public int hashCode() {
250 int result
= (tableName
!= null ? tableName
.hashCode() : 0);
251 result
= 31 * result
+ (state
!= null ? state
.hashCode() : 0);
256 public String
toString() {
257 return "tableName=" + tableName
+ ", state=" + state
;