Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup old JDK collection compatibility classes #3979

Merged
merged 1 commit into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,20 @@ public static JdkVersion parseVersion(String versionString) {
}
final String[] parts = versionString.split("\\.|_");
if (parts.length == 3) {
// e.g. "1.8.0" or "9.0.1"
return new JdkVersion(Integer.parseInt(parts[0]),
Integer.parseInt(parts[1]),
Integer.parseInt(parts[2]),
0);
} else {
} else if (parts.length == 4) {
// e.g. "1.8.0_141"
return new JdkVersion(Integer.parseInt(parts[0]),
Integer.parseInt(parts[1]),
Integer.parseInt(parts[2]),
Integer.parseInt(parts[3]));
} else {
// e.g "11"
return new JdkVersion(Integer.parseInt(parts[0]), 0, 0, 0);
}
} catch (final Exception e) {
return UNKNOWN_VERSION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -70,7 +71,7 @@ public final class ByteBufferInputStream extends NonBlockingInputStream {
* to be read.
*/
public ByteBufferInputStream() {
this.buffers = DataStructures.createLinkedTransferQueue();
this.buffers = new LinkedTransferQueue<>();
this.current = null;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@

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;
import java.util.concurrent.LinkedTransferQueue;

/**
* Utility class, which tries to pickup the best collection implementation depending
Expand All @@ -39,33 +32,6 @@
*/
public final class DataStructures {

private static final Class<?> LTQ_CLASS;

static {
String className = null;

Class<?> c;
try {
final JdkVersion jdkVersion = JdkVersion.getJdkVersion();
final 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 (final 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.
*/
Expand All @@ -76,125 +42,92 @@ private static int ceilingNextPowerOfTwo(final int x) {
return 1 << (Integer.SIZE - Integer.numberOfLeadingZeros(x - 1));
}

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

/**
* Create an instance of a {@link BlockingQueue} that is based on
* {@code LinkedTransferQueue} implementation from JDK 7.
* {@code LinkedTransferQueue} implementation, available in JDK 7 and above.
* <p>
* When running on JDK 7 or higher, JDK {@code LinkedTransferQueue} implementation is used,
* on JDK 6 an internal Jersey implementation class is used.
* Originally, the method was used to provide backwards compatibility for JDK versions 6 and below.
* As those versions are now unsupported, callers should instantiate an {@link LinkedTransferQueue}
* directly instead of using this method.
* </p>
*
* @param <E> 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")
@Deprecated
public static <E> BlockingQueue<E> createLinkedTransferQueue() {
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<BlockingQueue<E>>() {
@Override
public BlockingQueue<E> run() throws Exception {
return (BlockingQueue<E>) LTQ_CLASS.newInstance();
}
});
} catch (final PrivilegedActionException ex) {
final Throwable cause = ex.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else {
throw new RuntimeException(cause);
}
}
return new LinkedTransferQueue<>();
}

/**
* Creates a new, empty map with a default initial capacity (16),
* load factor (0.75) and concurrencyLevel (16).
* <p>
* On Oracle JDK, the factory method will return an instance of
* The method was originally used to provide the
* <a href="http://gee.cs.oswego.edu/dl/jsr166/dist/jsr166edocs/jsr166e/ConcurrentHashMapV8.html">
* {@code ConcurrentHashMapV8}</a>
* 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.
* {@code ConcurrentHashMapV8}</a>, available in JDK 8 and above, for JDK 7 or earlier.
* As those versions are now unsupported, callers should instantiate an {@link ConcurrentHashMap}
* directly instead of using this method.
* </p>
*
* @return the map.
*/
@Deprecated
public static <K, V> ConcurrentMap<K, V> createConcurrentMap() {
return JdkVersion.getJdkVersion().isUnsafeSupported()
? new ConcurrentHashMapV8<K, V>()
: new ConcurrentHashMap<K, V>();
return new ConcurrentHashMap<>();
}

/**
* Creates a new map with the same mappings as the given map.
* <p>
* On Oracle JDK, the factory method will return an instance of
* The method was originally used to provide the
* <a href="http://gee.cs.oswego.edu/dl/jsr166/dist/jsr166edocs/jsr166e/ConcurrentHashMapV8.html">
* {@code ConcurrentHashMapV8}</a>
* 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.
* {@code ConcurrentHashMapV8}</a>, available in JDK 8 and above, for JDK 7 or earlier.
* As those versions are now unsupported, callers should instantiate an {@link ConcurrentHashMap}
* directly instead of using this method.
* </p>
*
* @param map the map.
*/
@Deprecated
public static <K, V> ConcurrentMap<K, V> createConcurrentMap(
final Map<? extends K, ? extends V> map) {
return JdkVersion.getJdkVersion().isUnsafeSupported()
? new ConcurrentHashMapV8<K, V>(map)
: new ConcurrentHashMap<K, V>(map);
return 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.
* <p>
* On Oracle JDK, the factory method will return an instance of
* The method was originally used to provide the
* <a href="http://gee.cs.oswego.edu/dl/jsr166/dist/jsr166edocs/jsr166e/ConcurrentHashMapV8.html">
* {@code ConcurrentHashMapV8}</a>
* 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.
* {@code ConcurrentHashMapV8}</a>, available in JDK 8 and above, for JDK 7 or earlier.
* As those versions are now unsupported, callers should instantiate an {@link ConcurrentHashMap}
* directly instead of using this method.
* </p>
*
* @param initialCapacity The implementation performs internal
* sizing to accommodate this many elements.
* @throws IllegalArgumentException if the initial capacity of
* elements is negative.
*/
@Deprecated
public static <K, V> ConcurrentMap<K, V> createConcurrentMap(
final int initialCapacity) {
return JdkVersion.getJdkVersion().isUnsafeSupported()
? new ConcurrentHashMapV8<K, V>(initialCapacity)
: new ConcurrentHashMap<K, V>(initialCapacity);
return 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}).
* <p>
* On Oracle JDK, the factory method will return an instance of
* The method was originally used to provide the
* <a href="http://gee.cs.oswego.edu/dl/jsr166/dist/jsr166edocs/jsr166e/ConcurrentHashMapV8.html">
* {@code ConcurrentHashMapV8}</a>
* 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.
* {@code ConcurrentHashMapV8}</a>, available in JDK 8 and above, for JDK 7 or earlier.
* As those versions are now unsupported, callers should instantiate an {@link ConcurrentHashMap}
* directly instead of using this method.
* </p>
*
* @param initialCapacity the initial capacity. The implementation
Expand All @@ -209,11 +142,10 @@ public static <K, V> ConcurrentMap<K, V> createConcurrentMap(
* negative or the load factor or concurrencyLevel are
* not positive.
*/
@Deprecated
public static <K, V> ConcurrentMap<K, V> createConcurrentMap(
final int initialCapacity, final float loadFactor,
final int concurrencyLevel) {
return JdkVersion.getJdkVersion().isUnsafeSupported()
? new ConcurrentHashMapV8<K, V>(initialCapacity, loadFactor, concurrencyLevel)
: new ConcurrentHashMap<K, V>(initialCapacity, loadFactor, concurrencyLevel);
return new ConcurrentHashMap<>(initialCapacity, loadFactor, concurrencyLevel);
}
}
Loading