View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.util;
19  
20  import org.apache.hadoop.hbase.classification.InterfaceAudience;
21  import org.apache.hadoop.hbase.classification.InterfaceStability;
22  
23  /**
24   *  A read only version of the {@link ByteRange}.
25   */
26  @InterfaceAudience.Public
27  @InterfaceStability.Evolving
28  public class SimpleByteRange extends AbstractByteRange {
29    public SimpleByteRange() {
30    }
31  
32    public SimpleByteRange(int capacity) {
33      this(new byte[capacity]);
34    }
35  
36    /**
37     * Create a new {@code ByteRange} over the provided {@code bytes}.
38     * @param bytes The array to wrap.
39     */
40    public SimpleByteRange(byte[] bytes) {
41      set(bytes);
42    }
43  
44    /**
45     * Create a new {@code ByteRange} over the provided {@code bytes}.
46     * @param bytes The array to wrap.
47     * @param offset The offset into {@code bytes} considered the beginning
48     *          of this range.
49     * @param length The length of this range.
50     */
51    public SimpleByteRange(byte[] bytes, int offset, int length) {
52      set(bytes, offset, length);
53    }
54  
55    //
56    // methods for managing the backing array and range viewport
57    //
58  
59    @Override
60    public ByteRange unset() {
61      throw new ReadOnlyByteRangeException();
62    }
63  
64    @Override
65    public ByteRange set(int capacity) {
66      if (super.bytes != null) {
67        throw new ReadOnlyByteRangeException();
68      }
69      return super.set(capacity);
70    }
71  
72    @Override
73    public ByteRange set(byte[] bytes) {
74      if (super.bytes != null) {
75        throw new ReadOnlyByteRangeException();
76      }
77      return super.set(bytes);
78    }
79  
80    @Override
81    public ByteRange set(byte[] bytes, int offset, int length) {
82      if (super.bytes != null) {
83        throw new ReadOnlyByteRangeException();
84      }
85      return super.set(bytes, offset, length);
86    }
87  
88    //
89    // methods for retrieving data
90    //
91    @Override
92    public ByteRange put(int index, byte val) {
93      throw new ReadOnlyByteRangeException();
94    }
95  
96    @Override
97    public ByteRange put(int index, byte[] val) {
98      throw new ReadOnlyByteRangeException();
99    }
100 
101   @Override
102   public ByteRange put(int index, byte[] val, int offset, int length) {
103     throw new ReadOnlyByteRangeException();
104   }
105 
106   //
107   // methods for duplicating the current instance
108   //
109 
110   @Override
111   public ByteRange shallowCopy() {
112     SimpleByteRange clone = new SimpleByteRange(bytes, offset, length);
113     if (isHashCached()) {
114       clone.hash = hash;
115     }
116     return clone;
117   }
118 
119   @Override
120   public ByteRange shallowCopySubRange(int innerOffset, int copyLength) {
121     SimpleByteRange clone = new SimpleByteRange(bytes, offset + innerOffset,
122         copyLength);
123     if (isHashCached()) {
124       clone.hash = hash;
125     }
126     return clone;
127   }
128 
129   @Override
130   public boolean equals(Object thatObject) {
131     if (thatObject == null){
132       return false;
133     }
134     if (this == thatObject) {
135       return true;
136     }
137     if (hashCode() != thatObject.hashCode()) {
138       return false;
139     }
140     if (!(thatObject instanceof SimpleByteRange)) {
141       return false;
142     }
143     SimpleByteRange that = (SimpleByteRange) thatObject;
144     return Bytes.equals(bytes, offset, length, that.bytes, that.offset, that.length);
145   }
146 
147   @Override
148   public ByteRange deepCopy() {
149     SimpleByteRange clone = new SimpleByteRange(deepCopyToNewArray());
150     if (isHashCached()) {
151       clone.hash = hash;
152     }
153     return clone;
154   }
155 
156   @Override
157   public ByteRange putInt(int index, int val) {
158     throw new ReadOnlyByteRangeException();
159   }
160 
161   @Override
162   public ByteRange putLong(int index, long val) {
163     throw new ReadOnlyByteRangeException();
164   }
165 
166   @Override
167   public ByteRange putShort(int index, short val) {
168     throw new ReadOnlyByteRangeException();
169   }
170 
171   @Override
172   public int putVLong(int index, long val) {
173     throw new ReadOnlyByteRangeException();
174   }
175 }