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
046   *          of this range.
047   * @param length The length of this range.
048   */
049  public SimpleByteRange(byte[] bytes, int offset, int length) {
050    set(bytes, offset, length);
051  }
052
053  //
054  // methods for managing the backing array and range viewport
055  //
056
057  @Override
058  public ByteRange unset() {
059    throw new ReadOnlyByteRangeException();
060  }
061
062  @Override
063  public ByteRange set(int capacity) {
064    if (super.bytes != null) {
065      throw new ReadOnlyByteRangeException();
066    }
067    return super.set(capacity);
068  }
069
070  @Override
071  public ByteRange set(byte[] bytes) {
072    if (super.bytes != null) {
073      throw new ReadOnlyByteRangeException();
074    }
075    return super.set(bytes);
076  }
077
078  @Override
079  public ByteRange set(byte[] bytes, int offset, int length) {
080    if (super.bytes != null) {
081      throw new ReadOnlyByteRangeException();
082    }
083    return super.set(bytes, offset, length);
084  }
085
086  //
087  // methods for retrieving data
088  //
089  @Override
090  public ByteRange put(int index, byte val) {
091    throw new ReadOnlyByteRangeException();
092  }
093
094  @Override
095  public ByteRange put(int index, byte[] val) {
096    throw new ReadOnlyByteRangeException();
097  }
098
099  @Override
100  public ByteRange put(int index, byte[] val, int offset, int length) {
101    throw new ReadOnlyByteRangeException();
102  }
103
104  //
105  // methods for duplicating the current instance
106  //
107
108  @Override
109  public ByteRange shallowCopy() {
110    SimpleByteRange clone = new SimpleByteRange(bytes, offset, length);
111    if (isHashCached()) {
112      clone.hash = hash;
113    }
114    return clone;
115  }
116
117  @Override
118  public ByteRange shallowCopySubRange(int innerOffset, int copyLength) {
119    SimpleByteRange clone = new SimpleByteRange(bytes, offset + innerOffset,
120        copyLength);
121    if (isHashCached()) {
122      clone.hash = hash;
123    }
124    return clone;
125  }
126
127  @Override
128  public boolean equals(Object thatObject) {
129    if (thatObject == null){
130      return false;
131    }
132    if (this == thatObject) {
133      return true;
134    }
135    if (hashCode() != thatObject.hashCode()) {
136      return false;
137    }
138    if (!(thatObject instanceof SimpleByteRange)) {
139      return false;
140    }
141    SimpleByteRange that = (SimpleByteRange) thatObject;
142    return Bytes.equals(bytes, offset, length, that.bytes, that.offset, that.length);
143  }
144
145  @Override
146  public ByteRange deepCopy() {
147    SimpleByteRange clone = new SimpleByteRange(deepCopyToNewArray());
148    if (isHashCached()) {
149      clone.hash = hash;
150    }
151    return clone;
152  }
153
154  @Override
155  public ByteRange putInt(int index, int val) {
156    throw new ReadOnlyByteRangeException();
157  }
158
159  @Override
160  public ByteRange putLong(int index, long val) {
161    throw new ReadOnlyByteRangeException();
162  }
163
164  @Override
165  public ByteRange putShort(int index, short val) {
166    throw new ReadOnlyByteRangeException();
167  }
168
169  @Override
170  public int putVLong(int index, long val) {
171    throw new ReadOnlyByteRangeException();
172  }
173}