From d28b0e793e65687f370b11d0b8ce5aabfd69e75c Mon Sep 17 00:00:00 2001 From: Nishon Tandukar Date: Wed, 16 Oct 2019 07:55:45 +0545 Subject: [PATCH] Resolves Singleton is not thread safe violation (#403) --- .../common/FieldSightNotificationUtils.java | 2 +- .../naxa/common/SiteOverideLocalSource.java | 11 ++- .../naxa/common/ViewModelFactory.java | 67 +++++++++---------- .../database/FieldSightConfigDatabase.java | 20 +++--- .../naxa/contact/ContactLocalSource.java | 2 +- .../naxa/contact/ContactRemoteSource.java | 2 +- .../naxa/contact/ContactRepository.java | 8 +-- .../FieldSightNotificationLocalSource.java | 2 +- .../EducationalMaterialsLocalSource.java | 2 +- .../EducationalMaterialsRemoteSource.java | 2 +- .../naxa/flagform/FlagFormRemoteSource.java | 2 +- .../naxa/flagform/InstanceRemoteSource.java | 2 +- .../local/FieldSightFormsLocalSourcev3.java | 2 +- .../data/GeneralFormLocalSource.java | 2 +- .../data/GeneralFormRemoteSource.java | 2 +- .../data/GeneralFormRepository.java | 9 +-- .../naxa/network/ServiceGenerator.java | 6 +- .../FieldSightNotificationRepository.java | 10 +-- .../LastSubmissionLocalSource.java | 2 +- .../LastSubmissionRemoteSource.java | 2 +- .../naxa/project/data/ProjectLocalSource.java | 2 +- .../naxa/project/data/ProjectRepository.java | 14 ++-- .../data/ProjectSitesRemoteSource.java | 2 +- .../data/ScheduledFormRepository.java | 9 +-- .../data/ScheduledFormsLocalSource.java | 2 +- .../data/ScheduledFormsRemoteSource.java | 2 +- .../naxa/site/SiteTypeLocalSource.java | 2 +- .../naxa/site/SiteTypeRemoteSource.java | 2 +- .../naxa/site/db/SiteLocalSource.java | 2 +- .../naxa/site/db/SiteRemoteSource.java | 2 +- .../naxa/site/db/SiteRepository.java | 13 ++-- .../site/db/SiteUploadHistoryLocalSource.java | 2 +- .../naxa/stages/StageFormRepository.java | 16 ++--- .../naxa/stages/data/StageLocalSource.java | 2 +- .../naxa/stages/data/StageRemoteSource.java | 2 +- .../substages/data/SubStageLocalSource.java | 2 +- .../substages/data/SubStageRepository.java | 12 ++-- .../naxa/survey/SurveyFormLocalSource.java | 2 +- .../naxa/survey/SurveyFormRepository.java | 9 +-- .../sync/DownloadableItemLocalSource.java | 2 +- .../fieldsight/naxa/sync/SyncRepository.java | 9 +-- .../v3/network/NotificationRemoteSource.java | 2 +- .../naxa/v3/network/ProjectRemoteSource.java | 10 +-- .../naxa/v3/network/SyncLocalSource3.java | 2 +- config/pmd-ruleset.xml | 22 +++--- 45 files changed, 127 insertions(+), 176 deletions(-) diff --git a/collect_app/src/main/java/org/fieldsight/naxa/common/FieldSightNotificationUtils.java b/collect_app/src/main/java/org/fieldsight/naxa/common/FieldSightNotificationUtils.java index dff0ac2af..3ed783f9e 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/common/FieldSightNotificationUtils.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/common/FieldSightNotificationUtils.java @@ -16,7 +16,7 @@ import java.util.concurrent.atomic.AtomicInteger; - +@SuppressWarnings("PMD.UseNotifyAllInsteadOfNotify") public class FieldSightNotificationUtils { private NotificationManager notifManager; private static final String CHANNEL_ID = "fieldsight_notification_channel"; diff --git a/collect_app/src/main/java/org/fieldsight/naxa/common/SiteOverideLocalSource.java b/collect_app/src/main/java/org/fieldsight/naxa/common/SiteOverideLocalSource.java index a20f79cc9..9f58e947b 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/common/SiteOverideLocalSource.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/common/SiteOverideLocalSource.java @@ -1,6 +1,7 @@ package org.fieldsight.naxa.common; import androidx.lifecycle.LiveData; + import android.os.AsyncTask; import org.odk.collect.android.application.Collect; @@ -25,13 +26,11 @@ private SiteOverideLocalSource() { this.siteDao = fieldSightDatabase.getSiteDAO(); } - public static SiteOverideLocalSource getInstance() { + public synchronized static SiteOverideLocalSource getInstance() { + if (siteOverideLocalSource == null) { - synchronized (SiteOverideLocalSource.class) { - if (siteOverideLocalSource == null) { - siteOverideLocalSource = new SiteOverideLocalSource(); - } - } + siteOverideLocalSource = new SiteOverideLocalSource(); + } return siteOverideLocalSource; } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/common/ViewModelFactory.java b/collect_app/src/main/java/org/fieldsight/naxa/common/ViewModelFactory.java index 132e5bdc1..5566b6bc6 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/common/ViewModelFactory.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/common/ViewModelFactory.java @@ -45,7 +45,7 @@ public class ViewModelFactory extends ViewModelProvider.NewInstanceFactory { @SuppressLint("StaticFieldLeak") - private static volatile ViewModelFactory viewModelFactory; + private static ViewModelFactory viewModelFactory; private final GeneralFormRepository generalFormRepository; private final ScheduledFormRepository scheduledFormRepository; @@ -59,15 +59,15 @@ public class ViewModelFactory extends ViewModelProvider.NewInstanceFactory { public ViewModelFactory( - GeneralFormRepository repository, - ScheduledFormRepository scheduledFormRepository, - StageFormRepository stageFormRepository, - SubStageRepository subStageRepository, - ProjectRepository projectRepository, - SiteRepository siteRepository, - SurveyFormRepository surveyFormRepository, - FieldSightNotificationRepository notificationRepository, - ContactRepository contactRepository + GeneralFormRepository repository, + ScheduledFormRepository scheduledFormRepository, + StageFormRepository stageFormRepository, + SubStageRepository subStageRepository, + ProjectRepository projectRepository, + SiteRepository siteRepository, + SurveyFormRepository surveyFormRepository, + FieldSightNotificationRepository notificationRepository, + ContactRepository contactRepository ) { this.generalFormRepository = repository; @@ -81,30 +81,28 @@ public ViewModelFactory( this.contactRepository = contactRepository; } - public static ViewModelFactory getInstance() { + public static synchronized ViewModelFactory getInstance() { if (viewModelFactory == null) { - synchronized (ViewModelFactory.class) { - if (viewModelFactory == null) { - GeneralFormRepository generalFormRepository = GeneralFormRepository.getInstance( - GeneralFormLocalSource.getInstance(), GeneralFormRemoteSource.getInstance()); - ScheduledFormRepository scheduledFormRepository = ScheduledFormRepository.getInstance( - ScheduledFormsLocalSource.getInstance(), ScheduledFormsRemoteSource.getInstance()); - - StageFormRepository stageFormRepository = StageFormRepository.getInstance(StageLocalSource.getInstance(), StageRemoteSource.getInstance()); - SubStageRepository subStageRepository = SubStageRepository.getInstance(SubStageLocalSource.getInstance()); - ProjectRepository projectRepository = ProjectRepository.getInstance(ProjectLocalSource.getInstance(), ProjectSitesRemoteSource.getInstance()); - SiteRepository siteRepository = SiteRepository.getInstance(SiteLocalSource.getInstance()); - SurveyFormRepository surveyFormRepository = SurveyFormRepository.getInstance(SurveyFormLocalSource.getInstance()); - FieldSightNotificationRepository notificationRepository = FieldSightNotificationRepository.getInstance(FieldSightNotificationLocalSource.getInstance()); - ContactRepository contactRepository = ContactRepository.getInstance(ContactLocalSource.getInstance()); - - - viewModelFactory = new ViewModelFactory(generalFormRepository, scheduledFormRepository, - stageFormRepository, subStageRepository, projectRepository, siteRepository, - surveyFormRepository, notificationRepository, contactRepository); - } - } + + GeneralFormRepository generalFormRepository = GeneralFormRepository.getInstance( + GeneralFormLocalSource.getInstance(), GeneralFormRemoteSource.getInstance()); + ScheduledFormRepository scheduledFormRepository = ScheduledFormRepository.getInstance( + ScheduledFormsLocalSource.getInstance(), ScheduledFormsRemoteSource.getInstance()); + + StageFormRepository stageFormRepository = StageFormRepository.getInstance(StageLocalSource.getInstance(), StageRemoteSource.getInstance()); + SubStageRepository subStageRepository = SubStageRepository.getInstance(SubStageLocalSource.getInstance()); + ProjectRepository projectRepository = ProjectRepository.getInstance(ProjectLocalSource.getInstance(), ProjectSitesRemoteSource.getInstance()); + SiteRepository siteRepository = SiteRepository.getInstance(SiteLocalSource.getInstance()); + SurveyFormRepository surveyFormRepository = SurveyFormRepository.getInstance(SurveyFormLocalSource.getInstance()); + FieldSightNotificationRepository notificationRepository = FieldSightNotificationRepository.getInstance(FieldSightNotificationLocalSource.getInstance()); + ContactRepository contactRepository = ContactRepository.getInstance(ContactLocalSource.getInstance()); + + + viewModelFactory = new ViewModelFactory(generalFormRepository, scheduledFormRepository, + stageFormRepository, subStageRepository, projectRepository, siteRepository, + surveyFormRepository, notificationRepository, contactRepository); + } return viewModelFactory; } @@ -147,11 +145,10 @@ public T create(@NonNull Class modelClass) { } else if (modelClass.isAssignableFrom(DownloadViewModel.class)) { //noinspection unchecked return (T) new DownloadViewModel(); - }else if (modelClass.isAssignableFrom(FragmentHostViewModel.class)) { + } else if (modelClass.isAssignableFrom(FragmentHostViewModel.class)) { //noinspection unchecked return (T) new FragmentHostViewModel(); - } - else if (modelClass.isAssignableFrom(FieldSightFormViewModel.class)) { + } else if (modelClass.isAssignableFrom(FieldSightFormViewModel.class)) { //noinspection unchecked return (T) new FieldSightFormViewModel(); } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/common/database/FieldSightConfigDatabase.java b/collect_app/src/main/java/org/fieldsight/naxa/common/database/FieldSightConfigDatabase.java index f0b374f9c..50e683c3a 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/common/database/FieldSightConfigDatabase.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/common/database/FieldSightConfigDatabase.java @@ -3,6 +3,7 @@ import androidx.room.Database; import androidx.room.Room; import androidx.room.RoomDatabase; + import android.content.Context; import org.odk.collect.android.application.Collect; @@ -35,18 +36,17 @@ public abstract class FieldSightConfigDatabase extends RoomDatabase { private static final String DB_PATH = Collect.METADATA_PATH + File.separator + "fieldsight_cofig"; - public static FieldSightConfigDatabase getDatabase(final Context context) { + public synchronized static FieldSightConfigDatabase getDatabase(final Context context) { + if (fieldSightConfigDatabase == null) { - synchronized (FieldSightConfigDatabase.class) { - if (fieldSightConfigDatabase == null) { - fieldSightConfigDatabase = Room.databaseBuilder(context.getApplicationContext(), - FieldSightConfigDatabase.class, DB_PATH) - .fallbackToDestructiveMigration() - .allowMainThreadQueries() - .build(); - } - } + fieldSightConfigDatabase = Room.databaseBuilder(context.getApplicationContext(), + FieldSightConfigDatabase.class, DB_PATH) + .fallbackToDestructiveMigration() + .allowMainThreadQueries() + .build(); } + + return fieldSightConfigDatabase; } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/contact/ContactLocalSource.java b/collect_app/src/main/java/org/fieldsight/naxa/contact/ContactLocalSource.java index 0ea1c8047..7df63d339 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/contact/ContactLocalSource.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/contact/ContactLocalSource.java @@ -23,7 +23,7 @@ private ContactLocalSource() { } - public static ContactLocalSource getInstance() { + public synchronized static ContactLocalSource getInstance() { if (INSTANCE == null) { INSTANCE = new ContactLocalSource(); } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/contact/ContactRemoteSource.java b/collect_app/src/main/java/org/fieldsight/naxa/contact/ContactRemoteSource.java index 8005eeaad..39e042775 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/contact/ContactRemoteSource.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/contact/ContactRemoteSource.java @@ -16,7 +16,7 @@ public class ContactRemoteSource implements BaseRemoteDataSource private static ContactRepository contactRepository; - public static ContactRepository getInstance(ContactLocalSource localSource) { + public static synchronized ContactRepository getInstance(ContactLocalSource localSource) { if (contactRepository == null) { - synchronized (ContactRepository.class) { - if (contactRepository == null) { - contactRepository = new ContactRepository(localSource); - } - } + contactRepository = new ContactRepository(localSource); } return contactRepository; } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/data/source/local/FieldSightNotificationLocalSource.java b/collect_app/src/main/java/org/fieldsight/naxa/data/source/local/FieldSightNotificationLocalSource.java index 3d28d8a23..ba6a73692 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/data/source/local/FieldSightNotificationLocalSource.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/data/source/local/FieldSightNotificationLocalSource.java @@ -80,7 +80,7 @@ public class FieldSightNotificationLocalSource implements BaseLocalDataSource private static EducationalMaterialsLocalSource educationalMaterialsLocalSource; private final EducationalMaterialsDao dao; - public static EducationalMaterialsLocalSource getInstance() { + public synchronized static EducationalMaterialsLocalSource getInstance() { if (educationalMaterialsLocalSource == null) { educationalMaterialsLocalSource = new EducationalMaterialsLocalSource(); } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/educational/EducationalMaterialsRemoteSource.java b/collect_app/src/main/java/org/fieldsight/naxa/educational/EducationalMaterialsRemoteSource.java index 04e18ab0f..78bca4c96 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/educational/EducationalMaterialsRemoteSource.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/educational/EducationalMaterialsRemoteSource.java @@ -49,7 +49,7 @@ public class EducationalMaterialsRemoteSource implements BaseRemoteDataSource { private final GeneralFormLocalSource localSource; private final GeneralFormRemoteSource remoteSource; - public static GeneralFormRepository getInstance(GeneralFormLocalSource localSource, GeneralFormRemoteSource remoteSource) { + public static synchronized GeneralFormRepository getInstance(GeneralFormLocalSource localSource, GeneralFormRemoteSource remoteSource) { if (generalFormRepository == null) { - synchronized (GeneralFormRepository.class) { - if (generalFormRepository == null) { - generalFormRepository = new GeneralFormRepository(localSource, remoteSource); - } - } + generalFormRepository = new GeneralFormRepository(localSource, remoteSource); } return generalFormRepository; } @@ -54,7 +50,6 @@ public LiveData> getByProjectId(boolean forcedUpdate, String p } - public LiveData> getFormsBySiteId(@NonNull String siteId, @NonNull String projectId) { return localSource.getFormsBySiteId(siteId, projectId); } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/network/ServiceGenerator.java b/collect_app/src/main/java/org/fieldsight/naxa/network/ServiceGenerator.java index e2d77d2a5..c8f05d0dd 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/network/ServiceGenerator.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/network/ServiceGenerator.java @@ -82,7 +82,7 @@ private static OkHttpClient createOkHttpClient(boolean cacheRequest) { .build(); } - public static T createService(Class serviceClass) { + public synchronized static T createService(Class serviceClass) { if (retrofit == null) { retrofit = new Retrofit.Builder() .client(createOkHttpClient(false)) @@ -94,7 +94,7 @@ public static T createService(Class serviceClass) { return retrofit.create(serviceClass); } - public static T createCacheService(Class serviceClass) { + public synchronized static T createCacheService(Class serviceClass) { if (cacheablesRetrofit == null) { cacheablesRetrofit = new Retrofit.Builder() .client(createOkHttpClient(true)) @@ -107,7 +107,7 @@ public static T createCacheService(Class serviceClass) { return cacheablesRetrofit.create(serviceClass); } - public static Retrofit getRxClient() { + public synchronized static Retrofit getRxClient() { if (okHttp == null) { diff --git a/collect_app/src/main/java/org/fieldsight/naxa/notificationslist/FieldSightNotificationRepository.java b/collect_app/src/main/java/org/fieldsight/naxa/notificationslist/FieldSightNotificationRepository.java index 5e14379a7..95efd89b2 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/notificationslist/FieldSightNotificationRepository.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/notificationslist/FieldSightNotificationRepository.java @@ -13,16 +13,12 @@ public class FieldSightNotificationRepository implements BaseRepository { private final ScheduledFormsRemoteSource remoteSource; - - public static ScheduledFormRepository getInstance(ScheduledFormsLocalSource localSource, ScheduledFormsRemoteSource remoteSource) { + public synchronized static ScheduledFormRepository getInstance(ScheduledFormsLocalSource localSource, ScheduledFormsRemoteSource remoteSource) { if (scheduledFormRepository == null) { - synchronized (ScheduledFormRepository.class) { - if (scheduledFormRepository == null) { - scheduledFormRepository = new ScheduledFormRepository(localSource, remoteSource); - } - } + scheduledFormRepository = new ScheduledFormRepository(localSource, remoteSource); } return scheduledFormRepository; } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/scheduled/data/ScheduledFormsLocalSource.java b/collect_app/src/main/java/org/fieldsight/naxa/scheduled/data/ScheduledFormsLocalSource.java index e6972c2b2..310495207 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/scheduled/data/ScheduledFormsLocalSource.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/scheduled/data/ScheduledFormsLocalSource.java @@ -39,7 +39,7 @@ private ScheduledFormsLocalSource() { this.dao = database.getProjectScheduledFormsDAO(); } - public static ScheduledFormsLocalSource getInstance() { + public synchronized static ScheduledFormsLocalSource getInstance() { if (scheduledFormsLocalSource == null) { scheduledFormsLocalSource = new ScheduledFormsLocalSource(); } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/scheduled/data/ScheduledFormsRemoteSource.java b/collect_app/src/main/java/org/fieldsight/naxa/scheduled/data/ScheduledFormsRemoteSource.java index fbd3d2996..5c945e21e 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/scheduled/data/ScheduledFormsRemoteSource.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/scheduled/data/ScheduledFormsRemoteSource.java @@ -44,7 +44,7 @@ public class ScheduledFormsRemoteSource implements BaseRemoteDataSource { private static SiteTypeRemoteSource siteTypeRemoteSource; - public static SiteTypeRemoteSource getSiteTypeRemoteSource() { + public synchronized static SiteTypeRemoteSource getSiteTypeRemoteSource() { if (siteTypeRemoteSource == null) { siteTypeRemoteSource = new SiteTypeRemoteSource(); diff --git a/collect_app/src/main/java/org/fieldsight/naxa/site/db/SiteLocalSource.java b/collect_app/src/main/java/org/fieldsight/naxa/site/db/SiteLocalSource.java index 89e992e92..00a891a94 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/site/db/SiteLocalSource.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/site/db/SiteLocalSource.java @@ -35,7 +35,7 @@ private SiteLocalSource() { } - public static SiteLocalSource getInstance() { + public synchronized static SiteLocalSource getInstance() { if (INSTANCE == null) { INSTANCE = new SiteLocalSource(); } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/site/db/SiteRemoteSource.java b/collect_app/src/main/java/org/fieldsight/naxa/site/db/SiteRemoteSource.java index 2681a743e..f70f1c286 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/site/db/SiteRemoteSource.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/site/db/SiteRemoteSource.java @@ -48,7 +48,7 @@ public class SiteRemoteSource implements BaseRemoteDataSource { private static SiteRemoteSource INSTANCE; - public static SiteRemoteSource getInstance() { + public synchronized static SiteRemoteSource getInstance() { if (INSTANCE == null) { INSTANCE = new SiteRemoteSource(); } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/site/db/SiteRepository.java b/collect_app/src/main/java/org/fieldsight/naxa/site/db/SiteRepository.java index b5d787ecf..816ca4e23 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/site/db/SiteRepository.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/site/db/SiteRepository.java @@ -25,14 +25,9 @@ public class SiteRepository implements BaseRepository { private final SiteLocalSource localSource; - - public static SiteRepository getInstance(SiteLocalSource localSource) { + public synchronized static SiteRepository getInstance(SiteLocalSource localSource) { if (instance == null) { - synchronized (SiteRepository.class) { - if (instance == null) { - instance = new SiteRepository(localSource); - } - } + instance = new SiteRepository(localSource); } return instance; } @@ -43,8 +38,8 @@ private SiteRepository(@NonNull SiteLocalSource localSource) { } - public List searchSites(String searchQuery,String projectId) { - return localSource.searchSites(searchQuery,projectId); + public List searchSites(String searchQuery, String projectId) { + return localSource.searchSites(searchQuery, projectId); } public LiveData getSiteById(String id) { diff --git a/collect_app/src/main/java/org/fieldsight/naxa/site/db/SiteUploadHistoryLocalSource.java b/collect_app/src/main/java/org/fieldsight/naxa/site/db/SiteUploadHistoryLocalSource.java index 5a9f5d628..f6e2a06e1 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/site/db/SiteUploadHistoryLocalSource.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/site/db/SiteUploadHistoryLocalSource.java @@ -21,7 +21,7 @@ public class SiteUploadHistoryLocalSource implements BaseLocalDataSource { private final StageLocalSource localSource; private final StageRemoteSource remoteSource; - public static StageFormRepository getInstance(StageLocalSource localSource, StageRemoteSource remoteSource) { + public synchronized static StageFormRepository getInstance(StageLocalSource localSource, StageRemoteSource remoteSource) { if (stageFormRepository == null) { - synchronized (StageFormRepository.class) { - if (stageFormRepository == null) { - stageFormRepository = new StageFormRepository(localSource, remoteSource); - } - } + stageFormRepository = new StageFormRepository(localSource, remoteSource); } return stageFormRepository; } @@ -58,12 +54,12 @@ public void updateAll(ArrayList items) { localSource.updateAll(items); } - public LiveData> getBySiteId(boolean forceUpdate, String siteId, String siteIdType,String projectId) { + public LiveData> getBySiteId(boolean forceUpdate, String siteId, String siteIdType, String projectId) { if (forceUpdate) { remoteSource.getAll(); } - return localSource.getBySiteId(siteId, siteIdType,projectId); + return localSource.getBySiteId(siteId, siteIdType, projectId); } @@ -80,8 +76,8 @@ public Observable> getByProjectIdMaybe(String projectId, String site } - public Observable> getBySiteIdMaybe(String siteId, String siteIdType, String projectId){ - return localSource.getBySiteIdMaybe(siteId,siteIdType,projectId); + public Observable> getBySiteIdMaybe(String siteId, String siteIdType, String projectId) { + return localSource.getBySiteIdMaybe(siteId, siteIdType, projectId); } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/stages/data/StageLocalSource.java b/collect_app/src/main/java/org/fieldsight/naxa/stages/data/StageLocalSource.java index e383c8553..ed3805f54 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/stages/data/StageLocalSource.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/stages/data/StageLocalSource.java @@ -31,7 +31,7 @@ private StageLocalSource() { this.dao = database.getStageDAO(); } - public static StageLocalSource getInstance() { + public synchronized static StageLocalSource getInstance() { if (stageLocalSource == null) { stageLocalSource = new StageLocalSource(); } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/stages/data/StageRemoteSource.java b/collect_app/src/main/java/org/fieldsight/naxa/stages/data/StageRemoteSource.java index c2c9458f1..2f5a1d7c4 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/stages/data/StageRemoteSource.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/stages/data/StageRemoteSource.java @@ -39,7 +39,7 @@ public class StageRemoteSource implements BaseRemoteDataSource { private final SyncRepository syncRepository; - public static StageRemoteSource getInstance() { + public synchronized static StageRemoteSource getInstance() { if (stageRemoteSource == null) { stageRemoteSource = new StageRemoteSource(); } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/substages/data/SubStageLocalSource.java b/collect_app/src/main/java/org/fieldsight/naxa/substages/data/SubStageLocalSource.java index f17ce1a65..c1b9648ae 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/substages/data/SubStageLocalSource.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/substages/data/SubStageLocalSource.java @@ -42,7 +42,7 @@ private SubStageLocalSource() { this.dao = database.getSubStageDAO(); } - public static SubStageLocalSource getInstance() { + public synchronized static SubStageLocalSource getInstance() { if (subStageLocalSource == null) { subStageLocalSource = new SubStageLocalSource(); } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/substages/data/SubStageRepository.java b/collect_app/src/main/java/org/fieldsight/naxa/substages/data/SubStageRepository.java index 1498c245a..9af1f4671 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/substages/data/SubStageRepository.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/substages/data/SubStageRepository.java @@ -17,13 +17,9 @@ public class SubStageRepository implements BaseLocalDataSource { private static SubStageRepository subStageRepository; private final SubStageLocalSource localSource; - public static SubStageRepository getInstance(SubStageLocalSource localSource) { + public synchronized static SubStageRepository getInstance(SubStageLocalSource localSource) { if (subStageRepository == null) { - synchronized (SubStageRepository.class) { - if (subStageRepository == null) { - subStageRepository = new SubStageRepository(localSource); - } - } + subStageRepository = new SubStageRepository(localSource); } return subStageRepository; } @@ -36,7 +32,7 @@ private SubStageRepository(@NonNull SubStageLocalSource localSource) { @Override - public LiveData> getAll( ) { + public LiveData> getAll() { return localSource.getAll(); } @@ -56,6 +52,6 @@ public void updateAll(ArrayList items) { } public MediatorLiveData> getByStageId(String stageId, String siteTypeId) { - return localSource.getByStageId(stageId,siteTypeId); + return localSource.getByStageId(stageId, siteTypeId); } } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/survey/SurveyFormLocalSource.java b/collect_app/src/main/java/org/fieldsight/naxa/survey/SurveyFormLocalSource.java index ff08921f7..0bec76634 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/survey/SurveyFormLocalSource.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/survey/SurveyFormLocalSource.java @@ -22,7 +22,7 @@ private SurveyFormLocalSource() { this.dao = database.getSurveyDAO(); } - public static SurveyFormLocalSource getInstance() { + public synchronized static SurveyFormLocalSource getInstance() { if (surveyFormLocalSource == null) { surveyFormLocalSource = new SurveyFormLocalSource(); } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/survey/SurveyFormRepository.java b/collect_app/src/main/java/org/fieldsight/naxa/survey/SurveyFormRepository.java index e4b5bc2d4..753b445bf 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/survey/SurveyFormRepository.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/survey/SurveyFormRepository.java @@ -3,7 +3,6 @@ import androidx.lifecycle.LiveData; import org.fieldsight.naxa.common.BaseRepository; -import org.fieldsight.naxa.site.db.SiteRepository; import java.util.ArrayList; import java.util.List; @@ -18,13 +17,9 @@ public SurveyFormRepository(SurveyFormLocalSource localSource) { } - public static SurveyFormRepository getInstance(SurveyFormLocalSource localSource) { + public synchronized static SurveyFormRepository getInstance(SurveyFormLocalSource localSource) { if (surveyFormRepository == null) { - synchronized (SiteRepository.class) { - if (surveyFormRepository == null) { - surveyFormRepository = new SurveyFormRepository(localSource); - } - } + surveyFormRepository = new SurveyFormRepository(localSource); } return surveyFormRepository; } diff --git a/collect_app/src/main/java/org/fieldsight/naxa/sync/DownloadableItemLocalSource.java b/collect_app/src/main/java/org/fieldsight/naxa/sync/DownloadableItemLocalSource.java index cfc7ae3e7..30879a229 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/sync/DownloadableItemLocalSource.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/sync/DownloadableItemLocalSource.java @@ -31,7 +31,7 @@ public class DownloadableItemLocalSource implements BaseLocalDataSourceRX getProjects() { diff --git a/collect_app/src/main/java/org/fieldsight/naxa/v3/network/SyncLocalSource3.java b/collect_app/src/main/java/org/fieldsight/naxa/v3/network/SyncLocalSource3.java index 3bdb5c3ae..08a7fad50 100644 --- a/collect_app/src/main/java/org/fieldsight/naxa/v3/network/SyncLocalSource3.java +++ b/collect_app/src/main/java/org/fieldsight/naxa/v3/network/SyncLocalSource3.java @@ -27,7 +27,7 @@ private SyncLocalSource3() { } - public static SyncLocalSource3 getInstance() { + public synchronized static SyncLocalSource3 getInstance() { if (syncLocalSource3 == null) { syncLocalSource3 = new SyncLocalSource3(); } diff --git a/config/pmd-ruleset.xml b/config/pmd-ruleset.xml index 96cd8927d..877657381 100644 --- a/config/pmd-ruleset.xml +++ b/config/pmd-ruleset.xml @@ -83,12 +83,12 @@ - - - - - - + + + + + + @@ -118,11 +118,11 @@ - - - - - + + + + +