• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.res.android;
2 
3 // transliterated from
4 // https://android.googlesource.com/platform/frameworks/base/+/android-9.0.0_r12/libs/androidfw/include/androidfw/ByteBucketArray.h
5 /**
6  * Stores a sparsely populated array. Has a fixed size of 256 (number of entries that a byte can
7  * represent).
8  */
9 @SuppressWarnings("AndroidJdkLibsChecker")
10 public abstract class ByteBucketArray<T> {
ByteBucketArray(T mDefault)11   public ByteBucketArray(T mDefault) {
12     this.mDefault = mDefault;
13   }
14 
size()15   final int size() {
16     return NUM_BUCKETS * BUCKET_SIZE;
17   }
18 
19   //  inline const T& get(size_t index) const {
20   //    return (*this)[index];
21   //  }
22 
get(int index)23   final T get(int index) {
24     if (index >= size()) {
25       return mDefault;
26     }
27 
28     //    byte bucketIndex = static_cast<byte>(index) >> 4;
29     byte bucketIndex = (byte) (Byte.toUnsignedInt((byte) index) >> 4);
30     T[] bucket = (T[]) mBuckets[bucketIndex];
31     if (bucket == null) {
32       return mDefault;
33     }
34     T t = bucket[0x0f & ((byte) index)];
35     return t == null ? mDefault : t;
36   }
37 
editItemAt(int index)38   T editItemAt(int index) {
39     //    ALOG_ASSERT(index < size(), "ByteBucketArray.getOrCreate(index=%u) with size=%u",
40     //        (uint32_t) index, (uint32_t) size());
41 
42     //    uint8_t bucketIndex = static_cast<uint8_t>(index) >> 4;
43     byte bucketIndex = (byte) (Byte.toUnsignedInt((byte) index) >> 4);
44     Object[] bucket = mBuckets[bucketIndex];
45     if (bucket == null) {
46       bucket = mBuckets[bucketIndex] = new Object[BUCKET_SIZE];
47     }
48     //    return bucket[0x0f & static_cast<uint8_t>(index)];
49     T t = (T) bucket[0x0f & ((byte) index)];
50     if (t == null) {
51       t = newInstance();
52       bucket[0x0f & ((byte) index)] = t;
53     }
54     return t;
55   }
56 
newInstance()57   abstract T newInstance();
58 
set(int index, final T value)59   boolean set(int index, final T value) {
60     if (index >= size()) {
61       return false;
62     }
63 
64     //    editItemAt(index) = value;
65     byte bucketIndex = (byte) (Byte.toUnsignedInt((byte) index) >> 4);
66     Object[] bucket = mBuckets[bucketIndex];
67     if (bucket == null) {
68       bucket = mBuckets[bucketIndex] = new Object[BUCKET_SIZE];
69     }
70     bucket[0x0f & ((byte) index)] = value;
71 
72     return true;
73   }
74 
75   private static final int NUM_BUCKETS = 16, BUCKET_SIZE = 16;
76 
77   Object[][] mBuckets = new Object[NUM_BUCKETS][];
78   T mDefault;
79 }
80