View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest.model;
21  
22  import java.io.IOException;
23  import java.io.Serializable;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import javax.xml.bind.annotation.XmlElementRef;
28  import javax.xml.bind.annotation.XmlRootElement;
29  
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
32  import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
33  import org.apache.hadoop.hbase.rest.protobuf.generated.TableListMessage.TableList;
34  
35  /**
36   * Simple representation of a list of table names.
37   */
38  @XmlRootElement(name="TableList")
39  @InterfaceAudience.Private
40  public class TableListModel implements Serializable, ProtobufMessageHandler {
41  
42    private static final long serialVersionUID = 1L;
43  
44    private List<TableModel> tables = new ArrayList<TableModel>();
45  
46    /**
47     * Default constructor
48     */
49    public TableListModel() {}
50  
51    /**
52     * Add the table name model to the list
53     * @param table the table model
54     */
55    public void add(TableModel table) {
56      tables.add(table);
57    }
58    
59    /**
60     * @param index the index
61     * @return the table model
62     */
63    public TableModel get(int index) {
64      return tables.get(index);
65    }
66  
67    /**
68     * @return the tables
69     */
70    @XmlElementRef(name="table")
71    public List<TableModel> getTables() {
72      return tables;
73    }
74  
75    /**
76     * @param tables the tables to set
77     */
78    public void setTables(List<TableModel> tables) {
79      this.tables = tables;
80    }
81  
82    /* (non-Javadoc)
83     * @see java.lang.Object#toString()
84     */
85    @Override
86    public String toString() {
87      StringBuilder sb = new StringBuilder();
88      for(TableModel aTable : tables) {
89        sb.append(aTable.toString());
90        sb.append('\n');
91      }
92      return sb.toString();
93    }
94  
95    @Override
96    public byte[] createProtobufOutput() {
97      TableList.Builder builder = TableList.newBuilder();
98      for (TableModel aTable : tables) {
99        builder.addName(aTable.getName());
100     }
101     return builder.build().toByteArray();
102   }
103 
104   @Override
105   public ProtobufMessageHandler getObjectFromMessage(byte[] message)
106       throws IOException {
107     TableList.Builder builder = TableList.newBuilder();
108     ProtobufUtil.mergeFrom(builder, message);
109     for (String table: builder.getNameList()) {
110       this.add(new TableModel(table));
111     }
112     return this;
113   }
114 }