jdk/src/share/classes/com/sun/jmx/mbeanserver/Util.java
author dfuchs
Tue, 29 Jul 2008 19:21:59 +0200
changeset 900 55c9c5a88bde
parent 834 dc74d4ddc28e
child 1004 5ba8217eb504
permissions -rw-r--r--
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode. Reviewed-by: emcmanus
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 287
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.jmx.mbeanserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.IdentityHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.LinkedHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.SortedMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.TreeMap;
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 287
diff changeset
    42
import java.util.WeakHashMap;
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
    43
import javax.management.MalformedObjectNameException;
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
    44
import javax.management.ObjectName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class Util {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static <K, V> Map<K, V> newMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        return new HashMap<K, V>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static <K, V> Map<K, V> newSynchronizedMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        return Collections.synchronizedMap(Util.<K, V>newMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static <K, V> IdentityHashMap<K, V> newIdentityHashMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        return new IdentityHashMap<K, V>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static <K, V> Map<K, V> newSynchronizedIdentityHashMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Map<K, V> map = newIdentityHashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return Collections.synchronizedMap(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static <K, V> SortedMap<K, V> newSortedMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return new TreeMap<K, V>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static <K, V> SortedMap<K, V> newSortedMap(Comparator<? super K> comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return new TreeMap<K, V>(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static <K, V> Map<K, V> newInsertionOrderMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return new LinkedHashMap<K, V>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static <E> Set<E> newSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return new HashSet<E>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static <E> Set<E> newSet(Collection<E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return new HashSet<E>(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static <E> List<E> newList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return new ArrayList<E>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    static <E> List<E> newList(Collection<E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return new ArrayList<E>(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
    96
    public static ObjectName newObjectName(String s) {
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
    97
        try {
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
    98
            return new ObjectName(s);
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
    99
        } catch (MalformedObjectNameException e) {
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   100
            throw new IllegalArgumentException(e);
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   101
        }
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   102
    }
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   103
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /* This method can be used by code that is deliberately violating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * allowed checked casts.  Rather than marking the whole method containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * the code with @SuppressWarnings, you can use a call to this method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * the exact place where you need to escape the constraints.  Typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * you will "import static" this method and then write either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *    X x = cast(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * or, if that doesn't work (e.g. X is a type variable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *    Util.<X>cast(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static <T> T cast(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return (T) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
}