blob: 712b4e378a88255d604f0bb1254d6daaa22f3259 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package com.sun.jmx.mbeanserver;
27
28import java.util.ArrayList;
29import java.util.Collection;
30import java.util.Collections;
31import java.util.Comparator;
32import java.util.HashMap;
33import java.util.HashSet;
34import java.util.IdentityHashMap;
35import java.util.LinkedHashMap;
36import java.util.List;
37import java.util.Map;
38import java.util.Set;
39import java.util.SortedMap;
40import java.util.TreeMap;
41
42public class Util {
43 static <K, V> Map<K, V> newMap() {
44 return new HashMap<K, V>();
45 }
46
47 static <K, V> Map<K, V> newSynchronizedMap() {
48 return Collections.synchronizedMap(Util.<K, V>newMap());
49 }
50
51 static <K, V> IdentityHashMap<K, V> newIdentityHashMap() {
52 return new IdentityHashMap<K, V>();
53 }
54
55 static <K, V> Map<K, V> newSynchronizedIdentityHashMap() {
56 Map<K, V> map = newIdentityHashMap();
57 return Collections.synchronizedMap(map);
58 }
59
60 static <K, V> SortedMap<K, V> newSortedMap() {
61 return new TreeMap<K, V>();
62 }
63
64 static <K, V> SortedMap<K, V> newSortedMap(Comparator<? super K> comp) {
65 return new TreeMap<K, V>(comp);
66 }
67
68 static <K, V> Map<K, V> newInsertionOrderMap() {
69 return new LinkedHashMap<K, V>();
70 }
71
72 static <E> Set<E> newSet() {
73 return new HashSet<E>();
74 }
75
76 static <E> Set<E> newSet(Collection<E> c) {
77 return new HashSet<E>(c);
78 }
79
80 static <E> List<E> newList() {
81 return new ArrayList<E>();
82 }
83
84 static <E> List<E> newList(Collection<E> c) {
85 return new ArrayList<E>(c);
86 }
87
88 /* This method can be used by code that is deliberately violating the
89 * allowed checked casts. Rather than marking the whole method containing
90 * the code with @SuppressWarnings, you can use a call to this method for
91 * the exact place where you need to escape the constraints. Typically
92 * you will "import static" this method and then write either
93 * X x = cast(y);
94 * or, if that doesn't work (e.g. X is a type variable)
95 * Util.<X>cast(y);
96 */
97 @SuppressWarnings("unchecked")
98 public static <T> T cast(Object x) {
99 return (T) x;
100 }
101}