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.rest.ProtobufMessageHandler;
32  import org.apache.hadoop.hbase.rest.protobuf.generated.TableListMessage.TableList;
33  
34  /**
35   * Simple representation of a list of table names.
36   */
37  @XmlRootElement(name="TableList")
38  @InterfaceAudience.Private
39  public class TableListModel implements Serializable, ProtobufMessageHandler {
40  
41  	private static final long serialVersionUID = 1L;
42  
43  	private List<TableModel> tables = new ArrayList<TableModel>();
44  
45  	/**
46  	 * Default constructor
47  	 */
48  	public TableListModel() {}
49  
50  	/**
51  	 * Add the table name model to the list
52  	 * @param table the table model
53  	 */
54  	public void add(TableModel table) {
55  		tables.add(table);
56  	}
57  	
58  	/**
59  	 * @param index the index
60  	 * @return the table model
61  	 */
62  	public TableModel get(int index) {
63  		return tables.get(index);
64  	}
65  
66  	/**
67  	 * @return the tables
68  	 */
69  	@XmlElementRef(name="table")
70  	public List<TableModel> getTables() {
71  		return tables;
72  	}
73  
74  	/**
75  	 * @param tables the tables to set
76  	 */
77  	public void setTables(List<TableModel> tables) {
78  		this.tables = tables;
79  	}
80  
81  	/* (non-Javadoc)
82  	 * @see java.lang.Object#toString()
83  	 */
84  	@Override
85  	public String toString() {
86  		StringBuilder sb = new StringBuilder();
87  		for(TableModel aTable : tables) {
88  			sb.append(aTable.toString());
89  			sb.append('\n');
90  		}
91  		return sb.toString();
92  	}
93  
94  	@Override
95  	public byte[] createProtobufOutput() {
96  		TableList.Builder builder = TableList.newBuilder();
97  		for (TableModel aTable : tables) {
98  			builder.addName(aTable.getName());
99  		}
100 		return builder.build().toByteArray();
101 	}
102 
103   @Override
104   public ProtobufMessageHandler getObjectFromMessage(byte[] message)
105       throws IOException {
106     TableList.Builder builder = TableList.newBuilder();
107     builder.mergeFrom(message);
108     for (String table: builder.getNameList()) {
109       this.add(new TableModel(table));
110     }
111     return this;
112   }
113 }