View Javadoc

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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.hadoop.hbase.client;
19  
20  import 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.protobuf.generated.HBaseProtos;
26  
27  /**
28   * Represents table state.
29   */
30  @InterfaceAudience.Private
31  public class TableState {
32  
33    @InterfaceAudience.Public
34    @InterfaceStability.Evolving
35    public static enum State {
36      ENABLED,
37      DISABLED,
38      DISABLING,
39      ENABLING;
40  
41      /**
42       * Covert from PB version of State
43       *
44       * @param state convert from
45       * @return POJO
46       */
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());
64        }
65        return ret;
66      }
67  
68      /**
69       * Covert to PB version of State
70       *
71       * @return PB
72       */
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());
90        }
91        return state;
92      }
93  
94    }
95  
96    private final TableName tableName;
97    private final State state;
98  
99    /**
100    * Create instance of TableState.
101    * @param tableName name of the table
102    * @param state table state
103    */
104   public TableState(TableName tableName, State state) {
105     this.tableName = tableName;
106     this.state = state;
107   }
108 
109   /**
110    * @return table state
111    */
112   public State getState() {
113     return state;
114   }
115 
116   /**
117    * Table name for state
118    *
119    * @return milliseconds
120    */
121   public TableName getTableName() {
122     return tableName;
123   }
124 
125   /**
126    * Check that table in given states
127    * @param state state
128    * @return true if satisfies
129    */
130   public boolean inStates(State state) {
131     return this.state.equals(state);
132   }
133 
134   /**
135    * Check that table in given states
136    * @param states state list
137    * @return true if satisfies
138    */
139   public boolean inStates(State... states) {
140     for (State s : states) {
141       if (s.equals(this.state))
142         return true;
143     }
144     return false;
145   }
146 
147 
148   /**
149    * Covert to PB version of TableState
150    * @return PB
151    */
152   public HBaseProtos.TableState convert() {
153     return HBaseProtos.TableState.newBuilder()
154         .setState(this.state.convert()).build();
155   }
156 
157   /**
158    * Covert from PB version of TableState
159    *
160    * @param tableName table this state of
161    * @param tableState convert from
162    * @return POJO
163    */
164   public static TableState convert(TableName tableName, HBaseProtos.TableState tableState) {
165     TableState.State state = State.convert(tableState.getState());
166     return new TableState(tableName, state);
167   }
168 
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);
175     }
176   }
177 
178   /**
179    * Static version of state checker
180    * @param state desired
181    * @param target equals to any of
182    * @return true if satisfies
183    */
184   public static boolean isInStates(State state, State... target) {
185     for (State tableState : target) {
186       if (state.equals(tableState))
187         return true;
188     }
189     return false;
190   }
191 
192   @Override
193   public boolean equals(Object o) {
194     if (this == o) return true;
195     if (o == null || getClass() != o.getClass()) return false;
196 
197     TableState that = (TableState) o;
198 
199     if (state != that.state) return false;
200     if (tableName != null ? !tableName.equals(that.tableName) : that.tableName != null)
201       return false;
202 
203     return true;
204   }
205 
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;
211   }
212 
213   @Override
214   public String toString() {
215     return "TableState{" +
216         ", tableName=" + tableName +
217         ", state=" + state +
218         '}';
219   }
220 }