From 536249e5df2594fee745849427fe89a0b8b1b8bf Mon Sep 17 00:00:00 2001 From: EHJ-52n Date: Tue, 6 Jun 2017 10:36:59 +0200 Subject: [PATCH 1/2] minor code cleaning --- .../org/n52/sos/util/CollectionHelper.java | 1 + .../n52/sos/util/CollectionHelperTest.java | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/core/api/src/main/java/org/n52/sos/util/CollectionHelper.java b/core/api/src/main/java/org/n52/sos/util/CollectionHelper.java index c2afb0e00b..9dcb9c316c 100644 --- a/core/api/src/main/java/org/n52/sos/util/CollectionHelper.java +++ b/core/api/src/main/java/org/n52/sos/util/CollectionHelper.java @@ -55,6 +55,7 @@ * @since 4.0.0 */ public final class CollectionHelper { + private CollectionHelper() { } diff --git a/core/api/src/test/java/org/n52/sos/util/CollectionHelperTest.java b/core/api/src/test/java/org/n52/sos/util/CollectionHelperTest.java index 76a1e6403c..b650a07272 100644 --- a/core/api/src/test/java/org/n52/sos/util/CollectionHelperTest.java +++ b/core/api/src/test/java/org/n52/sos/util/CollectionHelperTest.java @@ -53,7 +53,8 @@ * */ public class CollectionHelperTest { - private final Set EMPTY_COLLECTION = new HashSet(0); + + private final Set EMPTY_COLLECTION = new HashSet<>(0); @Test public void should_return_empty_list_when_union_receives_null() { @@ -62,32 +63,32 @@ public void should_return_empty_list_when_union_receives_null() { @Test public void should_return_empty_list_when_unionOfListOfLists_receives_empty_list() { - final Collection> emptyList = new ArrayList>(0); + final Collection> emptyList = new ArrayList<>(0); assertThat(unionOfListOfLists(emptyList), is(EMPTY_COLLECTION)); } @Test public void should_return_union_of_values_without_duplicates() { - final Collection listA = new ArrayList(2); + final Collection listA = new ArrayList<>(2); listA.add("A"); listA.add("B"); - final Collection listB = new ArrayList(4); + final Collection listB = new ArrayList<>(4); listB.add("B"); listB.add("C"); listB.add(null); - final Collection listC = new ArrayList(2); + final Collection listC = new ArrayList<>(2); listC.add(""); - final Collection> col = new ArrayList>(4); + final Collection> col = new ArrayList<>(4); col.add(listA); col.add(listB); col.add(listC); col.add(null); col.add(new ArrayList(0)); - final Collection check = new HashSet(4); + final Collection check = new HashSet<>(4); check.add("A"); check.add("B"); check.add("C"); @@ -97,26 +98,26 @@ public void should_return_union_of_values_without_duplicates() { @Test public void isNotEmpty_should_return_true_if_map_is_not_empty() { - final Map map = new HashMap(1); + final Map map = new HashMap<>(1); map.put("key", "value"); assertThat(CollectionHelper.isNotEmpty(map), is(TRUE)); } @Test public void isNotEmpty_should_return_false_if_map_is_empty() { - final Map map = new HashMap(0); + final Map map = new HashMap<>(0); assertThat(CollectionHelper.isNotEmpty(map), is(FALSE)); } @Test public void isEmpty_should_return_true_if_map_is_empty() { - final Map map = new HashMap(0); + final Map map = new HashMap<>(0); assertThat(CollectionHelper.isEmpty(map), is(TRUE)); } @Test public void isEmpty_should_return_false_if_map_is_not_empty() { - final Map map = new HashMap(1); + final Map map = new HashMap<>(1); map.put("key", "value"); assertThat(CollectionHelper.isEmpty(map), is(FALSE)); } From 32aee2b1ffa4305971d92f859462f60e2353f5fd Mon Sep 17 00:00:00 2001 From: EHJ-52n Date: Tue, 6 Jun 2017 10:43:24 +0200 Subject: [PATCH 2/2] Fix NPE when offering key is not contained in featuresOfInterestForOfferings --- .../main/java/org/n52/sos/util/CollectionHelper.java | 1 - .../java/org/n52/sos/util/CollectionHelperTest.java | 1 + .../java/org/n52/sos/cache/InMemoryCacheImpl.java | 11 +++++++---- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/core/api/src/main/java/org/n52/sos/util/CollectionHelper.java b/core/api/src/main/java/org/n52/sos/util/CollectionHelper.java index 9dcb9c316c..7d4f362e4c 100644 --- a/core/api/src/main/java/org/n52/sos/util/CollectionHelper.java +++ b/core/api/src/main/java/org/n52/sos/util/CollectionHelper.java @@ -546,6 +546,5 @@ public static String[] svStringToArray(String sv, String separator) { } return split; } - } diff --git a/core/api/src/test/java/org/n52/sos/util/CollectionHelperTest.java b/core/api/src/test/java/org/n52/sos/util/CollectionHelperTest.java index b650a07272..8540115a71 100644 --- a/core/api/src/test/java/org/n52/sos/util/CollectionHelperTest.java +++ b/core/api/src/test/java/org/n52/sos/util/CollectionHelperTest.java @@ -153,4 +153,5 @@ public void should_return_set_sorted_by_value() { assertThat(iterator.next(),is(3)); assertThat(iterator.next(),is(4)); } + } diff --git a/core/cache/src/main/java/org/n52/sos/cache/InMemoryCacheImpl.java b/core/cache/src/main/java/org/n52/sos/cache/InMemoryCacheImpl.java index 7dc6175295..a419555ad9 100644 --- a/core/cache/src/main/java/org/n52/sos/cache/InMemoryCacheImpl.java +++ b/core/cache/src/main/java/org/n52/sos/cache/InMemoryCacheImpl.java @@ -1359,11 +1359,14 @@ public void removeFeatureOfInterestForResultTemplate(final String resultTemplate @Override public void removeFeaturesOfInterestForOffering(final String offering) { notNullOrEmpty(OFFERING, offering); - LOG.trace("Removing featuresOfInterest for offering {}", offering); - for (String featureOfInterest : featuresOfInterestForOfferings.get(offering)) { - this.offeringsForFeaturesOfInterest.removeWithKey(featureOfInterest, offering); + // prevent NPEs in foreach of Map.get(..) + if (featuresOfInterestForOfferings.containsKey(offering)) { + LOG.trace("Removing featuresOfInterest for offering {}", offering); + for (String featureOfInterest : featuresOfInterestForOfferings.get(offering)) { + this.offeringsForFeaturesOfInterest.removeWithKey(featureOfInterest, offering); + } + this.featuresOfInterestForOfferings.remove(offering); } - this.featuresOfInterestForOfferings.remove(offering); } @Override