1021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov/* 2021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Copyright (C) 2011 The Android Open Source Project 3021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * 4021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Licensed under the Apache License, Version 2.0 (the "License"); 5021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * you may not use this file except in compliance with the License. 6021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * You may obtain a copy of the License at 7021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * 8021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * http://www.apache.org/licenses/LICENSE-2.0 9021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * 10021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Unless required by applicable law or agreed to in writing, software 11021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * distributed under the License is distributed on an "AS IS" BASIS, 12021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * See the License for the specific language governing permissions and 14021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * limitations under the License. 15021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */ 16021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 17021078554b902179442a345a9d080a165c3b5139Svetoslav Ganovpackage android.util; 18021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 19021078554b902179442a345a9d080a165c3b5139Svetoslav Ganovimport com.android.internal.util.ArrayUtils; 20776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinskiimport com.android.internal.util.GrowingArrayUtils; 21776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski 22776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinskiimport libcore.util.EmptyArray; 23021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 24021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov/** 25021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * SparseLongArrays map integers to longs. Unlike a normal array of longs, 26b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * there can be gaps in the indices. It is intended to be more memory efficient 27b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * than using a HashMap to map Integers to Longs, both because it avoids 28b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * auto-boxing keys and values and its data structure doesn't rely on an extra entry object 29b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * for each mapping. 30b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * 31b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * <p>Note that this container keeps its mappings in an array data structure, 32b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * using a binary search to find keys. The implementation is not intended to be appropriate for 33b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * data structures 34b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * that may contain large numbers of items. It is generally slower than a traditional 35b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * HashMap, since lookups require a binary search and adds and removes require inserting 36b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * and deleting entries in the array. For containers holding up to hundreds of items, 37b993f41eb2f165425dfdf0f93ea0b1e354eca837Dianne Hackborn * the performance difference is not significant, less than 50%.</p> 385771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * 395771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * <p>It is possible to iterate over the items in this container using 405771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * {@link #keyAt(int)} and {@link #valueAt(int)}. Iterating over the keys using 415771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * <code>keyAt(int)</code> with ascending values of the index will return the 425771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * keys in ascending order, or the values corresponding to the keys in ascending 43ebb47950f21d3c41955a591000dfb1f195e746feNewton Allen * order in the case of <code>valueAt(int)</code>.</p> 44021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */ 45021078554b902179442a345a9d080a165c3b5139Svetoslav Ganovpublic class SparseLongArray implements Cloneable { 46021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov private int[] mKeys; 47021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov private long[] mValues; 48021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov private int mSize; 49021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 50021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov /** 51021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Creates a new SparseLongArray containing no mappings. 52021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */ 53021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov public SparseLongArray() { 54021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov this(10); 55021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 56021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 57021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov /** 58021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Creates a new SparseLongArray containing no mappings that will not 59021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * require any additional memory allocation to store the specified 60f4bf0ae2a7c2d9d92c5c8abdb82baa53b4c9ccdaDianne Hackborn * number of mappings. If you supply an initial capacity of 0, the 61f4bf0ae2a7c2d9d92c5c8abdb82baa53b4c9ccdaDianne Hackborn * sparse array will be initialized with a light-weight representation 62f4bf0ae2a7c2d9d92c5c8abdb82baa53b4c9ccdaDianne Hackborn * not requiring any additional array allocations. 63021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */ 64021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov public SparseLongArray(int initialCapacity) { 65f4bf0ae2a7c2d9d92c5c8abdb82baa53b4c9ccdaDianne Hackborn if (initialCapacity == 0) { 66776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski mKeys = EmptyArray.INT; 67776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski mValues = EmptyArray.LONG; 68f4bf0ae2a7c2d9d92c5c8abdb82baa53b4c9ccdaDianne Hackborn } else { 69776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski mValues = ArrayUtils.newUnpaddedLongArray(initialCapacity); 70776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski mKeys = new int[mValues.length]; 71f4bf0ae2a7c2d9d92c5c8abdb82baa53b4c9ccdaDianne Hackborn } 72021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov mSize = 0; 73021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 74021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 75021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov @Override 76021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov public SparseLongArray clone() { 77021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov SparseLongArray clone = null; 78021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov try { 79021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov clone = (SparseLongArray) super.clone(); 80021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov clone.mKeys = mKeys.clone(); 81021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov clone.mValues = mValues.clone(); 82021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } catch (CloneNotSupportedException cnse) { 83021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov /* ignore */ 84021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 85021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov return clone; 86021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 87021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 88021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov /** 89021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Gets the long mapped from the specified key, or <code>0</code> 90021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * if no such mapping has been made. 91021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */ 92021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov public long get(int key) { 93021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov return get(key, 0); 94021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 95021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 96021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov /** 97021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Gets the long mapped from the specified key, or the specified value 98021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * if no such mapping has been made. 99021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */ 100021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov public long get(int key, long valueIfKeyNotFound) { 1013e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn int i = ContainerHelpers.binarySearch(mKeys, mSize, key); 102021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 103021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov if (i < 0) { 104021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov return valueIfKeyNotFound; 105021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } else { 106021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov return mValues[i]; 107021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 108021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 109021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 110021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov /** 111021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Removes the mapping from the specified key, if there was any. 112021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */ 113021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov public void delete(int key) { 1143e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn int i = ContainerHelpers.binarySearch(mKeys, mSize, key); 115021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 116021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov if (i >= 0) { 117021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov removeAt(i); 118021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 119021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 120021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 121021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov /** 122021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Removes the mapping at the given index. 123021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */ 124021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov public void removeAt(int index) { 125021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov System.arraycopy(mKeys, index + 1, mKeys, index, mSize - (index + 1)); 126021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov System.arraycopy(mValues, index + 1, mValues, index, mSize - (index + 1)); 127021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov mSize--; 128021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 129021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 130021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov /** 131021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Adds a mapping from the specified key to the specified value, 132021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * replacing the previous mapping from the specified key if there 133021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * was one. 134021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */ 135021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov public void put(int key, long value) { 1363e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn int i = ContainerHelpers.binarySearch(mKeys, mSize, key); 137021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 138021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov if (i >= 0) { 139021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov mValues[i] = value; 140021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } else { 141021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov i = ~i; 142021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 143776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key); 144776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski mValues = GrowingArrayUtils.insert(mValues, mSize, i, value); 145021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov mSize++; 146021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 147021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 148021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 149021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov /** 150021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Returns the number of key-value mappings that this SparseIntArray 151021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * currently stores. 152021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */ 153021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov public int size() { 154021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov return mSize; 155021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 156021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 157021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov /** 158021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Given an index in the range <code>0...size()-1</code>, returns 159021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * the key from the <code>index</code>th key-value mapping that this 160021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * SparseLongArray stores. 1615771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * 1625771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * <p>The keys corresponding to indices in ascending order are guaranteed to 1635771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * be in ascending order, e.g., <code>keyAt(0)</code> will return the 1645771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * smallest key and <code>keyAt(size()-1)</code> will return the largest 1655771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * key.</p> 166021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */ 167021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov public int keyAt(int index) { 168021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov return mKeys[index]; 169021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 170021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 171021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov /** 172021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Given an index in the range <code>0...size()-1</code>, returns 173021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * the value from the <code>index</code>th key-value mapping that this 174021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * SparseLongArray stores. 1755771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * 1765771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * <p>The values corresponding to indices in ascending order are guaranteed 1775771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * to be associated with keys in ascending order, e.g., 1785771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * <code>valueAt(0)</code> will return the value associated with the 1795771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * smallest key and <code>valueAt(size()-1)</code> will return the value 1805771302ca13c31cb85f17bc58da8f6f8227c9b85Flavio Lerda * associated with the largest key.</p> 181021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */ 182021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov public long valueAt(int index) { 183021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov return mValues[index]; 184021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 185021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 186021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov /** 187021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Returns the index for which {@link #keyAt} would return the 188021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * specified key, or a negative number if the specified 189021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * key is not mapped. 190021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */ 191021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov public int indexOfKey(int key) { 1923e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn return ContainerHelpers.binarySearch(mKeys, mSize, key); 193021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 194021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 195021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov /** 196021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Returns an index for which {@link #valueAt} would return the 197021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * specified key, or a negative number if no keys map to the 198021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * specified value. 199021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Beware that this is a linear search, unlike lookups by key, 200021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * and that multiple keys can map to the same value and this will 201021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * find only one of them. 202021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */ 203021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov public int indexOfValue(long value) { 204021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov for (int i = 0; i < mSize; i++) 205021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov if (mValues[i] == value) 206021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov return i; 207021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 208021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov return -1; 209021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 210021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 211021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov /** 212021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Removes all key-value mappings from this SparseIntArray. 213021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */ 214021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov public void clear() { 215021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov mSize = 0; 216021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 217021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 218021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov /** 219021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * Puts a key/value pair into the array, optimizing for the case where 220021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov * the key is greater than all existing keys in the array. 221021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov */ 222021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov public void append(int key, long value) { 223021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov if (mSize != 0 && key <= mKeys[mSize - 1]) { 224021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov put(key, value); 225021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov return; 226021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 227021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov 228776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski mKeys = GrowingArrayUtils.append(mKeys, mSize, key); 229776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski mValues = GrowingArrayUtils.append(mValues, mSize, value); 230776abc24cdd18610232a50b997cce3cffa74609bAdam Lesinski mSize++; 231021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov } 2323e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn 2333e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn /** 2343e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn * {@inheritDoc} 2353e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn * 2363e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn * <p>This implementation composes a string by iterating over its mappings. 2373e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn */ 2383e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn @Override 2393e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn public String toString() { 2403e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn if (size() <= 0) { 2413e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn return "{}"; 2423e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn } 2433e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn 2443e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn StringBuilder buffer = new StringBuilder(mSize * 28); 2453e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn buffer.append('{'); 2463e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn for (int i=0; i<mSize; i++) { 2473e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn if (i > 0) { 2483e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn buffer.append(", "); 2493e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn } 2503e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn int key = keyAt(i); 2513e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn buffer.append(key); 2523e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn buffer.append('='); 2533e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn long value = valueAt(i); 2543e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn buffer.append(value); 2553e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn } 2563e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn buffer.append('}'); 2573e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn return buffer.toString(); 2583e82ba1a67b0c756ab6a289985f4cfc53725b311Dianne Hackborn } 259021078554b902179442a345a9d080a165c3b5139Svetoslav Ganov} 260