0N/A/*
2362N/A * Copyright (c) 2005, 2008, Oracle and/or its affiliates. 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
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle 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 *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * 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;
1790N/Aimport javax.management.MalformedObjectNameException;
147N/Aimport javax.management.ObjectName;
0N/A
0N/Apublic class Util {
1790N/A public static ObjectName newObjectName(String string) {
1790N/A try {
1790N/A return new ObjectName(string);
1790N/A } catch (MalformedObjectNameException e) {
1790N/A throw new IllegalArgumentException(e);
1790N/A }
1790N/A }
528N/A
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
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
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
528N/A /** Match a part of a string against a shell-style pattern.
528N/A The only pattern characters recognized are <code>?</code>,
528N/A standing for any one character,
528N/A and <code>*</code>, standing for any string of
528N/A characters, including the empty string. For instance,
528N/A {@code wildmatch("sandwich","sa?d*ch",1,4,1,4)} will match
528N/A {@code "and"} against {@code "a?d"}.
528N/A
528N/A @param str the string containing the sequence to match.
528N/A @param pat a string containing a pattern to match the sub string
528N/A against.
528N/A @param stri the index in the string at which matching should begin.
528N/A @param strend the index in the string at which the matching should
528N/A end.
528N/A @param pati the index in the pattern at which matching should begin.
528N/A @param patend the index in the pattern at which the matching should
528N/A end.
528N/A
528N/A @return true if and only if the string matches the pattern.
528N/A */
528N/A /* The algorithm is a classical one. We advance pointers in
528N/A parallel through str and pat. If we encounter a star in pat,
528N/A we remember its position and continue advancing. If at any
528N/A stage we get a mismatch between str and pat, we look to see if
528N/A there is a remembered star. If not, we fail. If so, we
528N/A retreat pat to just past that star and str to the position
528N/A after the last one we tried, and we let the match advance
528N/A again.
528N/A
528N/A Even though there is only one remembered star position, the
528N/A algorithm works when there are several stars in the pattern.
528N/A When we encounter the second star, we forget the first one.
528N/A This is OK, because if we get to the second star in A*B*C
528N/A (where A etc are arbitrary strings), we have already seen AXB.
528N/A We're therefore setting up a match of *C against the remainder
528N/A of the string, which will match if that remainder looks like
528N/A YC, so the whole string looks like AXBYC.
528N/A */
528N/A private static boolean wildmatch(final String str, final String pat,
528N/A int stri, final int strend, int pati, final int patend) {
528N/A
528N/A // System.out.println("matching "+pat.substring(pati,patend)+
528N/A // " against "+str.substring(stri, strend));
528N/A int starstri; // index for backtrack if "*" attempt fails
528N/A int starpati; // index for backtrack if "*" attempt fails, +1
528N/A
528N/A starstri = starpati = -1;
528N/A
528N/A /* On each pass through this loop, we either advance pati,
528N/A or we backtrack pati and advance starstri. Since starstri
528N/A is only ever assigned from pati, the loop must terminate. */
528N/A while (true) {
528N/A if (pati < patend) {
528N/A final char patc = pat.charAt(pati);
528N/A switch (patc) {
528N/A case '?':
528N/A if (stri == strend)
528N/A break;
528N/A stri++;
528N/A pati++;
528N/A continue;
528N/A case '*':
528N/A pati++;
528N/A starpati = pati;
528N/A starstri = stri;
528N/A continue;
528N/A default:
528N/A if (stri < strend && str.charAt(stri) == patc) {
528N/A stri++;
528N/A pati++;
528N/A continue;
528N/A }
528N/A break;
528N/A }
528N/A } else if (stri == strend)
528N/A return true;
528N/A
528N/A // Mismatched, can we backtrack to a "*"?
528N/A if (starpati < 0 || starstri == strend)
528N/A return false;
528N/A
528N/A // Retry the match one position later in str
528N/A pati = starpati;
528N/A starstri++;
528N/A stri = starstri;
528N/A }
528N/A }
528N/A
528N/A /** Match a string against a shell-style pattern. The only pattern
528N/A characters recognized are <code>?</code>, standing for any one
528N/A character, and <code>*</code>, standing for any string of
528N/A characters, including the empty string.
528N/A
528N/A @param str the string to match.
528N/A @param pat the pattern to match the string against.
528N/A
528N/A @return true if and only if the string matches the pattern.
528N/A */
528N/A public static boolean wildmatch(String str, String pat) {
528N/A return wildmatch(str,pat,0,str.length(),0,pat.length());
528N/A }
0N/A}