limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Google Inc. |
| 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 | |
limpbizkit@gmail.com | 9a227be | 2010-07-03 15:51:31 +0000 | [diff] [blame] | 17 | package com.google.inject.internal.util; |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 18 | |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 19 | import java.util.Collections; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.IdentityHashMap; |
| 22 | import java.util.LinkedHashMap; |
| 23 | import java.util.Map; |
| 24 | import java.util.Map.Entry; |
| 25 | import java.util.TreeMap; |
| 26 | |
| 27 | /** |
| 28 | * Static utility methods pertaining to {@link Map} instances. Also see this |
| 29 | * class's counterparts {@link Lists} and {@link Sets}. |
| 30 | * |
| 31 | * @author Kevin Bourrillion |
| 32 | * @author Mike Bostock |
| 33 | * @author Isaac Shum |
| 34 | */ |
| 35 | public final class Maps { |
| 36 | private Maps() {} |
| 37 | |
| 38 | /** |
| 39 | * Creates a {@code HashMap} instance. |
| 40 | * |
| 41 | * <p><b>Note:</b> if you don't actually need the resulting map to be mutable, |
| 42 | * use {@link Collections#emptyMap} instead. |
| 43 | * |
| 44 | * @return a new, empty {@code HashMap} |
| 45 | */ |
| 46 | public static <K, V> HashMap<K, V> newHashMap() { |
| 47 | return new HashMap<K, V>(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Creates an insertion-ordered {@code LinkedHashMap} instance. |
| 52 | * |
| 53 | * @return a new, empty {@code LinkedHashMap} |
| 54 | */ |
| 55 | public static <K, V> LinkedHashMap<K, V> newLinkedHashMap() { |
| 56 | return new LinkedHashMap<K, V>(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Creates a {@code TreeMap} instance using the natural ordering of its |
| 61 | * elements. |
| 62 | * |
| 63 | * @return a new, empty {@code TreeMap} |
| 64 | */ |
| 65 | @SuppressWarnings("unchecked") // allow ungenerified Comparable types |
| 66 | public static <K extends Comparable, V> TreeMap<K, V> newTreeMap() { |
| 67 | return new TreeMap<K, V>(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Creates an {@code IdentityHashMap} instance. |
| 72 | * |
| 73 | * @return a new, empty {@code IdentityHashMap} |
| 74 | */ |
| 75 | public static <K, V> IdentityHashMap<K, V> newIdentityHashMap() { |
| 76 | return new IdentityHashMap<K, V>(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns an immutable map entry with the specified key and value. The {@link |
| 81 | * Entry#setValue} operation throws an {@link UnsupportedOperationException}. |
| 82 | * |
| 83 | * <p>The returned entry is serializable. |
| 84 | * |
| 85 | * @param key the key to be associated with the returned entry |
| 86 | * @param value the value to be associated with the returned entry |
| 87 | */ |
| 88 | public static <K, V> Entry<K, V> immutableEntry( |
| 89 | @Nullable final K key, @Nullable final V value) { |
| 90 | return new ImmutableEntry<K, V>(key, value); |
| 91 | } |
| 92 | } |