2
|
1 |
/*
|
715
|
2 |
* Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
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 |
|
|
26 |
package com.sun.jmx.mbeanserver;
|
|
27 |
|
|
28 |
import java.util.ArrayList;
|
|
29 |
import java.util.Collection;
|
|
30 |
import java.util.Collections;
|
|
31 |
import java.util.Comparator;
|
|
32 |
import java.util.HashMap;
|
|
33 |
import java.util.HashSet;
|
|
34 |
import java.util.IdentityHashMap;
|
|
35 |
import java.util.LinkedHashMap;
|
|
36 |
import java.util.List;
|
|
37 |
import java.util.Map;
|
|
38 |
import java.util.Set;
|
|
39 |
import java.util.SortedMap;
|
|
40 |
import java.util.TreeMap;
|
287
|
41 |
import javax.management.MalformedObjectNameException;
|
|
42 |
import javax.management.ObjectName;
|
2
|
43 |
|
|
44 |
public class Util {
|
|
45 |
static <K, V> Map<K, V> newMap() {
|
|
46 |
return new HashMap<K, V>();
|
|
47 |
}
|
|
48 |
|
|
49 |
static <K, V> Map<K, V> newSynchronizedMap() {
|
|
50 |
return Collections.synchronizedMap(Util.<K, V>newMap());
|
|
51 |
}
|
|
52 |
|
|
53 |
static <K, V> IdentityHashMap<K, V> newIdentityHashMap() {
|
|
54 |
return new IdentityHashMap<K, V>();
|
|
55 |
}
|
|
56 |
|
|
57 |
static <K, V> Map<K, V> newSynchronizedIdentityHashMap() {
|
|
58 |
Map<K, V> map = newIdentityHashMap();
|
|
59 |
return Collections.synchronizedMap(map);
|
|
60 |
}
|
|
61 |
|
|
62 |
static <K, V> SortedMap<K, V> newSortedMap() {
|
|
63 |
return new TreeMap<K, V>();
|
|
64 |
}
|
|
65 |
|
|
66 |
static <K, V> SortedMap<K, V> newSortedMap(Comparator<? super K> comp) {
|
|
67 |
return new TreeMap<K, V>(comp);
|
|
68 |
}
|
|
69 |
|
|
70 |
static <K, V> Map<K, V> newInsertionOrderMap() {
|
|
71 |
return new LinkedHashMap<K, V>();
|
|
72 |
}
|
|
73 |
|
|
74 |
static <E> Set<E> newSet() {
|
|
75 |
return new HashSet<E>();
|
|
76 |
}
|
|
77 |
|
|
78 |
static <E> Set<E> newSet(Collection<E> c) {
|
|
79 |
return new HashSet<E>(c);
|
|
80 |
}
|
|
81 |
|
|
82 |
static <E> List<E> newList() {
|
|
83 |
return new ArrayList<E>();
|
|
84 |
}
|
|
85 |
|
|
86 |
static <E> List<E> newList(Collection<E> c) {
|
|
87 |
return new ArrayList<E>(c);
|
|
88 |
}
|
|
89 |
|
287
|
90 |
public static ObjectName newObjectName(String s) {
|
|
91 |
try {
|
|
92 |
return new ObjectName(s);
|
|
93 |
} catch (MalformedObjectNameException e) {
|
|
94 |
throw new IllegalArgumentException(e);
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
2
|
98 |
/* This method can be used by code that is deliberately violating the
|
|
99 |
* allowed checked casts. Rather than marking the whole method containing
|
|
100 |
* the code with @SuppressWarnings, you can use a call to this method for
|
|
101 |
* the exact place where you need to escape the constraints. Typically
|
|
102 |
* you will "import static" this method and then write either
|
|
103 |
* X x = cast(y);
|
|
104 |
* or, if that doesn't work (e.g. X is a type variable)
|
|
105 |
* Util.<X>cast(y);
|
|
106 |
*/
|
|
107 |
@SuppressWarnings("unchecked")
|
|
108 |
public static <T> T cast(Object x) {
|
|
109 |
return (T) x;
|
|
110 |
}
|
|
111 |
}
|