diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/CoreFeature.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/CoreFeature.java index 2ddc7e92..76aa0a4d 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/CoreFeature.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/CoreFeature.java @@ -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), @@ -46,24 +43,4 @@ public ModuleBaseCreator getCreator() { public int getIndex() { return index; } - - private static final Map featureMap = new HashMap() {{ - 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); - } } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKCore.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKCore.java index 0991a7cf..61fb5c21 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKCore.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/SDKCore.java @@ -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); } /** @@ -74,13 +74,7 @@ protected static void registerDefaultModuleMappings() { /** * Selected by config map of module mappings */ - private static final Map> moduleMappings = new ConcurrentHashMap<>(); - - protected static void registerModuleMapping(int feature, Class cls) { - if (cls != null) { - moduleMappings.put(feature, cls); - } - } + private static final Map> moduleMappings = new ConcurrentHashMap<>(); // TreeMap to keep modules sorted by their feature indexes protected final Map modules; @@ -92,7 +86,7 @@ protected static void registerModuleMapping(int feature, Class 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); } } } @@ -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; } @@ -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)); } @@ -364,7 +351,7 @@ 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(); } @@ -372,7 +359,7 @@ public SessionImpl onSessionEnded(final InternalConfig config, SessionImpl sessi } public SessionImpl getSession() { - ModuleSessions sessions = (ModuleSessions) module(CoreFeature.Sessions.getIndex()); + ModuleSessions sessions = (ModuleSessions) module(CoreFeature.Sessions); if (sessions != null) { return sessions.getSession(); } @@ -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); } @@ -418,7 +405,7 @@ public void init(final InternalConfig givenConfig) { } //setup module mapping - prepareMappings(config); + prepareMappings(); //create internal timer countlyTimer = new CountlyTimer(L); @@ -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(); } /** @@ -705,7 +692,7 @@ public void onRequest(InternalConfig config, Request request) { * @deprecated use {@link ModuleEvents.Events#startEvent(String)} instead via instance().events() call */ TimedEvents timedEvents() { - return ((ModuleSessions) module(CoreFeature.Sessions.getIndex())).timedEvents(); + return ((ModuleSessions) module(CoreFeature.Sessions)).timedEvents(); } public ModuleEvents.Events events() { diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/SessionImpl.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/SessionImpl.java index a964a84c..d2e1b277 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/SessionImpl.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/SessionImpl.java @@ -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); } diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleEventsTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleEventsTests.java index 688ee6c7..a1a854b9 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleEventsTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleEventsTests.java @@ -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