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