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