Util.java revision 443
0N/A/*
381N/A * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/Apackage com.sun.jmx.mbeanserver;
0N/A
0N/Aimport java.util.ArrayList;
443N/Aimport java.util.Arrays;
0N/Aimport java.util.Collection;
0N/Aimport java.util.Collections;
0N/Aimport java.util.Comparator;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.HashSet;
0N/Aimport java.util.IdentityHashMap;
0N/Aimport java.util.LinkedHashMap;
0N/Aimport java.util.List;
0N/Aimport java.util.Map;
0N/Aimport java.util.Set;
0N/Aimport java.util.SortedMap;
0N/Aimport java.util.TreeMap;
406N/Aimport java.util.WeakHashMap;
147N/Aimport javax.management.MalformedObjectNameException;
147N/Aimport javax.management.ObjectName;
0N/A
0N/Apublic class Util {
0N/A static <K, V> Map<K, V> newMap() {
0N/A return new HashMap<K, V>();
0N/A }
0N/A
0N/A static <K, V> Map<K, V> newSynchronizedMap() {
0N/A return Collections.synchronizedMap(Util.<K, V>newMap());
0N/A }
0N/A
0N/A static <K, V> IdentityHashMap<K, V> newIdentityHashMap() {
0N/A return new IdentityHashMap<K, V>();
0N/A }
0N/A
0N/A static <K, V> Map<K, V> newSynchronizedIdentityHashMap() {
0N/A Map<K, V> map = newIdentityHashMap();
0N/A return Collections.synchronizedMap(map);
0N/A }
0N/A
0N/A static <K, V> SortedMap<K, V> newSortedMap() {
0N/A return new TreeMap<K, V>();
0N/A }
0N/A
0N/A static <K, V> SortedMap<K, V> newSortedMap(Comparator<? super K> comp) {
0N/A return new TreeMap<K, V>(comp);
0N/A }
0N/A
0N/A static <K, V> Map<K, V> newInsertionOrderMap() {
0N/A return new LinkedHashMap<K, V>();
0N/A }
0N/A
406N/A static <K, V> WeakHashMap<K, V> newWeakHashMap() {
406N/A return new WeakHashMap<K, V>();
406N/A }
406N/A
0N/A static <E> Set<E> newSet() {
0N/A return new HashSet<E>();
0N/A }
0N/A
0N/A static <E> Set<E> newSet(Collection<E> c) {
0N/A return new HashSet<E>(c);
0N/A }
0N/A
0N/A static <E> List<E> newList() {
0N/A return new ArrayList<E>();
0N/A }
0N/A
0N/A static <E> List<E> newList(Collection<E> c) {
0N/A return new ArrayList<E>(c);
0N/A }
0N/A
147N/A public static ObjectName newObjectName(String s) {
147N/A try {
147N/A return new ObjectName(s);
147N/A } catch (MalformedObjectNameException e) {
147N/A throw new IllegalArgumentException(e);
147N/A }
147N/A }
147N/A
0N/A /* This method can be used by code that is deliberately violating the
0N/A * allowed checked casts. Rather than marking the whole method containing
0N/A * the code with @SuppressWarnings, you can use a call to this method for
0N/A * the exact place where you need to escape the constraints. Typically
0N/A * you will "import static" this method and then write either
0N/A * X x = cast(y);
0N/A * or, if that doesn't work (e.g. X is a type variable)
0N/A * Util.<X>cast(y);
0N/A */
0N/A @SuppressWarnings("unchecked")
0N/A public static <T> T cast(Object x) {
0N/A return (T) x;
0N/A }
443N/A
443N/A /**
443N/A * Computes a descriptor hashcode from its names and values.
443N/A * @param names the sorted array of descriptor names.
443N/A * @param values the array of descriptor values.
443N/A * @return a hash code value, as described in {@link #hashCode(Descriptor)}
443N/A */
443N/A public static int hashCode(String[] names, Object[] values) {
443N/A int hash = 0;
443N/A for (int i = 0; i < names.length; i++) {
443N/A Object v = values[i];
443N/A int h;
443N/A if (v == null) {
443N/A h = 0;
443N/A } else if (v instanceof Object[]) {
443N/A h = Arrays.deepHashCode((Object[]) v);
443N/A } else if (v.getClass().isArray()) {
443N/A h = Arrays.deepHashCode(new Object[]{v}) - 31;
443N/A // hashcode of a list containing just v is
443N/A // v.hashCode() + 31, see List.hashCode()
443N/A } else {
443N/A h = v.hashCode();
443N/A }
443N/A hash += names[i].toLowerCase().hashCode() ^ h;
443N/A }
443N/A return hash;
443N/A }
443N/A
0N/A}