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 com.fasterxml.jackson.annotation.JsonProperty;
021import java.io.IOException;
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.List;
025import javax.xml.bind.annotation.XmlAccessType;
026import javax.xml.bind.annotation.XmlAccessorType;
027import javax.xml.bind.annotation.XmlAttribute;
028import javax.xml.bind.annotation.XmlElement;
029import javax.xml.bind.annotation.XmlRootElement;
030import org.apache.commons.lang3.builder.EqualsBuilder;
031import org.apache.commons.lang3.builder.HashCodeBuilder;
032import org.apache.commons.lang3.builder.ToStringBuilder;
033import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.apache.yetus.audience.InterfaceAudience;
036
037/**
038 * Representation of a row. A row is a related set of cells, grouped by common row key. RowModels do
039 * not appear in results by themselves. They are always encapsulated within CellSetModels.
040 *
041 * <pre>
042 * &lt;complexType name="Row"&gt;
043 *   &lt;sequence&gt;
044 *     &lt;element name="key" type="base64Binary"&gt;&lt;/element&gt;
045 *     &lt;element name="cell" type="tns:Cell"
046 *       maxOccurs="unbounded" minOccurs="1"&gt;&lt;/element&gt;
047 *   &lt;/sequence&gt;
048 * &lt;/complexType&gt;
049 * </pre>
050 */
051@XmlRootElement(name = "Row")
052@XmlAccessorType(XmlAccessType.FIELD)
053@InterfaceAudience.Private
054public class RowModel implements ProtobufMessageHandler, Serializable {
055  private static final long serialVersionUID = 1L;
056
057  @JsonProperty("key")
058  @XmlAttribute
059  private byte[] key;
060
061  @JsonProperty("Cell")
062  @XmlElement(name = "Cell")
063  private List<CellModel> cells = new ArrayList<>();
064
065  /**
066   * Default constructor
067   */
068  public RowModel() {
069  }
070
071  /**
072   * Constructor
073   * @param key the row key
074   */
075  public RowModel(final String key) {
076    this(Bytes.toBytes(key));
077  }
078
079  /**
080   * Constructor
081   * @param key the row key
082   */
083  public RowModel(final byte[] key) {
084    this.key = key;
085    cells = new ArrayList<>();
086  }
087
088  /**
089   * Constructor
090   * @param key   the row key
091   * @param cells the cells
092   */
093  public RowModel(final String key, final List<CellModel> cells) {
094    this(Bytes.toBytes(key), cells);
095  }
096
097  /**
098   * Constructor
099   * @param key   the row key
100   * @param cells the cells
101   */
102  public RowModel(final byte[] key, final List<CellModel> cells) {
103    this.key = key;
104    this.cells = cells;
105  }
106
107  /**
108   * Adds a cell to the list of cells for this row
109   * @param cell the cell
110   */
111  public void addCell(CellModel cell) {
112    cells.add(cell);
113  }
114
115  /** Returns the row key */
116  public byte[] getKey() {
117    return key;
118  }
119
120  /**
121   * @param key the row key
122   */
123  public void setKey(byte[] key) {
124    this.key = key;
125  }
126
127  /** Returns the cells */
128  public List<CellModel> getCells() {
129    return cells;
130  }
131
132  @Override
133  public byte[] createProtobufOutput() {
134    // there is no standalone row protobuf message
135    throw new UnsupportedOperationException("no protobuf equivalent to RowModel");
136  }
137
138  @Override
139  public ProtobufMessageHandler getObjectFromMessage(byte[] message) throws IOException {
140    // there is no standalone row protobuf message
141    throw new UnsupportedOperationException("no protobuf equivalent to RowModel");
142  }
143
144  @Override
145  public boolean equals(Object obj) {
146    if (obj == null) {
147      return false;
148    }
149    if (obj == this) {
150      return true;
151    }
152    if (obj.getClass() != getClass()) {
153      return false;
154    }
155    RowModel rowModel = (RowModel) obj;
156    return new EqualsBuilder().append(key, rowModel.key).append(cells, rowModel.cells).isEquals();
157  }
158
159  @Override
160  public int hashCode() {
161    return new HashCodeBuilder().append(key).append(cells).toHashCode();
162  }
163
164  @Override
165  public String toString() {
166    return new ToStringBuilder(this).append("key", key).append("cells", cells).toString();
167  }
168}