-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #328 from emdobrin/coreutils
- Loading branch information
Showing
5 changed files
with
238 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
code/android-core-library/src/main/java/com/adobe/marketing/mobile/util/MapUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
Copyright 2023 Adobe. All rights reserved. | ||
This file is licensed to you 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 REPRESENTATIONS | ||
OF ANY KIND, either express or implied. See the License for the specific language | ||
governing permissions and limitations under the License. | ||
*/ | ||
|
||
package com.adobe.marketing.mobile.util; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import java.util.Map; | ||
|
||
/** Utility class for Maps */ | ||
public final class MapUtils { | ||
|
||
private MapUtils() {} | ||
|
||
/** | ||
* Checks if the provided {@code map} is null or it has no element | ||
* | ||
* @param map the {@code map} to be verified | ||
* @return true if null or empty, false otherwise | ||
*/ | ||
public static boolean isNullOrEmpty(@Nullable final Map<String, Object> map) { | ||
return map == null || map.isEmpty(); | ||
} | ||
|
||
/** | ||
* Adds {@code key}/{@code values} to {@code map} if {@code values} is not null or an empty | ||
* collection. | ||
* | ||
* @param map collection to put {@code values} mapped to {@code key} if {@code values} is | ||
* non-null and contains at least one entry | ||
* @param key key used to map {@code values} in {@code map} | ||
* @param values a map to add to {@code map} if not null or empty | ||
*/ | ||
public static void putIfNotEmpty( | ||
@NonNull final Map<String, Object> map, | ||
@Nullable final String key, | ||
@Nullable final Map<String, Object> values) { | ||
boolean addValues = map != null && key != null && !isNullOrEmpty(values); | ||
|
||
if (addValues) { | ||
map.put(key, values); | ||
} | ||
} | ||
|
||
/** | ||
* Adds {@code key}/{@code value} to {@code map} if {@code value} is not null or empty. | ||
* | ||
* @param map collection to put {@code value} mapped to {@code key} if {@code value} is non-null | ||
* and not empty | ||
* @param key key used to map {@code value} in {@code map} | ||
* @param value a String to add to {@code map} if not null or empty | ||
*/ | ||
public static void putIfNotEmpty( | ||
@NonNull final Map<String, Object> map, | ||
@Nullable final String key, | ||
@Nullable final String value) { | ||
boolean addValues = map != null && key != null && !StringUtils.isNullOrEmpty(value); | ||
|
||
if (addValues) { | ||
map.put(key, value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
code/android-core-library/src/test/java/com/adobe/marketing/mobile/util/MapUtilsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
Copyright 2023 Adobe. All rights reserved. | ||
This file is licensed to you 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 REPRESENTATIONS | ||
OF ANY KIND, either express or implied. See the License for the specific language | ||
governing permissions and limitations under the License. | ||
*/ | ||
|
||
package com.adobe.marketing.mobile.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.junit.Test; | ||
|
||
public class MapUtilsTests { | ||
|
||
@Test | ||
public void testIsNullOrEmpty_withMap_whenNull() { | ||
assertTrue(MapUtils.isNullOrEmpty((Map) null)); | ||
} | ||
|
||
@Test | ||
public void testIsNullOrEmpty_withMap_whenEmpty() { | ||
assertTrue(MapUtils.isNullOrEmpty(new HashMap<>())); | ||
} | ||
|
||
@Test | ||
public void testIsNullOrEmpty_withMap_whenNonEmpty() { | ||
Map<String, Object> test = new HashMap<>(); | ||
test.put("key", "value"); | ||
assertFalse(MapUtils.isNullOrEmpty(test)); | ||
} | ||
|
||
@Test | ||
public void testPutIfNotEmpty_withString_whenNullMap() { | ||
MapUtils.putIfNotEmpty(null, "key", "value"); | ||
} | ||
|
||
@Test | ||
public void testPutIfNotEmpty_withString_whenNullKey() { | ||
Map<String, Object> map = new HashMap<>(); | ||
MapUtils.putIfNotEmpty(map, null, "value"); | ||
assertTrue(map.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testPutIfNotEmpty_withString_whenNullValue() { | ||
Map<String, Object> map = new HashMap<>(); | ||
MapUtils.putIfNotEmpty(map, "key", (String) null); | ||
assertTrue(map.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testPutIfNotEmpty_withString_whenValidValue() { | ||
Map<String, Object> map = new HashMap<>(); | ||
MapUtils.putIfNotEmpty(map, "key", "value"); | ||
assertEquals(1, map.size()); | ||
assertEquals("value", map.get("key")); | ||
} | ||
|
||
@Test | ||
public void testPutIfNotEmpty_withMap_whenNullMap() { | ||
Map<String, Object> innerMap = new HashMap<>(); | ||
innerMap.put("test", true); | ||
MapUtils.putIfNotEmpty(null, "key", innerMap); | ||
} | ||
|
||
@Test | ||
public void testPutIfNotEmpty_withMap_whenNullKey() { | ||
Map<String, Object> innerMap = new HashMap<>(); | ||
innerMap.put("test", true); | ||
|
||
Map<String, Object> map = new HashMap<>(); | ||
MapUtils.putIfNotEmpty(map, null, innerMap); | ||
assertTrue(map.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testPutIfNotEmpty_withMap_whenNullValue() { | ||
Map<String, Object> map = new HashMap<>(); | ||
MapUtils.putIfNotEmpty(map, "key", (Map) null); | ||
assertTrue(map.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testPutIfNotEmpty_withMap_whenValidValue() { | ||
Map<String, Object> innerMap = new HashMap<>(); | ||
innerMap.put("test", true); | ||
|
||
Map<String, Object> map = new HashMap<>(); | ||
MapUtils.putIfNotEmpty(map, "key", innerMap); | ||
assertEquals(1, map.size()); | ||
assertEquals(innerMap, map.get("key")); | ||
} | ||
} |