001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
021import org.apache.hadoop.hbase.TableName;
022import org.apache.yetus.audience.InterfaceAudience;
023import org.apache.yetus.audience.InterfaceStability;
024import org.apache.hadoop.hbase.exceptions.DeserializationException;
025import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
026
027/**
028 * Represents table state.
029 */
030@InterfaceAudience.Private
031public class TableState {
032
033  @InterfaceAudience.Private
034  @InterfaceStability.Evolving
035  public static enum State {
036    ENABLED,
037    DISABLED,
038    DISABLING,
039    ENABLING;
040
041    /**
042     * Covert from PB version of State
043     *
044     * @param state convert from
045     * @return POJO
046     */
047    public static State convert(HBaseProtos.TableState.State state) {
048      State ret;
049      switch (state) {
050      case ENABLED:
051        ret = State.ENABLED;
052        break;
053      case DISABLED:
054        ret = State.DISABLED;
055        break;
056      case DISABLING:
057        ret = State.DISABLING;
058        break;
059      case ENABLING:
060        ret = State.ENABLING;
061        break;
062      default:
063        throw new IllegalStateException(state.toString());
064      }
065      return ret;
066    }
067
068    /**
069     * Covert to PB version of State
070     *
071     * @return PB
072     */
073    public HBaseProtos.TableState.State convert() {
074      HBaseProtos.TableState.State state;
075      switch (this) {
076      case ENABLED:
077        state = HBaseProtos.TableState.State.ENABLED;
078        break;
079      case DISABLED:
080        state = HBaseProtos.TableState.State.DISABLED;
081        break;
082      case DISABLING:
083        state = HBaseProtos.TableState.State.DISABLING;
084        break;
085      case ENABLING:
086        state = HBaseProtos.TableState.State.ENABLING;
087        break;
088      default:
089        throw new IllegalStateException(this.toString());
090      }
091      return state;
092    }
093
094  }
095
096  private final TableName tableName;
097  private final State state;
098
099  /**
100   * @return True if table is {@link State#ENABLED}.
101   */
102  public boolean isEnabled() {
103    return isInStates(State.ENABLED);
104  }
105
106  /**
107   * @return True if table is {@link State#ENABLING}.
108   */
109  public boolean isEnabling() {
110    return isInStates(State.ENABLING);
111  }
112
113  /**
114   * @return True if {@link State#ENABLED} or {@link State#ENABLING}
115   */
116  public boolean isEnabledOrEnabling() {
117    return isInStates(State.ENABLED, State.ENABLING);
118  }
119
120  /**
121   * @return True if table is disabled.
122   */
123  public boolean isDisabled() {
124    return isInStates(State.DISABLED);
125  }
126
127  /**
128   * @return True if table is disabling.
129   */
130  public boolean isDisabling() {
131    return isInStates(State.DISABLING);
132  }
133
134  /**
135   * @return True if {@link State#DISABLED} or {@link State#DISABLED}
136   */
137  public boolean isDisabledOrDisabling() {
138    return isInStates(State.DISABLED, State.DISABLING);
139  }
140
141  /**
142   * Create instance of TableState.
143   * @param tableName name of the table
144   * @param state table state
145   */
146  public TableState(TableName tableName, State state) {
147    this.tableName = tableName;
148    this.state = state;
149  }
150
151  /**
152   * @return table state
153   */
154  public State getState() {
155    return state;
156  }
157
158  /**
159   * Table name for state
160   *
161   * @return milliseconds
162   */
163  public TableName getTableName() {
164    return tableName;
165  }
166
167  /**
168   * Check that table in given states
169   * @param state state
170   * @return true if satisfies
171   */
172  public boolean inStates(State state) {
173    return this.state.equals(state);
174  }
175
176  /**
177   * Check that table in given states
178   * @param states state list
179   * @return true if satisfies
180   */
181  public boolean inStates(State... states) {
182    for (State s : states) {
183      if (s.equals(this.state))
184        return true;
185    }
186    return false;
187  }
188
189
190  /**
191   * Covert to PB version of TableState
192   * @return PB
193   */
194  public HBaseProtos.TableState convert() {
195    return HBaseProtos.TableState.newBuilder()
196        .setState(this.state.convert()).build();
197  }
198
199  /**
200   * Covert from PB version of TableState
201   *
202   * @param tableName table this state of
203   * @param tableState convert from
204   * @return POJO
205   */
206  public static TableState convert(TableName tableName, HBaseProtos.TableState tableState) {
207    TableState.State state = State.convert(tableState.getState());
208    return new TableState(tableName, state);
209  }
210
211  public static TableState parseFrom(TableName tableName, byte[] bytes)
212      throws DeserializationException {
213    try {
214      return convert(tableName, HBaseProtos.TableState.parseFrom(bytes));
215    } catch (InvalidProtocolBufferException e) {
216      throw new DeserializationException(e);
217    }
218  }
219
220  /**
221   * Static version of state checker
222   * @param target equals to any of
223   * @return true if satisfies
224   */
225  public boolean isInStates(State... target) {
226    for (State tableState : target) {
227      if (this.state.equals(tableState)) {
228        return true;
229      }
230    }
231    return false;
232  }
233
234  @Override
235  public boolean equals(Object o) {
236    if (this == o) return true;
237    if (o == null || getClass() != o.getClass()) return false;
238
239    TableState that = (TableState) o;
240
241    if (state != that.state) return false;
242    if (tableName != null ? !tableName.equals(that.tableName) : that.tableName != null)
243      return false;
244
245    return true;
246  }
247
248  @Override
249  public int hashCode() {
250    int result = (tableName != null ? tableName.hashCode() : 0);
251    result = 31 * result + (state != null ? state.hashCode() : 0);
252    return result;
253  }
254
255  @Override
256  public String toString() {
257    return "tableName=" + tableName + ", state=" + state;
258  }
259}