001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.rest.model;
021
022import java.io.IOException;
023import java.io.Serializable;
024import java.util.ArrayList;
025import java.util.List;
026
027import javax.xml.bind.annotation.XmlElementRef;
028import javax.xml.bind.annotation.XmlRootElement;
029
030import org.apache.yetus.audience.InterfaceAudience;
031import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
032import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
033import org.apache.hadoop.hbase.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   * 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  /**
068   * @return the tables
069   */
070  @XmlElementRef(name="table")
071  public List<TableModel> getTables() {
072    return tables;
073  }
074
075  /**
076   * @param tables the tables to set
077   */
078  public void setTables(List<TableModel> tables) {
079    this.tables = tables;
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 byte[] createProtobufOutput() {
097    TableList.Builder builder = TableList.newBuilder();
098    for (TableModel aTable : tables) {
099      builder.addName(aTable.getName());
100    }
101    return builder.build().toByteArray();
102  }
103
104  @Override
105  public ProtobufMessageHandler getObjectFromMessage(byte[] message)
106      throws IOException {
107    TableList.Builder builder = TableList.newBuilder();
108    ProtobufUtil.mergeFrom(builder, message);
109    for (String table: builder.getNameList()) {
110      this.add(new TableModel(table));
111    }
112    return this;
113  }
114}