Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 android.util; |
| 18 | |
| 19 | import com.android.internal.util.ArrayUtils; |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 20 | import com.android.internal.util.GrowingArrayUtils; |
| 21 | |
| 22 | import libcore.util.EmptyArray; |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 25 | * SparseArray mapping longs to Objects. Unlike a normal array of Objects, |
Dianne Hackborn | b993f41 | 2013-07-12 17:46:45 -0700 | [diff] [blame] | 26 | * there can be gaps in the indices. It is intended to be more memory efficient |
| 27 | * than using a HashMap to map Longs to Objects, both because it avoids |
| 28 | * auto-boxing keys and its data structure doesn't rely on an extra entry object |
| 29 | * for each mapping. |
| 30 | * |
| 31 | * <p>Note that this container keeps its mappings in an array data structure, |
| 32 | * using a binary search to find keys. The implementation is not intended to be appropriate for |
| 33 | * data structures |
| 34 | * that may contain large numbers of items. It is generally slower than a traditional |
| 35 | * HashMap, since lookups require a binary search and adds and removes require inserting |
| 36 | * and deleting entries in the array. For containers holding up to hundreds of items, |
| 37 | * the performance difference is not significant, less than 50%.</p> |
| 38 | * |
| 39 | * <p>To help with performance, the container includes an optimization when removing |
| 40 | * keys: instead of compacting its array immediately, it leaves the removed entry marked |
| 41 | * as deleted. The entry can then be re-used for the same key, or compacted later in |
| 42 | * a single garbage collection step of all removed entries. This garbage collection will |
| 43 | * need to be performed at any time the array needs to be grown or the the map size or |
| 44 | * entry values are retrieved.</p> |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 45 | * |
| 46 | * <p>It is possible to iterate over the items in this container using |
| 47 | * {@link #keyAt(int)} and {@link #valueAt(int)}. Iterating over the keys using |
| 48 | * <code>keyAt(int)</code> with ascending values of the index will return the |
| 49 | * keys in ascending order, or the values corresponding to the keys in ascending |
Newton Allen | ebb4795 | 2013-11-21 13:27:10 -0800 | [diff] [blame] | 50 | * order in the case of <code>valueAt(int)</code>.</p> |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 51 | */ |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 52 | public class LongSparseArray<E> implements Cloneable { |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 53 | private static final Object DELETED = new Object(); |
| 54 | private boolean mGarbage = false; |
| 55 | |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 56 | private long[] mKeys; |
| 57 | private Object[] mValues; |
| 58 | private int mSize; |
| 59 | |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 60 | /** |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 61 | * Creates a new LongSparseArray containing no mappings. |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 62 | */ |
| 63 | public LongSparseArray() { |
| 64 | this(10); |
| 65 | } |
| 66 | |
| 67 | /** |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 68 | * Creates a new LongSparseArray containing no mappings that will not |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 69 | * require any additional memory allocation to store the specified |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 70 | * number of mappings. If you supply an initial capacity of 0, the |
| 71 | * sparse array will be initialized with a light-weight representation |
| 72 | * not requiring any additional array allocations. |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 73 | */ |
| 74 | public LongSparseArray(int initialCapacity) { |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 75 | if (initialCapacity == 0) { |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 76 | mKeys = EmptyArray.LONG; |
| 77 | mValues = EmptyArray.OBJECT; |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 78 | } else { |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 79 | mKeys = ArrayUtils.newUnpaddedLongArray(initialCapacity); |
| 80 | mValues = ArrayUtils.newUnpaddedObjectArray(initialCapacity); |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 81 | } |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 82 | mSize = 0; |
| 83 | } |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 84 | |
| 85 | @Override |
| 86 | @SuppressWarnings("unchecked") |
| 87 | public LongSparseArray<E> clone() { |
| 88 | LongSparseArray<E> clone = null; |
| 89 | try { |
| 90 | clone = (LongSparseArray<E>) super.clone(); |
| 91 | clone.mKeys = mKeys.clone(); |
| 92 | clone.mValues = mValues.clone(); |
| 93 | } catch (CloneNotSupportedException cnse) { |
| 94 | /* ignore */ |
Adam Powell | 8f1bfe1 | 2010-03-05 15:13:56 -0800 | [diff] [blame] | 95 | } |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 96 | return clone; |
Adam Powell | 8f1bfe1 | 2010-03-05 15:13:56 -0800 | [diff] [blame] | 97 | } |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Gets the Object mapped from the specified key, or <code>null</code> |
| 101 | * if no such mapping has been made. |
| 102 | */ |
| 103 | public E get(long key) { |
| 104 | return get(key, null); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Gets the Object mapped from the specified key, or the specified Object |
| 109 | * if no such mapping has been made. |
| 110 | */ |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 111 | @SuppressWarnings("unchecked") |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 112 | public E get(long key, E valueIfKeyNotFound) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 113 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 114 | |
| 115 | if (i < 0 || mValues[i] == DELETED) { |
| 116 | return valueIfKeyNotFound; |
| 117 | } else { |
| 118 | return (E) mValues[i]; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Removes the mapping from the specified key, if there was any. |
| 124 | */ |
| 125 | public void delete(long key) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 126 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 127 | |
| 128 | if (i >= 0) { |
| 129 | if (mValues[i] != DELETED) { |
| 130 | mValues[i] = DELETED; |
| 131 | mGarbage = true; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Alias for {@link #delete(long)}. |
| 138 | */ |
| 139 | public void remove(long key) { |
| 140 | delete(key); |
| 141 | } |
| 142 | |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 143 | /** |
| 144 | * Removes the mapping at the specified index. |
| 145 | */ |
| 146 | public void removeAt(int index) { |
Kweku Adams | 17d453e | 2019-03-05 15:30:42 -0800 | [diff] [blame] | 147 | if (index >= mSize) { |
| 148 | // The array might be slightly bigger than mSize, in which case, indexing won't fail. |
| 149 | throw new ArrayIndexOutOfBoundsException(index); |
| 150 | } |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 151 | if (mValues[index] != DELETED) { |
| 152 | mValues[index] = DELETED; |
| 153 | mGarbage = true; |
| 154 | } |
| 155 | } |
| 156 | |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 157 | private void gc() { |
| 158 | // Log.e("SparseArray", "gc start with " + mSize); |
| 159 | |
| 160 | int n = mSize; |
| 161 | int o = 0; |
| 162 | long[] keys = mKeys; |
| 163 | Object[] values = mValues; |
| 164 | |
| 165 | for (int i = 0; i < n; i++) { |
| 166 | Object val = values[i]; |
| 167 | |
| 168 | if (val != DELETED) { |
| 169 | if (i != o) { |
| 170 | keys[o] = keys[i]; |
| 171 | values[o] = val; |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 172 | values[i] = null; |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | o++; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | mGarbage = false; |
| 180 | mSize = o; |
| 181 | |
| 182 | // Log.e("SparseArray", "gc end with " + mSize); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Adds a mapping from the specified key to the specified value, |
| 187 | * replacing the previous mapping from the specified key if there |
| 188 | * was one. |
| 189 | */ |
| 190 | public void put(long key, E value) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 191 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 192 | |
| 193 | if (i >= 0) { |
| 194 | mValues[i] = value; |
| 195 | } else { |
| 196 | i = ~i; |
| 197 | |
| 198 | if (i < mSize && mValues[i] == DELETED) { |
| 199 | mKeys[i] = key; |
| 200 | mValues[i] = value; |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | if (mGarbage && mSize >= mKeys.length) { |
| 205 | gc(); |
| 206 | |
| 207 | // Search again because indices may have changed. |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 208 | i = ~ContainerHelpers.binarySearch(mKeys, mSize, key); |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 211 | mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key); |
| 212 | mValues = GrowingArrayUtils.insert(mValues, mSize, i, value); |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 213 | mSize++; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 218 | * Returns the number of key-value mappings that this LongSparseArray |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 219 | * currently stores. |
| 220 | */ |
| 221 | public int size() { |
| 222 | if (mGarbage) { |
| 223 | gc(); |
| 224 | } |
| 225 | |
| 226 | return mSize; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Given an index in the range <code>0...size()-1</code>, returns |
| 231 | * the key from the <code>index</code>th key-value mapping that this |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 232 | * LongSparseArray stores. |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 233 | * |
| 234 | * <p>The keys corresponding to indices in ascending order are guaranteed to |
| 235 | * be in ascending order, e.g., <code>keyAt(0)</code> will return the |
| 236 | * smallest key and <code>keyAt(size()-1)</code> will return the largest |
| 237 | * key.</p> |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 238 | */ |
| 239 | public long keyAt(int index) { |
Kweku Adams | 17d453e | 2019-03-05 15:30:42 -0800 | [diff] [blame] | 240 | if (index >= mSize) { |
| 241 | // The array might be slightly bigger than mSize, in which case, indexing won't fail. |
| 242 | throw new ArrayIndexOutOfBoundsException(index); |
| 243 | } |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 244 | if (mGarbage) { |
| 245 | gc(); |
| 246 | } |
| 247 | |
| 248 | return mKeys[index]; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Given an index in the range <code>0...size()-1</code>, returns |
| 253 | * the value from the <code>index</code>th key-value mapping that this |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 254 | * LongSparseArray stores. |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 255 | * |
| 256 | * <p>The values corresponding to indices in ascending order are guaranteed |
| 257 | * to be associated with keys in ascending order, e.g., |
| 258 | * <code>valueAt(0)</code> will return the value associated with the |
| 259 | * smallest key and <code>valueAt(size()-1)</code> will return the value |
| 260 | * associated with the largest key.</p> |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 261 | */ |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 262 | @SuppressWarnings("unchecked") |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 263 | public E valueAt(int index) { |
Kweku Adams | 17d453e | 2019-03-05 15:30:42 -0800 | [diff] [blame] | 264 | if (index >= mSize) { |
| 265 | // The array might be slightly bigger than mSize, in which case, indexing won't fail. |
| 266 | throw new ArrayIndexOutOfBoundsException(index); |
| 267 | } |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 268 | if (mGarbage) { |
| 269 | gc(); |
| 270 | } |
| 271 | |
| 272 | return (E) mValues[index]; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Given an index in the range <code>0...size()-1</code>, sets a new |
| 277 | * value for the <code>index</code>th key-value mapping that this |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 278 | * LongSparseArray stores. |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 279 | */ |
| 280 | public void setValueAt(int index, E value) { |
Kweku Adams | 17d453e | 2019-03-05 15:30:42 -0800 | [diff] [blame] | 281 | if (index >= mSize) { |
| 282 | // The array might be slightly bigger than mSize, in which case, indexing won't fail. |
| 283 | throw new ArrayIndexOutOfBoundsException(index); |
| 284 | } |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 285 | if (mGarbage) { |
| 286 | gc(); |
| 287 | } |
| 288 | |
| 289 | mValues[index] = value; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Returns the index for which {@link #keyAt} would return the |
| 294 | * specified key, or a negative number if the specified |
| 295 | * key is not mapped. |
| 296 | */ |
| 297 | public int indexOfKey(long key) { |
| 298 | if (mGarbage) { |
| 299 | gc(); |
| 300 | } |
| 301 | |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 302 | return ContainerHelpers.binarySearch(mKeys, mSize, key); |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Returns an index for which {@link #valueAt} would return the |
| 307 | * specified key, or a negative number if no keys map to the |
| 308 | * specified value. |
| 309 | * Beware that this is a linear search, unlike lookups by key, |
| 310 | * and that multiple keys can map to the same value and this will |
| 311 | * find only one of them. |
| 312 | */ |
| 313 | public int indexOfValue(E value) { |
| 314 | if (mGarbage) { |
| 315 | gc(); |
| 316 | } |
| 317 | |
Tejas Khorana | fb5166d | 2016-07-18 11:18:27 -0700 | [diff] [blame] | 318 | for (int i = 0; i < mSize; i++) { |
| 319 | if (mValues[i] == value) { |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 320 | return i; |
Tejas Khorana | fb5166d | 2016-07-18 11:18:27 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | return -1; |
| 324 | } |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 325 | |
Tejas Khorana | fb5166d | 2016-07-18 11:18:27 -0700 | [diff] [blame] | 326 | /** |
| 327 | * Returns an index for which {@link #valueAt} would return the |
| 328 | * specified key, or a negative number if no keys map to the |
| 329 | * specified value. |
| 330 | * <p>Beware that this is a linear search, unlike lookups by key, |
| 331 | * and that multiple keys can map to the same value and this will |
| 332 | * find only one of them. |
| 333 | * <p>Note also that this method uses {@code equals} unlike {@code indexOfValue}. |
Suprabh Shukla | 0693ff6 | 2017-03-23 16:09:27 -0700 | [diff] [blame] | 334 | * @hide |
Tejas Khorana | fb5166d | 2016-07-18 11:18:27 -0700 | [diff] [blame] | 335 | */ |
| 336 | public int indexOfValueByValue(E value) { |
| 337 | if (mGarbage) { |
| 338 | gc(); |
| 339 | } |
| 340 | |
| 341 | for (int i = 0; i < mSize; i++) { |
| 342 | if (value == null) { |
| 343 | if (mValues[i] == null) { |
| 344 | return i; |
| 345 | } |
| 346 | } else { |
| 347 | if (value.equals(mValues[i])) { |
| 348 | return i; |
| 349 | } |
| 350 | } |
| 351 | } |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 352 | return -1; |
| 353 | } |
| 354 | |
| 355 | /** |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 356 | * Removes all key-value mappings from this LongSparseArray. |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 357 | */ |
| 358 | public void clear() { |
| 359 | int n = mSize; |
| 360 | Object[] values = mValues; |
| 361 | |
| 362 | for (int i = 0; i < n; i++) { |
| 363 | values[i] = null; |
| 364 | } |
| 365 | |
| 366 | mSize = 0; |
| 367 | mGarbage = false; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Puts a key/value pair into the array, optimizing for the case where |
| 372 | * the key is greater than all existing keys in the array. |
| 373 | */ |
| 374 | public void append(long key, E value) { |
| 375 | if (mSize != 0 && key <= mKeys[mSize - 1]) { |
| 376 | put(key, value); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | if (mGarbage && mSize >= mKeys.length) { |
| 381 | gc(); |
| 382 | } |
| 383 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 384 | mKeys = GrowingArrayUtils.append(mKeys, mSize, key); |
| 385 | mValues = GrowingArrayUtils.append(mValues, mSize, value); |
| 386 | mSize++; |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 389 | /** |
| 390 | * {@inheritDoc} |
| 391 | * |
| 392 | * <p>This implementation composes a string by iterating over its mappings. If |
| 393 | * this map contains itself as a value, the string "(this Map)" |
| 394 | * will appear in its place. |
| 395 | */ |
| 396 | @Override |
| 397 | public String toString() { |
| 398 | if (size() <= 0) { |
| 399 | return "{}"; |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 402 | StringBuilder buffer = new StringBuilder(mSize * 28); |
| 403 | buffer.append('{'); |
| 404 | for (int i=0; i<mSize; i++) { |
| 405 | if (i > 0) { |
| 406 | buffer.append(", "); |
| 407 | } |
| 408 | long key = keyAt(i); |
| 409 | buffer.append(key); |
| 410 | buffer.append('='); |
| 411 | Object value = valueAt(i); |
| 412 | if (value != this) { |
| 413 | buffer.append(value); |
| 414 | } else { |
| 415 | buffer.append("(this Map)"); |
| 416 | } |
| 417 | } |
| 418 | buffer.append('}'); |
| 419 | return buffer.toString(); |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 420 | } |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 421 | } |