Returns a newly-created immutable map. @throws IllegalArgumentException if duplicate keys were added
()
| 294 | |
| 295 | |
| 296 | public ImmutableMap<K, V> build() { |
| 297 | switch (size) { |
| 298 | case 0: |
| 299 | return of(); |
| 300 | case 1: |
| 301 | return of(entries[0].getKey(), entries[0].getValue()); |
| 302 | default: |
| 303 | /* |
| 304 | * If entries is full, then this implementation may end up using the entries array |
| 305 | * directly and writing over the entry objects with non-terminal entries, but this is |
| 306 | * safe; if this Builder is used further, it will grow the entries array (so it can't |
| 307 | * affect the original array), and future build() calls will always copy any entry |
| 308 | * objects that cannot be safely reused. |
| 309 | */ |
| 310 | if (valueComparator != null) { |
| 311 | if (entriesUsed) { |
| 312 | entries = ObjectArrays.arraysCopyOf(entries, size); |
| 313 | } |
| 314 | Arrays.sort(entries, 0, size, Ordering.from(valueComparator).onResultOf(Maps.<V>valueFunction())); |
| 315 | } |
| 316 | entriesUsed = size == entries.length; |
| 317 | return RegularImmutableMap.fromEntryArray(size, entries); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /** |
no test coverage detected