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  
19  package org.apache.hadoop.hbase.util;
20  
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.classification.InterfaceStability;
23  
24  /**
25   * Extends the basic {@link SimpleMutableByteRange} implementation with position
26   * support and it is a readonly version. {@code position} is considered
27   * transient, not fundamental to the definition of the range, and does not
28   * participate in {@link #compareTo(ByteRange)}, {@link #hashCode()}, or
29   * {@link #equals(Object)}. {@code Position} is retained by copy operations.
30   */
31  @InterfaceAudience.Public
32  @InterfaceStability.Evolving
33  @edu.umd.cs.findbugs.annotations.SuppressWarnings("EQ_DOESNT_OVERRIDE_EQUALS")
34  public class SimplePositionedByteRange extends AbstractPositionedByteRange {
35  
36    /**
37     * Create a new {@code PositionedByteRange} lacking a backing array and with
38     * an undefined viewport.
39     */
40    public SimplePositionedByteRange() {
41      super();
42    }
43  
44    /**
45     * Create a new {@code PositionedByteRange} over a new backing array of
46     * size {@code capacity}. The range's offset and length are 0 and
47     * {@code capacity}, respectively.
48     * @param capacity the size of the backing array.
49     */
50    public SimplePositionedByteRange(int capacity) {
51      this(new byte[capacity]);
52    }
53  
54    /**
55     * Create a new {@code PositionedByteRange} over the provided {@code bytes}.
56     * @param bytes The array to wrap.
57     */
58    public SimplePositionedByteRange(byte[] bytes) {
59      set(bytes);
60    }
61  
62    /**
63     * Create a new {@code PositionedByteRange} over the provided {@code bytes}.
64     * @param bytes The array to wrap.
65     * @param offset The offset into {@code bytes} considered the beginning
66     *          of this range.
67     * @param length The length of this range.
68     */
69    public SimplePositionedByteRange(byte[] bytes, int offset, int length) {
70      set(bytes, offset, length);
71    }
72  
73    @Override
74    public PositionedByteRange set(int capacity) {
75      if (super.bytes != null) {
76        throw new ReadOnlyByteRangeException();
77      }
78      return super.set(capacity);
79    }
80  
81    @Override
82    public PositionedByteRange set(byte[] bytes) {
83      if (super.bytes != null) {
84        throw new ReadOnlyByteRangeException();
85      }
86      return super.set(bytes);
87    }
88  
89    @Override
90    public PositionedByteRange set(byte[] bytes, int offset, int length) {
91      if (super.bytes != null) {
92        throw new ReadOnlyByteRangeException();
93      }
94      return super.set(bytes, offset, length);
95    }
96  
97    @Override
98    public PositionedByteRange put(byte val) {
99      throw new ReadOnlyByteRangeException();
100   }
101 
102   @Override
103   public PositionedByteRange putShort(short val) {
104     throw new ReadOnlyByteRangeException();
105   }
106 
107   @Override
108   public PositionedByteRange putInt(int val) {
109     throw new ReadOnlyByteRangeException();
110   }
111 
112   @Override
113   public PositionedByteRange putLong(long val) {
114     throw new ReadOnlyByteRangeException();
115   }
116 
117   @Override
118   public int putVLong(long val) {
119     throw new ReadOnlyByteRangeException();
120   }
121 
122   @Override
123   public PositionedByteRange put(byte[] val) {
124     throw new ReadOnlyByteRangeException();
125   }
126 
127   @Override
128   public PositionedByteRange put(byte[] val, int offset, int length) {
129     throw new ReadOnlyByteRangeException();
130   }
131 
132 
133   @Override
134   public PositionedByteRange get(int index, byte[] dst) { super.get(index, dst); return this; }
135 
136   @Override
137   public PositionedByteRange get(int index, byte[] dst, int offset, int length) {
138     super.get(index, dst, offset, length);
139     return this;
140   }
141 
142   @Override
143   public PositionedByteRange put(int index, byte val) {
144     throw new ReadOnlyByteRangeException();
145   }
146 
147   @Override
148   public PositionedByteRange putShort(int index, short val) {
149     throw new ReadOnlyByteRangeException();
150   }
151 
152   @Override
153   public PositionedByteRange putInt(int index, int val) {
154     throw new ReadOnlyByteRangeException();
155   }
156   
157   @Override
158   public int putVLong(int index, long val) {
159     throw new ReadOnlyByteRangeException();
160   }
161 
162   @Override
163   public PositionedByteRange putLong(int index, long val) {
164     throw new ReadOnlyByteRangeException();
165   }
166 
167   @Override
168   public PositionedByteRange put(int index, byte[] val) {
169     throw new ReadOnlyByteRangeException();
170   }
171 
172   @Override
173   public PositionedByteRange put(int index, byte[] val, int offset, int length) {
174     throw new ReadOnlyByteRangeException();
175   }
176 
177   @Override
178   public PositionedByteRange deepCopy() {
179     SimplePositionedByteRange clone = new SimplePositionedByteRange(deepCopyToNewArray());
180     clone.position = this.position;
181     return clone;
182   }
183 
184   @Override
185   public PositionedByteRange shallowCopy() {
186     SimplePositionedByteRange clone = new SimplePositionedByteRange(bytes, offset, length);
187     clone.position = this.position;
188     return clone;
189   }
190 
191   @Override
192   public PositionedByteRange shallowCopySubRange(int innerOffset, int copyLength) {
193     SimplePositionedByteRange clone =
194         new SimplePositionedByteRange(bytes, offset + innerOffset, copyLength);
195     clone.position = this.position;
196     return clone;
197   }
198 
199   @Override
200   public PositionedByteRange setLimit(int limit) {
201     throw new ReadOnlyByteRangeException();
202   }
203   
204   @Override
205   public PositionedByteRange unset() {
206     throw new ReadOnlyByteRangeException();
207   }
208 
209 }