Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.gallery3d.util;
     18 
     19 // This is an array whose index ranges from min to max (inclusive).
     20 public class RangeArray<T> {
     21     private T[] mData;
     22     private int mOffset;
     23 
     24     public RangeArray(int min, int max) {
     25         mData = (T[]) new Object[max - min + 1];
     26         mOffset = min;
     27     }
     28 
     29     // Wraps around an existing array
     30     public RangeArray(T[] src, int min, int max) {
     31         if (max - min + 1 != src.length) {
     32             throw new AssertionError();
     33         }
     34         mData = src;
     35         mOffset = min;
     36     }
     37 
     38     public void put(int i, T object) {
     39         mData[i - mOffset] = object;
     40     }
     41 
     42     public T get(int i) {
     43         return mData[i - mOffset];
     44     }
     45 
     46     public int indexOf(T object) {
     47         for (int i = 0; i < mData.length; i++) {
     48             if (mData[i] == object) return i + mOffset;
     49         }
     50         return Integer.MAX_VALUE;
     51     }
     52 }
     53