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

New JSONUtils and MapUtils #328

Merged
merged 2 commits into from
Jan 13, 2023
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
8 changes: 8 additions & 0 deletions code/android-core-library/api/android-core-library.api
Original file line number Diff line number Diff line change
Expand Up @@ -1031,10 +1031,18 @@ public class com/adobe/marketing/mobile/util/EventDataUtils {
}

public final class com/adobe/marketing/mobile/util/JSONUtils {
public static fun isNullOrEmpty (Lorg/json/JSONArray;)Z
public static fun isNullOrEmpty (Lorg/json/JSONObject;)Z
public static fun toList (Lorg/json/JSONArray;)Ljava/util/List;
public static fun toMap (Lorg/json/JSONObject;)Ljava/util/Map;
}

public final class com/adobe/marketing/mobile/util/MapUtils {
public static fun isNullOrEmpty (Ljava/util/Map;)Z
public static fun putIfNotEmpty (Ljava/util/Map;Ljava/lang/String;Ljava/lang/String;)V
public static fun putIfNotEmpty (Ljava/util/Map;Ljava/lang/String;Ljava/util/Map;)V
}

public class com/adobe/marketing/mobile/util/SerialWorkDispatcher {
public fun <init> (Ljava/lang/String;Lcom/adobe/marketing/mobile/util/SerialWorkDispatcher$WorkHandler;)V
protected fun canWork ()Z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,31 @@
import org.json.JSONException;
import org.json.JSONObject;

/** Utility class for transforming json objects. */
/** Utility class for JSON objects. */
public final class JSONUtils {

private JSONUtils() {}

/**
* Checks if the provided {@code JSONObject} is null or it has no element
*
* @param jsonObject the {@code JSONObject} to be verified
* @return true if null or empty, false otherwise
*/
public static boolean isNullOrEmpty(@Nullable final JSONObject jsonObject) {
return jsonObject == null || jsonObject.length() == 0;
}

/**
* Checks if the provided {@code JSONArray} is null or it has no element
*
* @param jsonArray the {@code JSONArray} to be verified
* @return true if null or empty, false otherwise
*/
public static boolean isNullOrEmpty(@Nullable final JSONArray jsonArray) {
return jsonArray == null || jsonArray.length() == 0;
}

/**
* Converts contents of a {@code JSONObject} into a {@code Map<String, Object>}
*
Expand Down
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
package com.adobe.marketing.mobile.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -26,6 +28,40 @@

public class JSONUtilsTest {

@Test
public void testIsNullOrEmpty_withJSONObject_whenNull() {
assertTrue(JSONUtils.isNullOrEmpty((JSONObject) null));
}

@Test
public void testIsNullOrEmpty_withJSONObject_whenEmpty() {
assertTrue(JSONUtils.isNullOrEmpty(new JSONObject()));
}

@Test
public void testIsNullOrEmpty_withJSONObject_whenNonEmpty() throws JSONException {
JSONObject test = new JSONObject();
test.put("key", "value");
assertFalse(JSONUtils.isNullOrEmpty(test));
}

@Test
public void testIsNullOrEmpty_withJSONArray_whenNull() {
assertTrue(JSONUtils.isNullOrEmpty((JSONArray) null));
}

@Test
public void testIsNullOrEmpty_withJSONArray_whenEmpty() {
assertTrue(JSONUtils.isNullOrEmpty(new JSONArray()));
}

@Test
public void testIsNullOrEmpty_withJSONArray_whenNonEmpty() {
JSONArray test = new JSONArray();
test.put("test");
assertFalse(JSONUtils.isNullOrEmpty(test));
}

@Test
public void testToList_Null() throws JSONException {
assertNull(JSONUtils.toList(null));
Expand Down
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"));
}
}