Browse Source

Upgraded JdkVersion and CollectionFactory to JDK 1.6+, also removing deprecated methods from CollectionFactory

pull/268/head
Juergen Hoeller 12 years ago
parent
commit
e11cf5f061
  1. 124
      spring-core/src/main/java/org/springframework/core/CollectionFactory.java
  2. 46
      spring-core/src/main/java/org/springframework/core/ConcurrentMap.java
  3. 69
      spring-core/src/main/java/org/springframework/core/JdkVersion.java
  4. 74
      spring-core/src/test/java/org/springframework/core/CollectionFactoryTests.java

124
spring-core/src/main/java/org/springframework/core/CollectionFactory.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,21 +20,19 @@ import java.util.ArrayList; @@ -20,21 +20,19 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@ -67,20 +65,10 @@ public abstract class CollectionFactory { @@ -67,20 +65,10 @@ public abstract class CollectionFactory {
approximableCollectionTypes.add(List.class);
approximableCollectionTypes.add(Set.class);
approximableCollectionTypes.add(SortedSet.class);
approximableCollectionTypes.add(NavigableSet.class);
approximableMapTypes.add(Map.class);
approximableMapTypes.add(SortedMap.class);
// New Java 6 collection interfaces
ClassLoader cl = CollectionFactory.class.getClassLoader();
try {
navigableSetClass = cl.loadClass("java.util.NavigableSet");
navigableMapClass = cl.loadClass("java.util.NavigableMap");
approximableCollectionTypes.add(navigableSetClass);
approximableMapTypes.add(navigableMapClass);
}
catch (ClassNotFoundException ex) {
// not running on Java 6 or above...
}
approximableMapTypes.add(NavigableMap.class);
// Common concrete collection classes
approximableCollectionTypes.add(ArrayList.class);
@ -94,95 +82,6 @@ public abstract class CollectionFactory { @@ -94,95 +82,6 @@ public abstract class CollectionFactory {
}
/**
* Create a linked Set if possible: This implementation always
* creates a {@link java.util.LinkedHashSet}, since Spring 2.5
* requires JDK 1.4 anyway.
* @param initialCapacity the initial capacity of the Set
* @return the new Set instance
* @deprecated as of Spring 2.5, for usage on JDK 1.4 or higher
*/
@Deprecated
public static <T> Set<T> createLinkedSetIfPossible(int initialCapacity) {
return new LinkedHashSet<T>(initialCapacity);
}
/**
* Create a copy-on-write Set (allowing for synchronization-less iteration) if possible:
* This implementation always creates a {@link java.util.concurrent.CopyOnWriteArraySet},
* since Spring 3 requires JDK 1.5 anyway.
* @return the new Set instance
* @deprecated as of Spring 3.0, for usage on JDK 1.5 or higher
*/
@Deprecated
public static <T> Set<T> createCopyOnWriteSet() {
return new CopyOnWriteArraySet<T>();
}
/**
* Create a linked Map if possible: This implementation always
* creates a {@link java.util.LinkedHashMap}, since Spring 2.5
* requires JDK 1.4 anyway.
* @param initialCapacity the initial capacity of the Map
* @return the new Map instance
* @deprecated as of Spring 2.5, for usage on JDK 1.4 or higher
*/
@Deprecated
public static <K,V> Map<K,V> createLinkedMapIfPossible(int initialCapacity) {
return new LinkedHashMap<K,V>(initialCapacity);
}
/**
* Create a linked case-insensitive Map if possible: This implementation
* always returns a {@link org.springframework.util.LinkedCaseInsensitiveMap}.
* @param initialCapacity the initial capacity of the Map
* @return the new Map instance
* @deprecated as of Spring 3.0, for usage on JDK 1.5 or higher
*/
@Deprecated
public static Map createLinkedCaseInsensitiveMapIfPossible(int initialCapacity) {
return new LinkedCaseInsensitiveMap(initialCapacity);
}
/**
* Create an identity Map if possible: This implementation always
* creates a {@link java.util.IdentityHashMap}, since Spring 2.5
* requires JDK 1.4 anyway.
* @param initialCapacity the initial capacity of the Map
* @return the new Map instance
* @deprecated as of Spring 2.5, for usage on JDK 1.4 or higher
*/
@Deprecated
public static Map createIdentityMapIfPossible(int initialCapacity) {
return new IdentityHashMap(initialCapacity);
}
/**
* Create a concurrent Map if possible: This implementation always
* creates a {@link java.util.concurrent.ConcurrentHashMap}, since Spring 3.0
* requires JDK 1.5 anyway.
* @param initialCapacity the initial capacity of the Map
* @return the new Map instance
* @deprecated as of Spring 3.0, for usage on JDK 1.5 or higher
*/
@Deprecated
public static Map createConcurrentMapIfPossible(int initialCapacity) {
return new ConcurrentHashMap(initialCapacity);
}
/**
* Create a concurrent Map with a dedicated {@link ConcurrentMap} interface:
* This implementation always creates a {@link java.util.concurrent.ConcurrentHashMap},
* since Spring 3.0 requires JDK 1.5 anyway.
* @param initialCapacity the initial capacity of the Map
* @return the new ConcurrentMap instance
* @deprecated as of Spring 3.0, for usage on JDK 1.5 or higher
*/
@Deprecated
public static ConcurrentMap createConcurrentMap(int initialCapacity) {
return new JdkConcurrentHashMap(initialCapacity);
}
/**
* Determine whether the given collection type is an approximable type,
* i.e. a type that {@link #createApproximateCollection} can approximate.
@ -329,17 +228,4 @@ public abstract class CollectionFactory { @@ -329,17 +228,4 @@ public abstract class CollectionFactory {
}
}
/**
* ConcurrentMap adapter for the JDK ConcurrentHashMap class.
*/
@Deprecated
@SuppressWarnings("serial")
private static class JdkConcurrentHashMap extends ConcurrentHashMap implements ConcurrentMap {
private JdkConcurrentHashMap(int initialCapacity) {
super(initialCapacity);
}
}
}

46
spring-core/src/main/java/org/springframework/core/ConcurrentMap.java

@ -1,46 +0,0 @@ @@ -1,46 +0,0 @@
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core;
import java.util.Map;
/**
* Common interface for a concurrent Map, as exposed by
* {@link CollectionFactory#createConcurrentMap}. Mirrors
* {@link java.util.concurrent.ConcurrentMap}, allowing to be backed by a
* JDK ConcurrentHashMap as well as a backport-concurrent ConcurrentHashMap.
*
* <p>Check out the {@link java.util.concurrent.ConcurrentMap ConcurrentMap javadoc}
* for details on the interface's methods.
*
* @author Juergen Hoeller
* @since 2.5
* @deprecated as of Spring 3.0, since standard {@link java.util.concurrent.ConcurrentMap}
* is available on Java 5+ anyway
*/
@Deprecated
public interface ConcurrentMap extends Map {
Object putIfAbsent(Object key, Object value);
boolean remove(Object key, Object value);
boolean replace(Object key, Object oldValue, Object newValue);
Object replace(Object key, Object value);
}

69
spring-core/src/main/java/org/springframework/core/JdkVersion.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -21,7 +21,7 @@ package org.springframework.core; @@ -21,7 +21,7 @@ package org.springframework.core;
* that Spring is operating on, to allow for automatically
* adapting to the present platform's capabilities.
*
* <p>Note that Spring requires JVM 1.5 or higher, as of Spring 3.0.
* <p>Note that Spring requires JVM 1.6 or higher, as of Spring 4.0.
*
* @author Rod Johnson
* @author Juergen Hoeller
@ -54,6 +54,11 @@ public abstract class JdkVersion { @@ -54,6 +54,11 @@ public abstract class JdkVersion {
*/
public static final int JAVA_17 = 4;
/**
* Constant identifying the 1.8 JVM (Java 8).
*/
public static final int JAVA_18 = 5;
private static final String javaVersion;
@ -62,15 +67,15 @@ public abstract class JdkVersion { @@ -62,15 +67,15 @@ public abstract class JdkVersion {
static {
javaVersion = System.getProperty("java.version");
// version String should look like "1.4.2_10"
if (javaVersion.contains("1.7.")) {
majorJavaVersion = JAVA_17;
if (javaVersion.contains("1.8.")) {
majorJavaVersion = JAVA_18;
}
else if (javaVersion.contains("1.6.")) {
majorJavaVersion = JAVA_16;
else if (javaVersion.contains("1.7.")) {
majorJavaVersion = JAVA_17;
}
else {
// else leave 1.5 as default (it's either 1.5 or unknown)
majorJavaVersion = JAVA_15;
// else leave 1.6 as default (it's either 1.6 or unknown)
majorJavaVersion = JAVA_16;
}
}
@ -87,7 +92,7 @@ public abstract class JdkVersion { @@ -87,7 +92,7 @@ public abstract class JdkVersion {
/**
* Get the major version code. This means we can do things like
* {@code if (getMajorJavaVersion() < JAVA_14)}.
* {@code if (getMajorJavaVersion() >= JAVA_17)}.
* @return a code comparable to the JAVA_XX codes in this class
* @see #JAVA_13
* @see #JAVA_14
@ -99,50 +104,4 @@ public abstract class JdkVersion { @@ -99,50 +104,4 @@ public abstract class JdkVersion {
return majorJavaVersion;
}
/**
* Convenience method to determine if the current JVM is at least Java 1.4.
* @return {@code true} if the current JVM is at least Java 1.4
* @deprecated as of Spring 3.0 which requires Java 1.5+
* @see #getMajorJavaVersion()
* @see #JAVA_14
* @see #JAVA_15
* @see #JAVA_16
* @see #JAVA_17
*/
@Deprecated
public static boolean isAtLeastJava14() {
return true;
}
/**
* Convenience method to determine if the current JVM is at least
* Java 1.5 (Java 5).
* @return {@code true} if the current JVM is at least Java 1.5
* @deprecated as of Spring 3.0 which requires Java 1.5+
* @see #getMajorJavaVersion()
* @see #JAVA_15
* @see #JAVA_16
* @see #JAVA_17
*/
@Deprecated
public static boolean isAtLeastJava15() {
return true;
}
/**
* Convenience method to determine if the current JVM is at least
* Java 1.6 (Java 6).
* @return {@code true} if the current JVM is at least Java 1.6
* @deprecated as of Spring 3.0, in favor of reflective checks for
* the specific Java 1.6 classes of interest
* @see #getMajorJavaVersion()
* @see #JAVA_16
* @see #JAVA_17
*/
@Deprecated
public static boolean isAtLeastJava16() {
return (majorJavaVersion >= JAVA_16);
}
}

74
spring-core/src/test/java/org/springframework/core/CollectionFactoryTests.java

@ -1,74 +0,0 @@ @@ -1,74 +0,0 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import junit.framework.TestCase;
import org.springframework.util.MultiValueMap;
/**
* @author Darren Davison
* @author Juergen Hoeller
* @author Dave Syer
*/
public class CollectionFactoryTests extends TestCase {
@SuppressWarnings("deprecation")
public void testLinkedSet() {
Set set = CollectionFactory.createLinkedSetIfPossible(16);
assertTrue(set instanceof LinkedHashSet);
}
@SuppressWarnings("deprecation")
public void testLinkedMap() {
Map map = CollectionFactory.createLinkedMapIfPossible(16);
assertTrue(map instanceof LinkedHashMap);
}
@SuppressWarnings("deprecation")
public void testIdentityMap() {
Map map = CollectionFactory.createIdentityMapIfPossible(16);
assertTrue(map instanceof IdentityHashMap);
}
@SuppressWarnings("deprecation")
public void testConcurrentMap() {
Map map = CollectionFactory.createConcurrentMapIfPossible(16);
assertTrue(map.getClass().getName().endsWith("ConcurrentHashMap"));
}
public void testMultiValueMap() {
Map map = CollectionFactory.createMap(MultiValueMap.class, 16);
assertTrue(map.getClass().getName().endsWith("MultiValueMap"));
}
@SuppressWarnings("deprecation")
public void testConcurrentMapWithExplicitInterface() {
ConcurrentMap map = CollectionFactory.createConcurrentMap(16);
assertTrue(map.getClass().getSuperclass().getName().endsWith("ConcurrentHashMap"));
map.putIfAbsent("key", "value1");
map.putIfAbsent("key", "value2");
assertEquals("value1", map.get("key"));
}
}
Loading…
Cancel
Save