Skip to content

Commit

Permalink
[Java] Fixing codacy - ConcurrentHashMap (#221)
Browse files Browse the repository at this point in the history
* feat: concurrent hash map

* fix: codacy

* fix: codacy

* fix: revert split up

* feat: revert transport

* fix: rever change

* fix: revert splitted
  • Loading branch information
arifBurakDemiray authored Jan 16, 2024
1 parent 2799ed3 commit 2562fb1
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 62 deletions.
4 changes: 2 additions & 2 deletions sdk-java/src/main/java/ly/count/sdk/java/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import ly.count.sdk.java.internal.CoreFeature;
import ly.count.sdk.java.internal.Log;
import ly.count.sdk.java.internal.LogCallback;
Expand Down Expand Up @@ -214,7 +214,7 @@ public String toString() {
*/
protected boolean enableBackendMode = false;

protected final Map<String, String> metricOverride = new HashMap<>();
protected final Map<String, String> metricOverride = new ConcurrentHashMap<>();

/**
* Salt string for parameter tampering protection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import ly.count.sdk.java.Crash;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -150,7 +151,7 @@ public Map<String, String> getSegments() {
return null;
}
JSONObject object = this.data.getJSONObject("_custom");
Map<String, String> map = new HashMap<>();
Map<String, String> map = new ConcurrentHashMap<>();
Iterator<String> iterator = object.keys();
while (iterator.hasNext()) {
String key = iterator.next();
Expand Down
8 changes: 4 additions & 4 deletions sdk-java/src/main/java/ly/count/sdk/java/internal/Device.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package ly.count.sdk.java.internal;

import com.sun.management.OperatingSystemMXBean;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.File;
import java.lang.management.ManagementFactory;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* Class encapsulating most of device-specific logic: metrics, info, etc.
Expand All @@ -31,7 +31,7 @@ public class Device {

private Log L;

private final Map<String, String> metricOverride = new HashMap<>();
private final Map<String, String> metricOverride = new ConcurrentHashMap<>();

protected Device() {
dev = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.json.JSONArray;
import org.json.JSONObject;

Expand All @@ -21,7 +22,7 @@ public class ModuleBackendMode extends ModuleBase {
protected boolean disabledModule = false;

protected int eventQSize = 0;
protected final Map<String, JSONArray> eventQueues = new HashMap<>();
protected final Map<String, JSONArray> eventQueues = new ConcurrentHashMap<>();

final String[] userPredefinedKeys = { "name", "username", "email", "organization", "phone", "gender", "byear" };

Expand Down Expand Up @@ -181,8 +182,8 @@ private void recordUserPropertiesInternal(String deviceID, Map<String, Object> u

removeInvalidDataFromSegments(userProperties);

Map<String, Object> userDetail = new HashMap<>();
Map<String, Object> customDetail = new HashMap<>();
Map<String, Object> userDetail = new ConcurrentHashMap<>();
Map<String, Object> customDetail = new ConcurrentHashMap<>();
for (Map.Entry<String, Object> item : userProperties.entrySet()) {
if (Arrays.stream(userPredefinedKeys).anyMatch(item.getKey()::equalsIgnoreCase)) {
userDetail.put(item.getKey(), item.getValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ly.count.sdk.java.internal;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import ly.count.sdk.java.Config;
import ly.count.sdk.java.Countly;

Expand Down Expand Up @@ -44,7 +44,7 @@ public String generate(InternalConfig config) {
}
}

private static final Map<Integer, DeviceIdGenerator> generators = new HashMap<>();
private static final Map<Integer, DeviceIdGenerator> generators = new ConcurrentHashMap<>();

protected DeviceId deviceIdInterface;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ly.count.sdk.java.internal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nullable;
import ly.count.sdk.java.Countly;
import org.json.JSONArray;
Expand Down Expand Up @@ -243,7 +243,7 @@ private void reportFeedbackWidgetManuallyInternal(CountlyFeedbackWidget widgetIn
}
}

Map<String, Object> segm = new HashMap<>();
Map<String, Object> segm = new ConcurrentHashMap<>();
segm.put("platform", internalConfig.getSdkPlatform());
segm.put("app_version", cachedAppVersion);
segm.put("widget_id", widgetInfo.widgetId);
Expand Down
4 changes: 2 additions & 2 deletions sdk-java/src/main/java/ly/count/sdk/java/internal/Params.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -230,7 +230,7 @@ public String remove(final String key) {
* @return map of the Params object
*/
public Map<String, String> map() {
Map<String, String> map = new HashMap<>();
Map<String, String> map = new ConcurrentHashMap<>();
List<String> pairs = new ArrayList<>(Arrays.asList(params.toString().split("&")));
for (String pair : pairs) {
String[] comps = pair.split("=");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nullable;
import ly.count.sdk.java.Config;

Expand Down Expand Up @@ -74,7 +74,7 @@ protected static void registerDefaultModuleMappings() {
/**
* Selected by config map of module mappings
*/
private static final Map<Integer, Class<? extends ModuleBase>> moduleMappings = new HashMap<>();
private static final Map<Integer, Class<? extends ModuleBase>> moduleMappings = new ConcurrentHashMap<>();

protected static void registerModuleMapping(int feature, Class<? extends ModuleBase> cls) {
if (cls != null) {
Expand Down Expand Up @@ -427,7 +427,7 @@ public void init(final InternalConfig givenConfig) {
//setup and perform migrations
MigrationHelper migrationHelper = new MigrationHelper(L);
migrationHelper.setupMigrations(config.storageProvider);
Map<String, Object> migrationParams = new HashMap<>();
Map<String, Object> migrationParams = new ConcurrentHashMap<>();
migrationParams.put("sdk_path", config.getSdkStorageRootDirectory());
migrationHelper.applyMigrations(migrationParams);

Expand Down
4 changes: 2 additions & 2 deletions sdk-java/src/main/java/ly/count/sdk/java/internal/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Utility class
Expand Down Expand Up @@ -269,7 +269,7 @@ public static Map<String, String> fixSegmentKeysAndValues(final int keyLength, f
return segments;
}

Map<String, String> segmentation = new HashMap<>();
Map<String, String> segmentation = new ConcurrentHashMap<>();

for (Map.Entry<String, String> entry : segments.entrySet()) {
String k = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ly.count.sdk.java.internal;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import ly.count.sdk.java.Config;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -126,7 +126,7 @@ public void metricOverride() {
Map<String, String> initialVals = internalConfig.getMetricOverride();
Assert.assertEquals(0, initialVals.size());

Map<String, String> newVals = new HashMap<>();
Map<String, String> newVals = new ConcurrentHashMap<>();
newVals.put("a", "1");
newVals.put("b", "2");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package ly.count.sdk.java.internal;

import org.json.JSONObject;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.junit.Assert;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;

public class DeviceTests {

Expand All @@ -13,7 +12,7 @@ public class DeviceTests {
*/
@Test
public void metricOverride_1() {
Map<String, String> newVals = new HashMap<>();
Map<String, String> newVals = new ConcurrentHashMap<>();
newVals.put("a12345", "1qwer");
newVals.put("b5678", "2sdfg");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -110,7 +111,7 @@ public void recorderCalledAfterRecord() {
Assert.assertEquals(true, eventImpl1.getSegment("test"));
}, "test_event", L);

Map<String, Object> segmentation = new HashMap<>();
Map<String, Object> segmentation = new ConcurrentHashMap<>();
segmentation.put("test", true);

event.count = 5;
Expand All @@ -130,7 +131,7 @@ public void validateToJson() {
EventImpl event = new EventImpl((event1) -> {
}, "test_buy_event", L);

Map<String, Object> segmentation = new HashMap<>();
Map<String, Object> segmentation = new ConcurrentHashMap<>();
segmentation.put("valid", false);

event.duration = 34.0;
Expand Down Expand Up @@ -161,7 +162,7 @@ public void validateFromJson() {
EventImpl event = new EventImpl((event1) -> {
}, "test_sell_event", L);

Map<String, Object> segmentation = new HashMap<>();
Map<String, Object> segmentation = new ConcurrentHashMap<>();
segmentation.put("sold", true);

event.count = 3;
Expand Down Expand Up @@ -214,7 +215,7 @@ public void validateFromJson_toJson_segmentation() {
segmentation.put("divisor", 0.2f);
event.segmentation = segmentation;

Map<String, Object> expectedSegmentation = new HashMap<>();
Map<String, Object> expectedSegmentation = new ConcurrentHashMap<>();
expectedSegmentation.put("sold", true);
expectedSegmentation.put("price", BigDecimal.valueOf(9.43));
expectedSegmentation.put("quantity", 3);
Expand Down Expand Up @@ -242,7 +243,7 @@ public void testGetters() {
EventImpl event = new EventImpl((event1) -> {
}, "test_getter_event", L);

Map<String, Object> segmentation = new HashMap<>();
Map<String, Object> segmentation = new ConcurrentHashMap<>();
segmentation.put("get_func", 90);

event.count = 47;
Expand Down Expand Up @@ -270,7 +271,7 @@ public void testSetters() {
event.setDuration(78);
event.setSum(46);

Map<String, String> segmentation = new HashMap<>();
Map<String, String> segmentation = new ConcurrentHashMap<>();
segmentation.put("donated_amount", "37656387");
event.setSegmentation(segmentation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import ly.count.sdk.java.Countly;
import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -156,7 +156,7 @@ public void applyMigrations_legacyToLatest() {
MigrationHelper migrationHelper = new MigrationHelper(mock(Log.class));
migrationHelper.setupMigrations(storageProvider);
Assert.assertEquals(0, migrationHelper.currentDataModelVersion); //legacy state
Map<String, Object> migrationParams = new HashMap<>();
Map<String, Object> migrationParams = new ConcurrentHashMap<>();
migrationParams.put("sdk_path", TestUtils.getTestSDirectory());
//apply migrations
migrationHelper.applyMigrations(migrationParams);
Expand All @@ -179,7 +179,7 @@ public void applyMigrations_latestToLatest() throws IOException {
migrationHelper.setupMigrations(storageProvider);
Assert.assertEquals(expectedLatestSchemaVersion, migrationHelper.currentDataModelVersion); //latest state
//run migration helper apply
migrationHelper.applyMigrations(new HashMap<>());
migrationHelper.applyMigrations(new ConcurrentHashMap<>());
//check migration version is at the latest after apply both from class and file
Assert.assertEquals(expectedLatestSchemaVersion, migrationHelper.currentDataModelVersion);
Assert.assertEquals(expectedLatestSchemaVersion, TestUtils.getJsonStorageProperty(SDKStorage.key_migration_version));
Expand All @@ -203,7 +203,7 @@ public void applyMigrations_0to1() throws IOException {
//run migration helper
Assert.assertNull(storageProvider.getDeviceID());
Assert.assertNull(storageProvider.getDeviceIdType());
Map<String, Object> migrationParams = new HashMap<>();
Map<String, Object> migrationParams = new ConcurrentHashMap<>();
migrationParams.put("sdk_path", TestUtils.getTestSDirectory());
Assert.assertTrue(migrationHelper.migration_DeleteConfigFile_01(migrationParams));

Expand Down Expand Up @@ -291,7 +291,7 @@ public void applyMigrations_1to2_nothingToMigrate() throws IOException {
setDataVersionInConfigFile(1); // set previous data version
initStorage();

Map<String, Object> migrationParams = new HashMap<>();
Map<String, Object> migrationParams = new ConcurrentHashMap<>();
migrationParams.put("sdk_path", TestUtils.getTestSDirectory());

MigrationHelper migrationHelper = new MigrationHelper(mock(Log.class));
Expand Down Expand Up @@ -319,7 +319,7 @@ public void applyMigrations_1to2_nullMigrationParams() throws IOException {
Assert.assertEquals(1, migrationHelper.currentDataModelVersion);
migrationHelper.logger = Mockito.spy(migrationHelper.logger);

Assert.assertFalse(migrationHelper.migration_UserImplFile_02(new HashMap<>()));
Assert.assertFalse(migrationHelper.migration_UserImplFile_02(new ConcurrentHashMap<>()));
Assert.assertEquals(2, migrationHelper.currentDataModelVersion);
Mockito.verify(migrationHelper.logger, Mockito.times(1)).d("[MigrationHelper] migration_UserImplFile_02, No files to delete, returning");
}
Expand Down
Loading

0 comments on commit 2562fb1

Please sign in to comment.