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;
019
020import java.util.List;
021import org.apache.commons.lang3.ArrayUtils;
022import org.apache.yetus.audience.InterfaceAudience;
023
024@InterfaceAudience.Private
025public abstract class ExtendedCellBuilderImpl implements ExtendedCellBuilder {
026  protected byte[] row = null;
027  protected int rOffset = 0;
028  protected int rLength = 0;
029  protected byte[] family = null;
030  protected int fOffset = 0;
031  protected int fLength = 0;
032  protected byte[] qualifier = null;
033  protected int qOffset = 0;
034  protected int qLength = 0;
035  protected long timestamp = HConstants.LATEST_TIMESTAMP;
036  protected KeyValue.Type type = null;
037  protected byte[] value = null;
038  protected int vOffset = 0;
039  protected int vLength = 0;
040  protected long seqId = 0;
041  protected byte[] tags = null;
042  protected int tagsOffset = 0;
043  protected int tagsLength = 0;
044
045  @Override
046  public ExtendedCellBuilder setRow(final byte[] row) {
047    return setRow(row, 0, ArrayUtils.getLength(row));
048  }
049
050  @Override
051  public ExtendedCellBuilder setRow(final byte[] row, int rOffset, int rLength) {
052    this.row = row;
053    this.rOffset = rOffset;
054    this.rLength = rLength;
055    return this;
056  }
057
058  @Override
059  public ExtendedCellBuilder setFamily(final byte[] family) {
060    return setFamily(family, 0, ArrayUtils.getLength(family));
061  }
062
063  @Override
064  public ExtendedCellBuilder setFamily(final byte[] family, int fOffset, int fLength) {
065    this.family = family;
066    this.fOffset = fOffset;
067    this.fLength = fLength;
068    return this;
069  }
070
071  @Override
072  public ExtendedCellBuilder setQualifier(final byte[] qualifier) {
073    return setQualifier(qualifier, 0, ArrayUtils.getLength(qualifier));
074  }
075
076  @Override
077  public ExtendedCellBuilder setQualifier(final byte[] qualifier, int qOffset, int qLength) {
078    this.qualifier = qualifier;
079    this.qOffset = qOffset;
080    this.qLength = qLength;
081    return this;
082  }
083
084  @Override
085  public ExtendedCellBuilder setTimestamp(final long timestamp) {
086    this.timestamp = timestamp;
087    return this;
088  }
089
090  @Override
091  public ExtendedCellBuilder setType(final Cell.Type type) {
092    this.type = PrivateCellUtil.toTypeByte(type);
093    return this;
094  }
095
096  @Override
097  public ExtendedCellBuilder setType(final byte type) {
098    this.type = KeyValue.Type.codeToType(type);
099    return this;
100  }
101
102  @Override
103  public ExtendedCellBuilder setValue(final byte[] value) {
104    return setValue(value, 0, ArrayUtils.getLength(value));
105  }
106
107  @Override
108  public ExtendedCellBuilder setValue(final byte[] value, int vOffset, int vLength) {
109    this.value = value;
110    this.vOffset = vOffset;
111    this.vLength = vLength;
112    return this;
113  }
114
115  @Override
116  public ExtendedCellBuilder setTags(final byte[] tags) {
117    return setTags(tags, 0, ArrayUtils.getLength(tags));
118  }
119
120  @Override
121  public ExtendedCellBuilder setTags(final byte[] tags, int tagsOffset, int tagsLength) {
122    this.tags = tags;
123    this.tagsOffset = tagsOffset;
124    this.tagsLength = tagsLength;
125    return this;
126  }
127
128  @Override
129  public ExtendedCellBuilder setTags(List<Tag> tags) {
130    byte[] tagBytes = TagUtil.fromList(tags);
131    return setTags(tagBytes);
132  }
133
134  @Override
135  public ExtendedCellBuilder setSequenceId(final long seqId) {
136    this.seqId = seqId;
137    return this;
138  }
139
140  private void checkBeforeBuild() {
141    if (type == null) {
142      throw new IllegalArgumentException("The type can't be NULL");
143    }
144  }
145
146  protected abstract ExtendedCell innerBuild();
147
148  @Override
149  public ExtendedCell build() {
150    checkBeforeBuild();
151    return innerBuild();
152  }
153
154  @Override
155  public ExtendedCellBuilder clear() {
156    row = null;
157    rOffset = 0;
158    rLength = 0;
159    family = null;
160    fOffset = 0;
161    fLength = 0;
162    qualifier = null;
163    qOffset = 0;
164    qLength = 0;
165    timestamp = HConstants.LATEST_TIMESTAMP;
166    type = null;
167    value = null;
168    vOffset = 0;
169    vLength = 0;
170    seqId = 0;
171    tags = null;
172    tagsOffset = 0;
173    tagsLength = 0;
174    return this;
175  }
176}