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