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.util;
019
020import org.apache.yetus.audience.InterfaceAudience;
021
022/**
023 * A read only version of the {@link ByteRange}.
024 */
025@InterfaceAudience.Public
026public class SimpleByteRange extends AbstractByteRange {
027  public SimpleByteRange() {
028  }
029
030  public SimpleByteRange(int capacity) {
031    this(new byte[capacity]);
032  }
033
034  /**
035   * Create a new {@code ByteRange} over the provided {@code bytes}.
036   * @param bytes The array to wrap.
037   */
038  public SimpleByteRange(byte[] bytes) {
039    set(bytes);
040  }
041
042  /**
043   * Create a new {@code ByteRange} over the provided {@code bytes}.
044   * @param bytes  The array to wrap.
045   * @param offset The offset into {@code bytes} considered the beginning of this range.
046   * @param length The length of this range.
047   */
048  public SimpleByteRange(byte[] bytes, int offset, int length) {
049    set(bytes, offset, length);
050  }
051
052  //
053  // methods for managing the backing array and range viewport
054  //
055
056  @Override
057  public ByteRange unset() {
058    throw new ReadOnlyByteRangeException();
059  }
060
061  @Override
062  public ByteRange set(int capacity) {
063    if (super.bytes != null) {
064      throw new ReadOnlyByteRangeException();
065    }
066    return super.set(capacity);
067  }
068
069  @Override
070  public ByteRange set(byte[] bytes) {
071    if (super.bytes != null) {
072      throw new ReadOnlyByteRangeException();
073    }
074    return super.set(bytes);
075  }
076
077  @Override
078  public ByteRange set(byte[] bytes, int offset, int length) {
079    if (super.bytes != null) {
080      throw new ReadOnlyByteRangeException();
081    }
082    return super.set(bytes, offset, length);
083  }
084
085  //
086  // methods for retrieving data
087  //
088  @Override
089  public ByteRange put(int index, byte val) {
090    throw new ReadOnlyByteRangeException();
091  }
092
093  @Override
094  public ByteRange put(int index, byte[] val) {
095    throw new ReadOnlyByteRangeException();
096  }
097
098  @Override
099  public ByteRange put(int index, byte[] val, int offset, int length) {
100    throw new ReadOnlyByteRangeException();
101  }
102
103  //
104  // methods for duplicating the current instance
105  //
106
107  @Override
108  public ByteRange shallowCopy() {
109    SimpleByteRange clone = new SimpleByteRange(bytes, offset, length);
110    if (isHashCached()) {
111      clone.hash = hash;
112    }
113    return clone;
114  }
115
116  @Override
117  public ByteRange shallowCopySubRange(int innerOffset, int copyLength) {
118    SimpleByteRange clone = new SimpleByteRange(bytes, offset + innerOffset, copyLength);
119    if (isHashCached()) {
120      clone.hash = hash;
121    }
122    return clone;
123  }
124
125  @Override
126  public boolean equals(Object thatObject) {
127    if (thatObject == null) {
128      return false;
129    }
130    if (this == thatObject) {
131      return true;
132    }
133    if (hashCode() != thatObject.hashCode()) {
134      return false;
135    }
136    if (!(thatObject instanceof SimpleByteRange)) {
137      return false;
138    }
139    SimpleByteRange that = (SimpleByteRange) thatObject;
140    return Bytes.equals(bytes, offset, length, that.bytes, that.offset, that.length);
141  }
142
143  @Override
144  public ByteRange deepCopy() {
145    SimpleByteRange clone = new SimpleByteRange(deepCopyToNewArray());
146    if (isHashCached()) {
147      clone.hash = hash;
148    }
149    return clone;
150  }
151
152  @Override
153  public ByteRange putInt(int index, int val) {
154    throw new ReadOnlyByteRangeException();
155  }
156
157  @Override
158  public ByteRange putLong(int index, long val) {
159    throw new ReadOnlyByteRangeException();
160  }
161
162  @Override
163  public ByteRange putShort(int index, short val) {
164    throw new ReadOnlyByteRangeException();
165  }
166
167  @Override
168  public int putVLong(int index, long val) {
169    throw new ReadOnlyByteRangeException();
170  }
171}