View Javadoc

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