All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.glassfish.jersey.internal.util.collection.DataStructures Maven / Gradle / Ivy

Go to download

Ehcache is an open source, standards-based cache used to boost performance, offload the database and simplify scalability. Ehcache is robust, proven and full-featured and this has made it the most widely-used Java-based cache.

There is a newer version: 2.10.9.2
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2011-2013 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * http://glassfish.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */
package org.glassfish.jersey.internal.util.collection;

import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.glassfish.jersey.internal.util.JdkVersion;

/**
 * Utility class, which tries to pickup the best collection implementation depending
 * on running environment.
 *
 * @author Gustav Trede
 * @author Marek Potociar (marek.potociar at oracle.com)
 * @since 2.3
 */
public final class DataStructures {
    private final static Class LTQ_CLASS;

    static {
        String className = null;

        Class c;
        try {
            JdkVersion jdkVersion = JdkVersion.getJdkVersion();
            JdkVersion minimumVersion = JdkVersion.parseVersion("1.7.0");

            className = (minimumVersion.compareTo(jdkVersion) <= 0)
                    ? "java.util.concurrent.LinkedTransferQueue"
                    : "org.glassfish.jersey.internal.util.collection.LinkedTransferQueue";

            c = getAndVerify(className);
            Logger.getLogger(DataStructures.class.getName()).log(Level.FINE, "USING LTQ class:{0}", c);
        } catch (Throwable t) {
            Logger.getLogger(DataStructures.class.getName()).log(Level.FINE,
                    "failed loading data structure class:" + className +
                            " fallback to embedded one", t);

            c = LinkedBlockingQueue.class; // fallback to LinkedBlockingQueue
        }

        LTQ_CLASS = c;
    }

    /**
     * Default concurrency level calculated based on the number of available CPUs.
     */
    public static final int DEFAULT_CONCURENCY_LEVEL = ceilingNextPowerOfTwo(Runtime.getRuntime().availableProcessors());

    private static int ceilingNextPowerOfTwo(int x) {
        // Hacker's Delight, Chapter 3, Harry S. Warren Jr.
        return 1 << (Integer.SIZE - Integer.numberOfLeadingZeros(x - 1));
    }

    private static Class getAndVerify(final String cn) throws Throwable {
        try {
            return AccessController.doPrivileged(new PrivilegedExceptionAction>() {
                @Override
                public Class run() throws Exception {
                    return DataStructures.class.getClassLoader().loadClass(cn).newInstance().getClass();
                }
            });
        } catch (PrivilegedActionException ex) {
            throw ex.getCause();
        }
    }

    /**
     * Create an instance of a {@link BlockingQueue} that is based on
     * {@code LinkedTransferQueue} implementation from JDK 7.
     * 

* When running on JDK 7 or higher, JDK {@code LinkedTransferQueue} implementation is used, * on JDK 6 an internal Jersey implementation class is used. *

* * @param the type of elements held in the queue. * @return new instance of a {@link BlockingQueue} that is based on {@code LinkedTransferQueue} * implementation from JDK 7. */ @SuppressWarnings("unchecked") public static BlockingQueue createLinkedTransferQueue() { try { return AccessController.doPrivileged(new PrivilegedExceptionAction>() { @Override public BlockingQueue run() throws Exception { return (BlockingQueue) LTQ_CLASS.newInstance(); } }); } catch (PrivilegedActionException ex) { final Throwable cause = ex.getCause(); if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } else { throw new RuntimeException(cause); } } } /** * Creates a new, empty map with a default initial capacity (16), * load factor (0.75) and concurrencyLevel (16). *

* On Oracle JDK, the factory method will return an instance of * * {@code ConcurrentHashMapV8} * that is supposed to be available in JDK 8 and provides better performance and memory characteristics than * {@link ConcurrentHashMap} implementation from JDK 7 or earlier. On non-Oracle JDK, * the factory instantiates the standard {@code ConcurrentHashMap} from JDK. *

* * @return the map. */ public static ConcurrentMap createConcurrentMap() { return JdkVersion.getJdkVersion().isUnsafeSupported() ? new ConcurrentHashMapV8() : new ConcurrentHashMap(); } /** * Creates a new map with the same mappings as the given map. *

* On Oracle JDK, the factory method will return an instance of * * {@code ConcurrentHashMapV8} * that is supposed to be available in JDK 8 and provides better performance and memory characteristics than * {@link ConcurrentHashMap} implementation from JDK 7 or earlier. On non-Oracle JDK, * the factory instantiates the standard {@code ConcurrentHashMap} from JDK. *

* * @param map the map. */ public static ConcurrentMap createConcurrentMap( final Map map) { return JdkVersion.getJdkVersion().isUnsafeSupported() ? new ConcurrentHashMapV8(map) : new ConcurrentHashMap(map); } /** * Creates a new, empty map with an initial table size accommodating the specified * number of elements without the need to dynamically resize. *

* On Oracle JDK, the factory method will return an instance of * * {@code ConcurrentHashMapV8} * that is supposed to be available in JDK 8 and provides better performance and memory characteristics than * {@link ConcurrentHashMap} implementation from JDK 7 or earlier. On non-Oracle JDK, * the factory instantiates the standard {@code ConcurrentHashMap} from JDK. *

* * @param initialCapacity The implementation performs internal * sizing to accommodate this many elements. * @throws IllegalArgumentException if the initial capacity of * elements is negative. */ public static ConcurrentMap createConcurrentMap( final int initialCapacity) { return JdkVersion.getJdkVersion().isUnsafeSupported() ? new ConcurrentHashMapV8(initialCapacity) : new ConcurrentHashMap(initialCapacity); } /** * Creates a new, empty map with an initial table size based on the given number of elements * ({@code initialCapacity}), table density ({@code loadFactor}), and number of concurrently * updating threads ({@code concurrencyLevel}). *

* On Oracle JDK, the factory method will return an instance of * * {@code ConcurrentHashMapV8} * that is supposed to be available in JDK 8 and provides better performance and memory characteristics than * {@link ConcurrentHashMap} implementation from JDK 7 or earlier. On non-Oracle JDK, * the factory instantiates the standard {@code ConcurrentHashMap} from JDK. *

* * @param initialCapacity the initial capacity. The implementation * performs internal sizing to accommodate this many elements, * given the specified load factor. * @param loadFactor the load factor (table density) for * establishing the initial table size. * @param concurrencyLevel the estimated number of concurrently * updating threads. The implementation may use this value as * a sizing hint. * @throws IllegalArgumentException if the initial capacity is * negative or the load factor or concurrencyLevel are * not positive. */ public static ConcurrentMap createConcurrentMap( final int initialCapacity, final float loadFactor, final int concurrencyLevel) { return JdkVersion.getJdkVersion().isUnsafeSupported() ? new ConcurrentHashMapV8(initialCapacity, loadFactor, concurrencyLevel) : new ConcurrentHashMap(initialCapacity, loadFactor, concurrencyLevel); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy