HBASE-18420 Some methods of Admin don't use ColumnFamilyDescriptor
[hbase.git] / hbase-client / src / main / java / org / apache / hadoop / hbase / client / TableState.java
blob71875a53798413cfc33ab7114969b49f04e42922
1 /**
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.hadoop.hbase.shaded.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.shaded.protobuf.generated.HBaseProtos;
27 /**
28 * Represents table state.
30 @InterfaceAudience.Private
31 public class TableState {
33 @InterfaceAudience.Private
34 @InterfaceStability.Evolving
35 public static enum State {
36 ENABLED,
37 DISABLED,
38 DISABLING,
39 ENABLING;
41 /**
42 * Covert from PB version of State
44 * @param state convert from
45 * @return POJO
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());
65 return ret;
68 /**
69 * Covert to PB version of State
71 * @return PB
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());
91 return state;
96 private final TableName tableName;
97 private final State state;
99 /**
100 * Create instance of TableState.
101 * @param tableName name of the table
102 * @param state table state
104 public TableState(TableName tableName, State state) {
105 this.tableName = tableName;
106 this.state = state;
110 * @return table state
112 public State getState() {
113 return state;
117 * Table name for state
119 * @return milliseconds
121 public TableName getTableName() {
122 return tableName;
126 * Check that table in given states
127 * @param state state
128 * @return true if satisfies
130 public boolean inStates(State state) {
131 return this.state.equals(state);
135 * Check that table in given states
136 * @param states state list
137 * @return true if satisfies
139 public boolean inStates(State... states) {
140 for (State s : states) {
141 if (s.equals(this.state))
142 return true;
144 return false;
149 * Covert to PB version of TableState
150 * @return PB
152 public HBaseProtos.TableState convert() {
153 return HBaseProtos.TableState.newBuilder()
154 .setState(this.state.convert()).build();
158 * Covert from PB version of TableState
160 * @param tableName table this state of
161 * @param tableState convert from
162 * @return POJO
164 public static TableState convert(TableName tableName, HBaseProtos.TableState tableState) {
165 TableState.State state = State.convert(tableState.getState());
166 return new TableState(tableName, state);
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);
179 * Static version of state checker
180 * @param state desired
181 * @param target equals to any of
182 * @return true if satisfies
184 public static boolean isInStates(State state, State... target) {
185 for (State tableState : target) {
186 if (state.equals(tableState))
187 return true;
189 return false;
192 @Override
193 public boolean equals(Object o) {
194 if (this == o) return true;
195 if (o == null || getClass() != o.getClass()) return false;
197 TableState that = (TableState) o;
199 if (state != that.state) return false;
200 if (tableName != null ? !tableName.equals(that.tableName) : that.tableName != null)
201 return false;
203 return true;
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;
213 @Override
214 public String toString() {
215 return "TableState{" +
216 ", tableName=" + tableName +
217 ", state=" + state +
218 '}';