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