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 */
018
019package org.apache.hadoop.hbase.util;
020
021import org.apache.yetus.audience.InterfaceAudience;
022
023/**
024 * Extends the basic {@link SimpleMutableByteRange} implementation with position
025 * support and it is a readonly version. {@code position} is considered
026 * transient, not fundamental to the definition of the range, and does not
027 * participate in {@link #compareTo(ByteRange)}, {@link #hashCode()}, or
028 * {@link #equals(Object)}. {@code Position} is retained by copy operations.
029 */
030@InterfaceAudience.Public
031@edu.umd.cs.findbugs.annotations.SuppressWarnings("EQ_DOESNT_OVERRIDE_EQUALS")
032public class SimplePositionedByteRange extends AbstractPositionedByteRange {
033
034  /**
035   * Create a new {@code PositionedByteRange} lacking a backing array and with
036   * an undefined viewport.
037   */
038  public SimplePositionedByteRange() {
039    super();
040  }
041
042  /**
043   * Create a new {@code PositionedByteRange} over a new backing array of
044   * size {@code capacity}. The range's offset and length are 0 and
045   * {@code capacity}, respectively.
046   * @param capacity the size of the backing array.
047   */
048  public SimplePositionedByteRange(int capacity) {
049    this(new byte[capacity]);
050  }
051
052  /**
053   * Create a new {@code PositionedByteRange} over the provided {@code bytes}.
054   * @param bytes The array to wrap.
055   */
056  public SimplePositionedByteRange(byte[] bytes) {
057    set(bytes);
058  }
059
060  /**
061   * Create a new {@code PositionedByteRange} over the provided {@code bytes}.
062   * @param bytes The array to wrap.
063   * @param offset The offset into {@code bytes} considered the beginning
064   *          of this range.
065   * @param length The length of this range.
066   */
067  public SimplePositionedByteRange(byte[] bytes, int offset, int length) {
068    set(bytes, offset, length);
069  }
070
071  @Override
072  public PositionedByteRange set(int capacity) {
073    if (super.bytes != null) {
074      throw new ReadOnlyByteRangeException();
075    }
076    return super.set(capacity);
077  }
078
079  @Override
080  public PositionedByteRange set(byte[] bytes) {
081    if (super.bytes != null) {
082      throw new ReadOnlyByteRangeException();
083    }
084    return super.set(bytes);
085  }
086
087  @Override
088  public PositionedByteRange set(byte[] bytes, int offset, int length) {
089    if (super.bytes != null) {
090      throw new ReadOnlyByteRangeException();
091    }
092    return super.set(bytes, offset, length);
093  }
094
095  @Override
096  public PositionedByteRange put(byte val) {
097    throw new ReadOnlyByteRangeException();
098  }
099
100  @Override
101  public PositionedByteRange putShort(short val) {
102    throw new ReadOnlyByteRangeException();
103  }
104
105  @Override
106  public PositionedByteRange putInt(int val) {
107    throw new ReadOnlyByteRangeException();
108  }
109
110  @Override
111  public PositionedByteRange putLong(long val) {
112    throw new ReadOnlyByteRangeException();
113  }
114
115  @Override
116  public int putVLong(long val) {
117    throw new ReadOnlyByteRangeException();
118  }
119
120  @Override
121  public PositionedByteRange put(byte[] val) {
122    throw new ReadOnlyByteRangeException();
123  }
124
125  @Override
126  public PositionedByteRange put(byte[] val, int offset, int length) {
127    throw new ReadOnlyByteRangeException();
128  }
129
130
131  @Override
132  public PositionedByteRange get(int index, byte[] dst) { super.get(index, dst); return this; }
133
134  @Override
135  public PositionedByteRange get(int index, byte[] dst, int offset, int length) {
136    super.get(index, dst, offset, length);
137    return this;
138  }
139
140  @Override
141  public PositionedByteRange put(int index, byte val) {
142    throw new ReadOnlyByteRangeException();
143  }
144
145  @Override
146  public PositionedByteRange putShort(int index, short val) {
147    throw new ReadOnlyByteRangeException();
148  }
149
150  @Override
151  public PositionedByteRange putInt(int index, int val) {
152    throw new ReadOnlyByteRangeException();
153  }
154  
155  @Override
156  public int putVLong(int index, long val) {
157    throw new ReadOnlyByteRangeException();
158  }
159
160  @Override
161  public PositionedByteRange putLong(int index, long val) {
162    throw new ReadOnlyByteRangeException();
163  }
164
165  @Override
166  public PositionedByteRange put(int index, byte[] val) {
167    throw new ReadOnlyByteRangeException();
168  }
169
170  @Override
171  public PositionedByteRange put(int index, byte[] val, int offset, int length) {
172    throw new ReadOnlyByteRangeException();
173  }
174
175  @Override
176  public PositionedByteRange deepCopy() {
177    SimplePositionedByteRange clone = new SimplePositionedByteRange(deepCopyToNewArray());
178    clone.position = this.position;
179    return clone;
180  }
181
182  @Override
183  public PositionedByteRange shallowCopy() {
184    SimplePositionedByteRange clone = new SimplePositionedByteRange(bytes, offset, length);
185    clone.position = this.position;
186    return clone;
187  }
188
189  @Override
190  public PositionedByteRange shallowCopySubRange(int innerOffset, int copyLength) {
191    SimplePositionedByteRange clone =
192        new SimplePositionedByteRange(bytes, offset + innerOffset, copyLength);
193    clone.position = this.position;
194    return clone;
195  }
196
197  @Override
198  public PositionedByteRange setLimit(int limit) {
199    throw new ReadOnlyByteRangeException();
200  }
201  
202  @Override
203  public PositionedByteRange unset() {
204    throw new ReadOnlyByteRangeException();
205  }
206
207}