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.hadoop.hbase.TableName;
021import org.apache.hadoop.hbase.exceptions.DeserializationException;
022import org.apache.yetus.audience.InterfaceAudience;
023import org.apache.yetus.audience.InterfaceStability;
024
025import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
026
027import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
028
029/**
030 * Represents table state.
031 */
032@InterfaceAudience.Private
033public class TableState {
034
035  @InterfaceAudience.Private
036  @InterfaceStability.Evolving
037  public static enum State {
038    ENABLED,
039    DISABLED,
040    DISABLING,
041    ENABLING;
042
043    /**
044     * Covert from PB version of State
045     * @param state convert from n
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 n
070     */
071    public HBaseProtos.TableState.State convert() {
072      HBaseProtos.TableState.State state;
073      switch (this) {
074        case ENABLED:
075          state = HBaseProtos.TableState.State.ENABLED;
076          break;
077        case DISABLED:
078          state = HBaseProtos.TableState.State.DISABLED;
079          break;
080        case DISABLING:
081          state = HBaseProtos.TableState.State.DISABLING;
082          break;
083        case ENABLING:
084          state = HBaseProtos.TableState.State.ENABLING;
085          break;
086        default:
087          throw new IllegalStateException(this.toString());
088      }
089      return state;
090    }
091
092  }
093
094  private final TableName tableName;
095  private final State state;
096
097  /** Returns True if table is {@link State#ENABLED}. */
098  public boolean isEnabled() {
099    return isInStates(State.ENABLED);
100  }
101
102  /** Returns True if table is {@link State#ENABLING}. */
103  public boolean isEnabling() {
104    return isInStates(State.ENABLING);
105  }
106
107  /** Returns True if {@link State#ENABLED} or {@link State#ENABLING} */
108  public boolean isEnabledOrEnabling() {
109    return isInStates(State.ENABLED, State.ENABLING);
110  }
111
112  /** Returns True if table is disabled. */
113  public boolean isDisabled() {
114    return isInStates(State.DISABLED);
115  }
116
117  /** Returns True if table is disabling. */
118  public boolean isDisabling() {
119    return isInStates(State.DISABLING);
120  }
121
122  /** Returns True if {@link State#DISABLED} or {@link State#DISABLED} */
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  /** Returns table state */
138  public State getState() {
139    return state;
140  }
141
142  /**
143   * Table name for state n
144   */
145  public TableName getTableName() {
146    return tableName;
147  }
148
149  /**
150   * Check that table in given states
151   * @param state state
152   * @return true if satisfies
153   */
154  public boolean inStates(State state) {
155    return this.state.equals(state);
156  }
157
158  /**
159   * Check that table in given states
160   * @param states state list
161   * @return true if satisfies
162   */
163  public boolean inStates(State... states) {
164    for (State s : states) {
165      if (s.equals(this.state)) return true;
166    }
167    return false;
168  }
169
170  /**
171   * Covert to PB version of TableState n
172   */
173  public HBaseProtos.TableState convert() {
174    return HBaseProtos.TableState.newBuilder().setState(this.state.convert()).build();
175  }
176
177  /**
178   * Covert from PB version of TableState
179   * @param tableName  table this state of
180   * @param tableState convert from n
181   */
182  public static TableState convert(TableName tableName, HBaseProtos.TableState tableState) {
183    TableState.State state = State.convert(tableState.getState());
184    return new TableState(tableName, state);
185  }
186
187  public static TableState parseFrom(TableName tableName, byte[] bytes)
188    throws DeserializationException {
189    try {
190      return convert(tableName, HBaseProtos.TableState.parseFrom(bytes));
191    } catch (InvalidProtocolBufferException e) {
192      throw new DeserializationException(e);
193    }
194  }
195
196  /**
197   * Static version of state checker
198   * @param target equals to any of
199   * @return true if satisfies
200   */
201  public boolean isInStates(State... target) {
202    for (State tableState : target) {
203      if (this.state.equals(tableState)) {
204        return true;
205      }
206    }
207    return false;
208  }
209
210  @Override
211  public boolean equals(Object o) {
212    if (this == o) {
213      return true;
214    }
215    if (!(o instanceof TableState)) {
216      return false;
217    }
218    TableState that = (TableState) o;
219    if (state != that.state) {
220      return false;
221    }
222    if (tableName != null ? !tableName.equals(that.tableName) : that.tableName != null) {
223      return false;
224    }
225    return true;
226  }
227
228  @Override
229  public int hashCode() {
230    int result = (tableName != null ? tableName.hashCode() : 0);
231    result = 31 * result + (state != null ? state.hashCode() : 0);
232    return result;
233  }
234
235  @Override
236  public String toString() {
237    return "tableName=" + tableName + ", state=" + state;
238  }
239}