author | dfuchs |
Tue, 29 Jul 2008 19:21:59 +0200 | |
changeset 900 | 55c9c5a88bde |
parent 834 | dc74d4ddc28e |
child 1004 | 5ba8217eb504 |
permissions | -rw-r--r-- |
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; |
|
900
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
29 |
import java.util.Arrays; |
2 | 30 |
import java.util.Collection; |
31 |
import java.util.Collections; |
|
32 |
import java.util.Comparator; |
|
33 |
import java.util.HashMap; |
|
34 |
import java.util.HashSet; |
|
35 |
import java.util.IdentityHashMap; |
|
36 |
import java.util.LinkedHashMap; |
|
37 |
import java.util.List; |
|
38 |
import java.util.Map; |
|
39 |
import java.util.Set; |
|
40 |
import java.util.SortedMap; |
|
41 |
import java.util.TreeMap; |
|
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
287
diff
changeset
|
42 |
import java.util.WeakHashMap; |
287 | 43 |
import javax.management.MalformedObjectNameException; |
44 |
import javax.management.ObjectName; |
|
2 | 45 |
|
46 |
public class Util { |
|
47 |
static <K, V> Map<K, V> newMap() { |
|
48 |
return new HashMap<K, V>(); |
|
49 |
} |
|
50 |
||
51 |
static <K, V> Map<K, V> newSynchronizedMap() { |
|
52 |
return Collections.synchronizedMap(Util.<K, V>newMap()); |
|
53 |
} |
|
54 |
||
55 |
static <K, V> IdentityHashMap<K, V> newIdentityHashMap() { |
|
56 |
return new IdentityHashMap<K, V>(); |
|
57 |
} |
|
58 |
||
59 |
static <K, V> Map<K, V> newSynchronizedIdentityHashMap() { |
|
60 |
Map<K, V> map = newIdentityHashMap(); |
|
61 |
return Collections.synchronizedMap(map); |
|
62 |
} |
|
63 |
||
64 |
static <K, V> SortedMap<K, V> newSortedMap() { |
|
65 |
return new TreeMap<K, V>(); |
|
66 |
} |
|
67 |
||
68 |
static <K, V> SortedMap<K, V> newSortedMap(Comparator<? super K> comp) { |
|
69 |
return new TreeMap<K, V>(comp); |
|
70 |
} |
|
71 |
||
72 |
static <K, V> Map<K, V> newInsertionOrderMap() { |
|
73 |
return new LinkedHashMap<K, V>(); |
|
74 |
} |
|
75 |
||
833
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
287
diff
changeset
|
76 |
static <K, V> WeakHashMap<K, V> newWeakHashMap() { |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
287
diff
changeset
|
77 |
return new WeakHashMap<K, V>(); |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
287
diff
changeset
|
78 |
} |
bfa2bef7517c
6323980: Annotations to simplify MBean development
emcmanus
parents:
287
diff
changeset
|
79 |
|
2 | 80 |
static <E> Set<E> newSet() { |
81 |
return new HashSet<E>(); |
|
82 |
} |
|
83 |
||
84 |
static <E> Set<E> newSet(Collection<E> c) { |
|
85 |
return new HashSet<E>(c); |
|
86 |
} |
|
87 |
||
88 |
static <E> List<E> newList() { |
|
89 |
return new ArrayList<E>(); |
|
90 |
} |
|
91 |
||
92 |
static <E> List<E> newList(Collection<E> c) { |
|
93 |
return new ArrayList<E>(c); |
|
94 |
} |
|
95 |
||
287 | 96 |
public static ObjectName newObjectName(String s) { |
97 |
try { |
|
98 |
return new ObjectName(s); |
|
99 |
} catch (MalformedObjectNameException e) { |
|
100 |
throw new IllegalArgumentException(e); |
|
101 |
} |
|
102 |
} |
|
103 |
||
2 | 104 |
/* This method can be used by code that is deliberately violating the |
105 |
* allowed checked casts. Rather than marking the whole method containing |
|
106 |
* the code with @SuppressWarnings, you can use a call to this method for |
|
107 |
* the exact place where you need to escape the constraints. Typically |
|
108 |
* you will "import static" this method and then write either |
|
109 |
* X x = cast(y); |
|
110 |
* or, if that doesn't work (e.g. X is a type variable) |
|
111 |
* Util.<X>cast(y); |
|
112 |
*/ |
|
113 |
@SuppressWarnings("unchecked") |
|
114 |
public static <T> T cast(Object x) { |
|
115 |
return (T) x; |
|
116 |
} |
|
900
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
117 |
|
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
118 |
/** |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
119 |
* Computes a descriptor hashcode from its names and values. |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
120 |
* @param names the sorted array of descriptor names. |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
121 |
* @param values the array of descriptor values. |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
122 |
* @return a hash code value, as described in {@link #hashCode(Descriptor)} |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
123 |
*/ |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
124 |
public static int hashCode(String[] names, Object[] values) { |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
125 |
int hash = 0; |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
126 |
for (int i = 0; i < names.length; i++) { |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
127 |
Object v = values[i]; |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
128 |
int h; |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
129 |
if (v == null) { |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
130 |
h = 0; |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
131 |
} else if (v instanceof Object[]) { |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
132 |
h = Arrays.deepHashCode((Object[]) v); |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
133 |
} else if (v.getClass().isArray()) { |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
134 |
h = Arrays.deepHashCode(new Object[]{v}) - 31; |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
135 |
// hashcode of a list containing just v is |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
136 |
// v.hashCode() + 31, see List.hashCode() |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
137 |
} else { |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
138 |
h = v.hashCode(); |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
139 |
} |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
140 |
hash += names[i].toLowerCase().hashCode() ^ h; |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
141 |
} |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
142 |
return hash; |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
143 |
} |
55c9c5a88bde
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents:
834
diff
changeset
|
144 |
|
2 | 145 |
} |