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 {@link State#ENABLED} or {@link State#ENABLING}
108   */
109  public boolean isEnabledOrEnabling() {
110    return isInStates(State.ENABLED, State.ENABLING);
111  }
112
113  /**
114   * @return True if table is disabled.
115   */
116  public boolean isDisabled() {
117    return isInStates(State.DISABLED);
118  }
119
120  /**
121   * @return True if {@link State#DISABLED} or {@link State#DISABLED}
122   */
123  public boolean isDisabledOrDisabling() {
124    return isInStates(State.DISABLED, State.DISABLING);
125  }
126
127  /**
128   * Create instance of TableState.
129   * @param tableName name of the table
130   * @param state table state
131   */
132  public TableState(TableName tableName, State state) {
133    this.tableName = tableName;
134    this.state = state;
135  }
136
137  /**
138   * @return table state
139   */
140  public State getState() {
141    return state;
142  }
143
144  /**
145   * Table name for state
146   *
147   * @return milliseconds
148   */
149  public TableName getTableName() {
150    return tableName;
151  }
152
153  /**
154   * Check that table in given states
155   * @param state state
156   * @return true if satisfies
157   */
158  public boolean inStates(State state) {
159    return this.state.equals(state);
160  }
161
162  /**
163   * Check that table in given states
164   * @param states state list
165   * @return true if satisfies
166   */
167  public boolean inStates(State... states) {
168    for (State s : states) {
169      if (s.equals(this.state))
170        return true;
171    }
172    return false;
173  }
174
175
176  /**
177   * Covert to PB version of TableState
178   * @return PB
179   */
180  public HBaseProtos.TableState convert() {
181    return HBaseProtos.TableState.newBuilder()
182        .setState(this.state.convert()).build();
183  }
184
185  /**
186   * Covert from PB version of TableState
187   *
188   * @param tableName table this state of
189   * @param tableState convert from
190   * @return POJO
191   */
192  public static TableState convert(TableName tableName, HBaseProtos.TableState tableState) {
193    TableState.State state = State.convert(tableState.getState());
194    return new TableState(tableName, state);
195  }
196
197  public static TableState parseFrom(TableName tableName, byte[] bytes)
198      throws DeserializationException {
199    try {
200      return convert(tableName, HBaseProtos.TableState.parseFrom(bytes));
201    } catch (InvalidProtocolBufferException e) {
202      throw new DeserializationException(e);
203    }
204  }
205
206  /**
207   * Static version of state checker
208   * @param target equals to any of
209   * @return true if satisfies
210   */
211  public boolean isInStates(State... target) {
212    for (State tableState : target) {
213      if (this.state.equals(tableState)) {
214        return true;
215      }
216    }
217    return false;
218  }
219
220  @Override
221  public boolean equals(Object o) {
222    if (this == o) return true;
223    if (o == null || getClass() != o.getClass()) return false;
224
225    TableState that = (TableState) o;
226
227    if (state != that.state) return false;
228    if (tableName != null ? !tableName.equals(that.tableName) : that.tableName != null)
229      return false;
230
231    return true;
232  }
233
234  @Override
235  public int hashCode() {
236    int result = (tableName != null ? tableName.hashCode() : 0);
237    result = 31 * result + (state != null ? state.hashCode() : 0);
238    return result;
239  }
240
241  @Override
242  public String toString() {
243    return "tableName=" + tableName + ", state=" + state;
244  }
245}