You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/12/10 15:02:44 UTC
[commons-collections] branch master updated: [COLLECTIONS-739] Fix
inconsistent @throws comments in DefaultedMap (#123)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git
The following commit(s) were added to refs/heads/master by this push:
new b337feb [COLLECTIONS-739] Fix inconsistent @throws comments in DefaultedMap (#123)
b337feb is described below
commit b337febe27fe070081d570d3413a24187e86d4ef
Author: Prodigysov <pr...@gmail.com>
AuthorDate: Tue Dec 10 09:02:37 2019 -0600
[COLLECTIONS-739] Fix inconsistent @throws comments in DefaultedMap (#123)
* COLLECTIONS-739: Fix inconsistent @throws in DefaultedMap.
* COLLECTIONS-739: Change IllegalArgumentException to NullPointerException in DefaultedMap.defaultedMap, and update Javadoc correspondingly.
* COLLECTIONS-739: Add tests for factory methods in DefaultedMap.
* COLLECTIONS-739: Change null checking to use Objects.requireNonNull.
---
.../commons/collections4/map/DefaultedMap.java | 13 +++----
.../commons/collections4/map/DefaultedMapTest.java | 41 ++++++++++++++++++++++
2 files changed, 45 insertions(+), 9 deletions(-)
diff --git a/src/main/java/org/apache/commons/collections4/map/DefaultedMap.java b/src/main/java/org/apache/commons/collections4/map/DefaultedMap.java
index e489d82..6fda447 100644
--- a/src/main/java/org/apache/commons/collections4/map/DefaultedMap.java
+++ b/src/main/java/org/apache/commons/collections4/map/DefaultedMap.java
@@ -110,10 +110,8 @@ public class DefaultedMap<K, V> extends AbstractMapDecorator<K, V> implements Se
* @since 4.0
*/
public static <K, V> DefaultedMap<K, V> defaultedMap(final Map<K, V> map, final Factory<? extends V> factory) {
- if (factory == null) {
- throw new IllegalArgumentException("Factory must not be null");
- }
- return new DefaultedMap<>(map, FactoryTransformer.factoryTransformer(factory));
+ return new DefaultedMap<>(map, FactoryTransformer.factoryTransformer(
+ Objects.requireNonNull(factory, "Factory must not be null")));
}
/**
@@ -128,15 +126,12 @@ public class DefaultedMap<K, V> extends AbstractMapDecorator<K, V> implements Se
* @param map the map to decorate, must not be null
* @param transformer the transformer to use as a factory to create entries, must not be null
* @return a new defaulting map
- * @throws NullPointerException if map or factory is null
+ * @throws NullPointerException if map or transformer is null
* @since 4.0
*/
public static <K, V> Map<K, V> defaultedMap(final Map<K, V> map,
final Transformer<? super K, ? extends V> transformer) {
- if (transformer == null) {
- throw new IllegalArgumentException("Transformer must not be null");
- }
- return new DefaultedMap<>(map, transformer);
+ return new DefaultedMap<>(map, Objects.requireNonNull(transformer, "Transformer must not be null"));
}
//-----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/collections4/map/DefaultedMapTest.java b/src/test/java/org/apache/commons/collections4/map/DefaultedMapTest.java
index eeb2d92..5c0da38 100644
--- a/src/test/java/org/apache/commons/collections4/map/DefaultedMapTest.java
+++ b/src/test/java/org/apache/commons/collections4/map/DefaultedMapTest.java
@@ -23,6 +23,7 @@ import org.apache.commons.collections4.Factory;
import org.apache.commons.collections4.FactoryUtils;
import org.apache.commons.collections4.IterableMap;
import org.apache.commons.collections4.Transformer;
+import org.apache.commons.collections4.TransformerUtils;
import org.apache.commons.collections4.functors.ConstantFactory;
/**
@@ -34,6 +35,7 @@ import org.apache.commons.collections4.functors.ConstantFactory;
public class DefaultedMapTest<K, V> extends AbstractIterableMapTest<K, V> {
protected final Factory<V> nullFactory = FactoryUtils.<V>nullFactory();
+ protected final Transformer<K, V> nullTransformer = TransformerUtils.<K, V>nullTransformer();
public DefaultedMapTest(final String testName) {
super(testName);
@@ -127,6 +129,45 @@ public class DefaultedMapTest<K, V> extends AbstractIterableMapTest<K, V> {
assertEquals("NULL_OBJECT", map.get(Integer.valueOf(0)));
}
+ public void testFactoryMethods() {
+ final HashMap<K, V> base = new HashMap<>();
+
+ try {
+ DefaultedMap.defaultedMap(null, (V) "DEFAULT_VALUE");
+ fail("Expecting NullPointerException");
+ } catch (NullPointerException e) {
+ // Expected
+ }
+
+ try {
+ DefaultedMap.defaultedMap((Map<K, V>) null, nullFactory);
+ fail("Expecting NullPointerException");
+ } catch (NullPointerException e) {
+ // Expected
+ }
+
+ try {
+ DefaultedMap.defaultedMap(base, (Factory<V>) null);
+ fail("Expecting NullPointerException");
+ } catch (NullPointerException e) {
+ // Expected
+ }
+
+ try {
+ DefaultedMap.defaultedMap((Map<K, V>) null, nullTransformer);
+ fail("Expecting NullPointerException");
+ } catch (NullPointerException e) {
+ // Expected
+ }
+
+ try {
+ DefaultedMap.defaultedMap(base, (Transformer<K, V>) null);
+ fail("Expecting NullPointerException");
+ } catch (NullPointerException e) {
+ // Expected
+ }
+ }
+
@Override
public String getCompatibilityVersion() {
return "4";