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