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.rest.model;
019
020import java.io.IOException;
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.List;
024import javax.xml.bind.annotation.XmlElementRef;
025import javax.xml.bind.annotation.XmlRootElement;
026import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
027import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
028import org.apache.hadoop.hbase.rest.protobuf.generated.TableListMessage.TableList;
029import org.apache.yetus.audience.InterfaceAudience;
030
031/**
032 * Simple representation of a list of table names.
033 */
034@XmlRootElement(name = "TableList")
035@InterfaceAudience.Private
036public class TableListModel implements Serializable, ProtobufMessageHandler {
037
038  private static final long serialVersionUID = 1L;
039
040  private List<TableModel> tables = new ArrayList<>();
041
042  /**
043   * Default constructor
044   */
045  public TableListModel() {
046  }
047
048  /**
049   * Add the table name model to the list
050   * @param table the table model
051   */
052  public void add(TableModel table) {
053    tables.add(table);
054  }
055
056  /**
057   * @param index the index
058   * @return the table model
059   */
060  public TableModel get(int index) {
061    return tables.get(index);
062  }
063
064  /** Returns the tables */
065  @XmlElementRef(name = "table")
066  public List<TableModel> getTables() {
067    return tables;
068  }
069
070  /**
071   * @param tables the tables to set
072   */
073  public void setTables(List<TableModel> tables) {
074    this.tables = tables;
075  }
076
077  /*
078   * (non-Javadoc)
079   * @see java.lang.Object#toString()
080   */
081  @Override
082  public String toString() {
083    StringBuilder sb = new StringBuilder();
084    for (TableModel aTable : tables) {
085      sb.append(aTable.toString());
086      sb.append('\n');
087    }
088    return sb.toString();
089  }
090
091  @Override
092  public byte[] createProtobufOutput() {
093    TableList.Builder builder = TableList.newBuilder();
094    for (TableModel aTable : tables) {
095      builder.addName(aTable.getName());
096    }
097    return builder.build().toByteArray();
098  }
099
100  @Override
101  public ProtobufMessageHandler getObjectFromMessage(byte[] message) throws IOException {
102    TableList.Builder builder = TableList.newBuilder();
103    ProtobufUtil.mergeFrom(builder, message);
104    for (String table : builder.getNameList()) {
105      this.add(new TableModel(table));
106    }
107    return this;
108  }
109}