Path: blob/master/src/java.management/share/classes/com/sun/jmx/mbeanserver/Util.java
41161 views
/*1* Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.jmx.mbeanserver;2627import java.util.ArrayList;28import java.util.Arrays;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;41import javax.management.MalformedObjectNameException;42import javax.management.ObjectName;4344public class Util {45public static ObjectName newObjectName(String string) {46try {47return new ObjectName(string);48} catch (MalformedObjectNameException e) {49throw new IllegalArgumentException(e);50}51}5253static <K, V> Map<K, V> newMap() {54return new HashMap<K, V>();55}5657static <K, V> Map<K, V> newSynchronizedMap() {58return Collections.synchronizedMap(Util.<K, V>newMap());59}6061static <K, V> IdentityHashMap<K, V> newIdentityHashMap() {62return new IdentityHashMap<K, V>();63}6465static <K, V> Map<K, V> newSynchronizedIdentityHashMap() {66Map<K, V> map = newIdentityHashMap();67return Collections.synchronizedMap(map);68}6970static <K, V> SortedMap<K, V> newSortedMap() {71return new TreeMap<K, V>();72}7374static <K, V> SortedMap<K, V> newSortedMap(Comparator<? super K> comp) {75return new TreeMap<K, V>(comp);76}7778static <K, V> Map<K, V> newInsertionOrderMap() {79return new LinkedHashMap<K, V>();80}8182static <E> Set<E> newSet() {83return new HashSet<E>();84}8586static <E> Set<E> newSet(Collection<E> c) {87return new HashSet<E>(c);88}8990static <E> List<E> newList() {91return new ArrayList<E>();92}9394static <E> List<E> newList(Collection<E> c) {95return new ArrayList<E>(c);96}9798/* This method can be used by code that is deliberately violating the99* allowed checked casts. Rather than marking the whole method containing100* the code with @SuppressWarnings, you can use a call to this method for101* the exact place where you need to escape the constraints. Typically102* you will "import static" this method and then write either103* 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")108public static <T> T cast(Object x) {109return (T) x;110}111112/**113* Computes a descriptor hashcode from its names and values.114* @param names the sorted array of descriptor names.115* @param values the array of descriptor values.116* @return a hash code value, as described in {@link #hashCode(Descriptor)}117*/118public static int hashCode(String[] names, Object[] values) {119int hash = 0;120for (int i = 0; i < names.length; i++) {121Object v = values[i];122int h;123if (v == null) {124h = 0;125} else if (v instanceof Object[]) {126h = Arrays.deepHashCode((Object[]) v);127} else if (v.getClass().isArray()) {128h = Arrays.deepHashCode(new Object[]{v}) - 31;129// hashcode of a list containing just v is130// v.hashCode() + 31, see List.hashCode()131} else {132h = v.hashCode();133}134hash += names[i].toLowerCase().hashCode() ^ h;135}136return hash;137}138139/** Match a part of a string against a shell-style pattern.140The only pattern characters recognized are <code>?</code>,141standing for any one character,142and <code>*</code>, standing for any string of143characters, including the empty string. For instance,144{@code wildmatch("sandwich","sa?d*ch",1,4,1,4)} will match145{@code "and"} against {@code "a?d"}.146147@param str the string containing the sequence to match.148@param pat a string containing a pattern to match the sub string149against.150@param stri the index in the string at which matching should begin.151@param strend the index in the string at which the matching should152end.153@param pati the index in the pattern at which matching should begin.154@param patend the index in the pattern at which the matching should155end.156157@return true if and only if the string matches the pattern.158*/159/* The algorithm is a classical one. We advance pointers in160parallel through str and pat. If we encounter a star in pat,161we remember its position and continue advancing. If at any162stage we get a mismatch between str and pat, we look to see if163there is a remembered star. If not, we fail. If so, we164retreat pat to just past that star and str to the position165after the last one we tried, and we let the match advance166again.167168Even though there is only one remembered star position, the169algorithm works when there are several stars in the pattern.170When we encounter the second star, we forget the first one.171This is OK, because if we get to the second star in A*B*C172(where A etc are arbitrary strings), we have already seen AXB.173We're therefore setting up a match of *C against the remainder174of the string, which will match if that remainder looks like175YC, so the whole string looks like AXBYC.176*/177private static boolean wildmatch(final String str, final String pat,178int stri, final int strend, int pati, final int patend) {179180// System.out.println("matching "+pat.substring(pati,patend)+181// " against "+str.substring(stri, strend));182int starstri; // index for backtrack if "*" attempt fails183int starpati; // index for backtrack if "*" attempt fails, +1184185starstri = starpati = -1;186187/* On each pass through this loop, we either advance pati,188or we backtrack pati and advance starstri. Since starstri189is only ever assigned from pati, the loop must terminate. */190while (true) {191if (pati < patend) {192final char patc = pat.charAt(pati);193switch (patc) {194case '?':195if (stri == strend)196break;197stri++;198pati++;199continue;200case '*':201pati++;202starpati = pati;203starstri = stri;204continue;205default:206if (stri < strend && str.charAt(stri) == patc) {207stri++;208pati++;209continue;210}211break;212}213} else if (stri == strend)214return true;215216// Mismatched, can we backtrack to a "*"?217if (starpati < 0 || starstri == strend)218return false;219220// Retry the match one position later in str221pati = starpati;222starstri++;223stri = starstri;224}225}226227/** Match a string against a shell-style pattern. The only pattern228characters recognized are <code>?</code>, standing for any one229character, and <code>*</code>, standing for any string of230characters, including the empty string.231232@param str the string to match.233@param pat the pattern to match the string against.234235@return true if and only if the string matches the pattern.236*/237public static boolean wildmatch(String str, String pat) {238return wildmatch(str,pat,0,str.length(),0,pat.length());239}240}241242243