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