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