Skip to content

Commit

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

* refactor: get rid of some static things

* fix: add merge conflict
  • Loading branch information
arifBurakDemiray authored Jan 22, 2024
1 parent 2562fb1 commit b32c2b0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 70 deletions.
23 changes: 0 additions & 23 deletions sdk-java/src/main/java/ly/count/sdk/java/internal/CoreFeature.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package ly.count.sdk.java.internal;

import java.util.HashMap;
import java.util.Map;

public enum CoreFeature {
Events(1 << 1, ModuleEvents::new),
Sessions(1 << 2, ModuleSessions::new),
Expand Down Expand Up @@ -46,24 +43,4 @@ public ModuleBaseCreator getCreator() {
public int getIndex() {
return index;
}

private static final Map<Integer, CoreFeature> featureMap = new HashMap<Integer, CoreFeature>() {{
put(Sessions.index, Sessions);
put(Events.index, Events);
put(Views.index, Views);
put(CrashReporting.index, CrashReporting);
put(Location.index, Location);
put(UserProfiles.index, UserProfiles);
put(BackendMode.index, BackendMode);
put(RemoteConfig.index, RemoteConfig);
put(TestDummy.index, TestDummy);
put(DeviceId.index, DeviceId);
put(Requests.index, Requests);
put(Logs.index, Logs);
put(Feedback.index, Feedback);
}};

static CoreFeature byIndex(int index) {
return featureMap.get(index);
}
}
77 changes: 32 additions & 45 deletions sdk-java/src/main/java/ly/count/sdk/java/internal/SDKCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ public SDKCore() {
protected static ModuleBase testDummyModule = null;//set during testing when trying to check the SDK's lifecycle

protected static void registerDefaultModuleMappings() {
moduleMappings.put(CoreFeature.DeviceId.getIndex(), ModuleDeviceIdCore.class);
moduleMappings.put(CoreFeature.Requests.getIndex(), ModuleRequests.class);
moduleMappings.put(CoreFeature.DeviceId, ModuleDeviceIdCore.class);
moduleMappings.put(CoreFeature.Requests, ModuleRequests.class);
//moduleMappings.put(CoreFeature.Logs.getIndex(), Log.class);
moduleMappings.put(CoreFeature.Views.getIndex(), ModuleViews.class);
moduleMappings.put(CoreFeature.Sessions.getIndex(), ModuleSessions.class);
moduleMappings.put(CoreFeature.CrashReporting.getIndex(), ModuleCrashes.class);
moduleMappings.put(CoreFeature.BackendMode.getIndex(), ModuleBackendMode.class);
moduleMappings.put(CoreFeature.Feedback.getIndex(), ModuleFeedback.class);
moduleMappings.put(CoreFeature.Events.getIndex(), ModuleEvents.class);
moduleMappings.put(CoreFeature.RemoteConfig.getIndex(), ModuleRemoteConfig.class);
moduleMappings.put(CoreFeature.UserProfiles.getIndex(), ModuleUserProfile.class);
moduleMappings.put(CoreFeature.Location.getIndex(), ModuleLocation.class);
moduleMappings.put(CoreFeature.Views, ModuleViews.class);
moduleMappings.put(CoreFeature.Sessions, ModuleSessions.class);
moduleMappings.put(CoreFeature.CrashReporting, ModuleCrashes.class);
moduleMappings.put(CoreFeature.BackendMode, ModuleBackendMode.class);
moduleMappings.put(CoreFeature.Feedback, ModuleFeedback.class);
moduleMappings.put(CoreFeature.Events, ModuleEvents.class);
moduleMappings.put(CoreFeature.RemoteConfig, ModuleRemoteConfig.class);
moduleMappings.put(CoreFeature.UserProfiles, ModuleUserProfile.class);
moduleMappings.put(CoreFeature.Location, ModuleLocation.class);
}

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

protected static void registerModuleMapping(int feature, Class<? extends ModuleBase> cls) {
if (cls != null) {
moduleMappings.put(feature, cls);
}
}
private static final Map<CoreFeature, Class<? extends ModuleBase>> moduleMappings = new ConcurrentHashMap<>();

// TreeMap to keep modules sorted by their feature indexes
protected final Map<Integer, ModuleBase> modules;
Expand All @@ -92,7 +86,7 @@ protected static void registerModuleMapping(int feature, Class<? extends ModuleB
* @return {@code true} if consent has been given
*/
public boolean isTracking(Integer feat) {
return modules != null && modules.containsKey(feat);
return modules.containsKey(feat);
}

private void onTimer() {
Expand Down Expand Up @@ -175,7 +169,7 @@ public void onConsent(final int consent) {

consents = consents | (consent & config.getFeatures1());

for (Integer feature : moduleMappings.keySet()) {
for (CoreFeature feature : moduleMappings.keySet()) {
ModuleBase existing = module(moduleMappings.get(feature));
if (SDKCore.enabled(feature) && existing == null) {
ModuleBase module = instantiateModule(feature);
Expand All @@ -184,7 +178,7 @@ public void onConsent(final int consent) {
} else {
module.init(config);
module.initFinished(config);
modules.put(feature, module);
modules.put(feature.getIndex(), module);
}
}
}
Expand Down Expand Up @@ -214,11 +208,11 @@ public void onConsentRemoval(final InternalConfig config, int noConsent) {

consents = consents & ~noConsent;

for (Integer feature : moduleMappings.keySet()) {
for (CoreFeature feature : moduleMappings.keySet()) {
ModuleBase existing = module(moduleMappings.get(feature));
if (feature != CoreFeature.Sessions.getIndex() && existing != null) {
if (feature.getIndex() != CoreFeature.Sessions.getIndex() && existing != null) {
existing.stop(config, true);
modules.remove(feature);
modules.remove(feature.getIndex());
}
}
}
Expand All @@ -228,21 +222,16 @@ public void onConsentRemoval(final InternalConfig config, int noConsent) {
* Uses {@link #moduleMappings} for {@code Config.Feature} / {@link CoreFeature}
* - Class&lt;ModuleBase&gt; mapping to enable overriding by app developer.
*
* @param config {@link InternalConfig} object containing config with mapping overrides
* @throws IllegalArgumentException in case some {@link ModuleBase} finds {@link #config} inconsistent.
* @throws IllegalStateException when this module is run second time on the same {@code Core} instance.
*/
protected void prepareMappings(InternalConfig config) throws IllegalStateException {
protected void prepareMappings() throws IllegalStateException {
if (!modules.isEmpty()) {
throw new IllegalStateException("Modules can only be built once");
}

moduleMappings.clear();
registerDefaultModuleMappings();

for (int feature : config.getModuleOverrides()) {
registerModuleMapping(feature, config.getModuleOverride(feature));
}
}

/**
Expand Down Expand Up @@ -279,16 +268,16 @@ protected void buildModules(InternalConfig config, int features) throws IllegalA
}

if (!config.requiresConsent()) {
for (int feature : moduleMappings.keySet()) {
for (CoreFeature feature : moduleMappings.keySet()) {
Class<? extends ModuleBase> cls = moduleMappings.get(feature);
if (cls == null) {
continue;
}
ModuleBase existing = module(cls);
if ((features & feature) > 0 && existing == null) {
if ((features & feature.getIndex()) > 0 && existing == null) {
ModuleBase m = instantiateModule(feature);
if (m != null) {
modules.put(feature, m);
modules.put(feature.getIndex(), m);
}
}
}
Expand All @@ -304,12 +293,10 @@ protected void buildModules(InternalConfig config, int features) throws IllegalA
/**
* Create {@link ModuleBase} by executing its default constructor.
*
* @param feature int value of feature
* @param coreFeature int value of feature
* @return {@link ModuleBase} instance or null if {@link ModuleBaseCreator} is not set for {@code feature}
*/
private ModuleBase instantiateModule(int feature) {
CoreFeature coreFeature = CoreFeature.byIndex(feature);

private ModuleBase instantiateModule(CoreFeature coreFeature) {
if (coreFeature.getCreator() == null) {
return null;
}
Expand All @@ -322,7 +309,7 @@ private ModuleBase instantiateModule(int feature) {
* @param feature to get a {@link ModuleBase} instance for
* @return {@link ModuleBase} instance or null if no such module is instantiated
*/
protected ModuleBase module(int feature) {
protected ModuleBase module(CoreFeature feature) {
return module(moduleMappings.get(feature));
}

Expand Down Expand Up @@ -364,15 +351,15 @@ public SessionImpl onSessionEnded(final InternalConfig config, SessionImpl sessi
for (ModuleBase m : modules.values()) {
m.onSessionEnded(session, config);
}
ModuleSessions sessions = (ModuleSessions) module(CoreFeature.Sessions.getIndex());
ModuleSessions sessions = (ModuleSessions) module(CoreFeature.Sessions);
if (sessions != null) {
sessions.forgetSession();
}
return session;
}

public SessionImpl getSession() {
ModuleSessions sessions = (ModuleSessions) module(CoreFeature.Sessions.getIndex());
ModuleSessions sessions = (ModuleSessions) module(CoreFeature.Sessions);
if (sessions != null) {
return sessions.getSession();
}
Expand All @@ -386,7 +373,7 @@ public SessionImpl getSession() {
* @return current {@link SessionImpl} instance
*/
public SessionImpl session(final Long id) {
ModuleSessions sessions = (ModuleSessions) module(CoreFeature.Sessions.getIndex());
ModuleSessions sessions = (ModuleSessions) module(CoreFeature.Sessions);
if (sessions != null) {
return sessions.session(config, id);
}
Expand Down Expand Up @@ -418,7 +405,7 @@ public void init(final InternalConfig givenConfig) {
}

//setup module mapping
prepareMappings(config);
prepareMappings();

//create internal timer
countlyTimer = new CountlyTimer(L);
Expand Down Expand Up @@ -549,11 +536,11 @@ public void notifyModulesDeviceIdChanged(@Nullable String old, final boolean wit
}

public void login(String id) {
((ModuleDeviceIdCore) module(CoreFeature.DeviceId.getIndex())).login(id);
((ModuleDeviceIdCore) module(CoreFeature.DeviceId)).login(id);
}

public void logout() {
((ModuleDeviceIdCore) module(CoreFeature.DeviceId.getIndex())).logout();
((ModuleDeviceIdCore) module(CoreFeature.DeviceId)).logout();
}

/**
Expand Down Expand Up @@ -705,7 +692,7 @@ public void onRequest(InternalConfig config, Request request) {
* @deprecated use {@link ModuleEvents.Events#startEvent(String)} instead via <code>instance().events()</code> call
*/
TimedEvents timedEvents() {
return ((ModuleSessions) module(CoreFeature.Sessions.getIndex())).timedEvents();
return ((ModuleSessions) module(CoreFeature.Sessions)).timedEvents();
}

public ModuleEvents.Events events() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public void recordEvent(Event event) {
begin();
}

ModuleEvents eventsModule = (ModuleEvents) SDKCore.instance.module(CoreFeature.Events.getIndex());
ModuleEvents eventsModule = (ModuleEvents) SDKCore.instance.module(CoreFeature.Events);
EventImpl eventImpl = (EventImpl) event;
eventsModule.recordEventInternal(eventImpl.key, eventImpl.count, eventImpl.sum, eventImpl.duration, eventImpl.segmentation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ModuleEventsTests {

private void init(Config cc) {
Countly.instance().init(cc);
moduleEvents = (ModuleEvents) SDKCore.instance.module(CoreFeature.Events.getIndex());
moduleEvents = (ModuleEvents) SDKCore.instance.module(CoreFeature.Events);
}

@After
Expand Down

0 comments on commit b32c2b0

Please sign in to comment.