Skip to content

Commit

Permalink
Get rid of CopyOnWriteArrayList
Browse files Browse the repository at this point in the history
  • Loading branch information
neonowy authored and westnordost committed Oct 31, 2023
1 parent fca808c commit d74a517
Show file tree
Hide file tree
Showing 29 changed files with 86 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import de.westnordost.streetcomplete.data.osm.edits.ElementEdit
import de.westnordost.streetcomplete.data.osm.edits.ElementEditsSource
import de.westnordost.streetcomplete.data.osmnotes.edits.NoteEdit
import de.westnordost.streetcomplete.data.osmnotes.edits.NoteEditsSource
import de.westnordost.streetcomplete.util.Listeners
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.util.concurrent.CopyOnWriteArrayList

/** Access and listen to how many unsynced (=uploadable) changes there are */
class UnsyncedChangesCountSource(
Expand All @@ -18,7 +18,7 @@ class UnsyncedChangesCountSource(
fun onDecreased()
}

private val listeners = CopyOnWriteArrayList<Listener>()
private val listeners = Listeners<Listener>()

suspend fun getCount(): Int = withContext(Dispatchers.IO) {
elementEditsSource.getUnsyncedCount() + noteEditsSource.getUnsyncedCount()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package de.westnordost.streetcomplete.data.download

import java.util.concurrent.CopyOnWriteArrayList
import de.westnordost.streetcomplete.util.Listeners

class DownloadProgressRelay : DownloadProgressListener {

private val listeners = CopyOnWriteArrayList<DownloadProgressListener>()
private val listeners = Listeners<DownloadProgressListener>()

override fun onStarted() { listeners.forEach { it.onStarted() } }
override fun onError(e: Exception) { listeners.forEach { it.onError(e) } }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package de.westnordost.streetcomplete.data.download.tiles

import de.westnordost.streetcomplete.ApplicationConstants
import de.westnordost.streetcomplete.util.Listeners
import de.westnordost.streetcomplete.util.ktx.nowAsEpochMilliseconds
import java.util.concurrent.CopyOnWriteArrayList

class DownloadedTilesController(
private val dao: DownloadedTilesDao
) : DownloadedTilesSource {

private val listeners = CopyOnWriteArrayList<DownloadedTilesSource.Listener>()
private val listeners = Listeners<DownloadedTilesSource.Listener>()

override fun contains(tilesRect: TilesRect, ignoreOlderThan: Long): Boolean =
dao.contains(tilesRect, ignoreOlderThan)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import de.westnordost.streetcomplete.data.osmnotes.edits.NoteEditsController
import de.westnordost.streetcomplete.data.osmnotes.edits.NoteEditsSource
import de.westnordost.streetcomplete.data.osmnotes.notequests.OsmNoteQuestController
import de.westnordost.streetcomplete.data.osmnotes.notequests.OsmNoteQuestHidden
import de.westnordost.streetcomplete.util.Listeners
import de.westnordost.streetcomplete.util.ktx.nowAsEpochMilliseconds
import java.util.concurrent.CopyOnWriteArrayList

/** All edits done by the user in one place: Edits made on notes, on map data, hidings of quests */
class EditHistoryController(
Expand All @@ -22,7 +22,7 @@ class EditHistoryController(
private val noteQuestController: OsmNoteQuestController,
private val osmQuestController: OsmQuestController
) : EditHistorySource {
private val listeners: MutableList<EditHistorySource.Listener> = CopyOnWriteArrayList()
private val listeners = Listeners<EditHistorySource.Listener>()

private val osmElementEditsListener = object : ElementEditsSource.Listener {
override fun onAddedEdit(edit: ElementEdit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import de.westnordost.streetcomplete.data.user.UserDataController
import de.westnordost.streetcomplete.data.user.UserDataSource
import de.westnordost.streetcomplete.data.user.achievements.Achievement
import de.westnordost.streetcomplete.data.user.achievements.AchievementsSource
import java.util.concurrent.CopyOnWriteArrayList
import de.westnordost.streetcomplete.util.Listeners

/** This class is to access user messages, which are basically dialogs that pop up when
* clicking on the mail icon, such as "you have a new OSM message in your inbox" etc. */
Expand All @@ -24,7 +24,7 @@ class MessagesSource(
interface UpdateListener {
fun onNumberOfMessagesUpdated(numberOfMessages: Int)
}
private val listeners: MutableList<UpdateListener> = CopyOnWriteArrayList()
private val listeners = Listeners<UpdateListener>()

/** Achievement levels unlocked since application start. I.e. when restarting the app, the
* messages about new achievements unlocked are lost, this is deliberate */
Expand Down Expand Up @@ -110,8 +110,6 @@ class MessagesSource(
}

private fun onNumberOfMessagesUpdated() {
for (listener in listeners) {
listener.onNumberOfMessagesUpdated(getNumberOfMessages())
}
listeners.forEach { it.onNumberOfMessagesUpdated(getNumberOfMessages()) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import de.westnordost.streetcomplete.data.messages.QuestSelectionHintState.SHOUL
import de.westnordost.streetcomplete.data.quest.Quest
import de.westnordost.streetcomplete.data.quest.QuestKey
import de.westnordost.streetcomplete.data.quest.VisibleQuestsSource
import java.util.concurrent.CopyOnWriteArrayList
import de.westnordost.streetcomplete.util.Listeners

class QuestSelectionHintController(
private val visibleQuestsSource: VisibleQuestsSource,
Expand All @@ -19,7 +19,7 @@ class QuestSelectionHintController(
interface Listener {
fun onQuestSelectionHintStateChanged()
}
private val listeners: MutableList<Listener> = CopyOnWriteArrayList()
private val listeners = Listeners<Listener>()

var state: QuestSelectionHintState
set(value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import de.westnordost.streetcomplete.data.osm.edits.upload.LastEditTimeStore
import de.westnordost.streetcomplete.data.osm.geometry.ElementGeometry
import de.westnordost.streetcomplete.data.osm.mapdata.ElementKey
import de.westnordost.streetcomplete.data.osm.mapdata.MapDataUpdates
import de.westnordost.streetcomplete.util.Listeners
import de.westnordost.streetcomplete.util.ktx.nowAsEpochMilliseconds
import java.util.concurrent.CopyOnWriteArrayList

class ElementEditsController(
private val editsDB: ElementEditsDao,
Expand All @@ -16,7 +16,7 @@ class ElementEditsController(
/* Must be a singleton because there is a listener that should respond to a change in the
* database table */

private val listeners: MutableList<ElementEditsSource.Listener> = CopyOnWriteArrayList()
private val listeners = Listeners<ElementEditsSource.Listener>()

/* ----------------------- Unsynced edits and syncing them -------------------------------- */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import de.westnordost.streetcomplete.data.osm.mapdata.Relation
import de.westnordost.streetcomplete.data.osm.mapdata.Way
import de.westnordost.streetcomplete.data.osm.mapdata.key
import de.westnordost.streetcomplete.data.upload.ConflictException
import de.westnordost.streetcomplete.util.Listeners
import de.westnordost.streetcomplete.util.math.contains
import de.westnordost.streetcomplete.util.math.intersect
import java.util.concurrent.CopyOnWriteArrayList

/** Source for map data. It combines the original data downloaded with the edits made.
*
Expand All @@ -52,7 +52,7 @@ class MapDataWithEditsSource internal constructor(
/** Called when all map data has been cleared */
fun onCleared()
}
private val listeners: MutableList<Listener> = CopyOnWriteArrayList()
private val listeners = Listeners<Listener>()

/* For thread-safety, all access to these three fields is synchronized. Since there is no hell
* of parallelism, simply any method that somehow accesses these fields (~just about any method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import de.westnordost.streetcomplete.data.osm.geometry.ElementGeometry
import de.westnordost.streetcomplete.data.osm.geometry.ElementGeometryCreator
import de.westnordost.streetcomplete.data.osm.geometry.ElementGeometryDao
import de.westnordost.streetcomplete.data.osm.geometry.ElementGeometryEntry
import de.westnordost.streetcomplete.util.Listeners
import de.westnordost.streetcomplete.util.ktx.format
import de.westnordost.streetcomplete.util.ktx.nowAsEpochMilliseconds
import java.util.concurrent.CopyOnWriteArrayList

/** Controller to access element data and its geometry and handle updates to it (from OSM API) */
class MapDataController internal constructor(
Expand Down Expand Up @@ -36,7 +36,7 @@ class MapDataController internal constructor(
/** Called when all elements have been cleared */
fun onCleared()
}
private val listeners: MutableList<Listener> = CopyOnWriteArrayList()
private val listeners = Listeners<Listener>()

private val cache = MapDataCache(
SPATIAL_CACHE_TILE_ZOOM,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import de.westnordost.streetcomplete.quests.existence.CheckExistence
import de.westnordost.streetcomplete.quests.oneway_suspects.AddSuspectedOneway
import de.westnordost.streetcomplete.quests.opening_hours.AddOpeningHours
import de.westnordost.streetcomplete.quests.place_name.AddPlaceName
import de.westnordost.streetcomplete.util.Listeners
import de.westnordost.streetcomplete.util.ktx.format
import de.westnordost.streetcomplete.util.ktx.intersects
import de.westnordost.streetcomplete.util.ktx.isInAny
Expand All @@ -38,7 +39,6 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.FutureTask

/** Controller for managing OsmQuests. Takes care of persisting OsmQuest objects and notifying
Expand All @@ -60,9 +60,9 @@ class OsmQuestController internal constructor(
fun onUnhid(edit: OsmQuestHidden)
fun onUnhidAll()
}
private val hideListeners: MutableList<HideOsmQuestListener> = CopyOnWriteArrayList()
private val hideListeners = Listeners<HideOsmQuestListener>()

private val listeners: MutableList<OsmQuestSource.Listener> = CopyOnWriteArrayList()
private val listeners = Listeners<OsmQuestSource.Listener>()

private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package de.westnordost.streetcomplete.data.osmnotes
import android.util.Log
import de.westnordost.streetcomplete.data.osm.mapdata.BoundingBox
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.util.Listeners
import de.westnordost.streetcomplete.util.ktx.format
import de.westnordost.streetcomplete.util.ktx.nowAsEpochMilliseconds
import java.util.concurrent.CopyOnWriteArrayList

/** Manages access to the notes storage */
class NoteController(
Expand All @@ -21,7 +21,7 @@ class NoteController(
/** called when all notes have been cleared */
fun onCleared()
}
private val listeners: MutableList<Listener> = CopyOnWriteArrayList()
private val listeners = Listeners<Listener>()

/** Replace all notes in the given bounding box with the given notes */
fun putAllForBBox(bbox: BoundingBox, notes: Collection<Note>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import de.westnordost.streetcomplete.data.osm.mapdata.ElementIdUpdate
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.data.osmnotes.Note
import de.westnordost.streetcomplete.data.osmtracks.Trackpoint
import de.westnordost.streetcomplete.util.Listeners
import de.westnordost.streetcomplete.util.ktx.nowAsEpochMilliseconds
import java.util.concurrent.CopyOnWriteArrayList

class NoteEditsController(
private val editsDB: NoteEditsDao
) : NoteEditsSource {
/* Must be a singleton because there is a listener that should respond to a change in the
* database table */

private val listeners: MutableList<NoteEditsSource.Listener> = CopyOnWriteArrayList()
private val listeners = Listeners<NoteEditsSource.Listener>()

fun add(
noteId: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import de.westnordost.streetcomplete.data.osmnotes.edits.NoteEditAction.COMMENT
import de.westnordost.streetcomplete.data.osmnotes.edits.NoteEditAction.CREATE
import de.westnordost.streetcomplete.data.user.User
import de.westnordost.streetcomplete.data.user.UserDataSource
import java.util.concurrent.CopyOnWriteArrayList
import de.westnordost.streetcomplete.util.Listeners

class NotesWithEditsSource(
private val noteController: NoteController,
Expand All @@ -22,7 +22,7 @@ class NotesWithEditsSource(

fun onCleared()
}
private val listeners: MutableList<Listener> = CopyOnWriteArrayList()
private val listeners = Listeners<Listener>()

private val noteControllerListener = object : NoteController.Listener {
override fun onUpdated(added: Collection<Note>, updated: Collection<Note>, deleted: Collection<Long>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import de.westnordost.streetcomplete.data.osmnotes.NoteComment
import de.westnordost.streetcomplete.data.osmnotes.edits.NotesWithEditsSource
import de.westnordost.streetcomplete.data.user.UserDataSource
import de.westnordost.streetcomplete.data.user.UserLoginStatusSource
import java.util.concurrent.CopyOnWriteArrayList
import de.westnordost.streetcomplete.util.Listeners

/** Used to get visible osm note quests */
class OsmNoteQuestController(
Expand All @@ -25,9 +25,9 @@ class OsmNoteQuestController(
fun onUnhid(edit: OsmNoteQuestHidden)
fun onUnhidAll()
}
private val hideListeners: MutableList<HideOsmNoteQuestListener> = CopyOnWriteArrayList()
private val hideListeners = Listeners<HideOsmNoteQuestListener>()

private val listeners: MutableList<OsmNoteQuestSource.Listener> = CopyOnWriteArrayList()
private val listeners = Listeners<OsmNoteQuestSource.Listener>()

private val showOnlyNotesPhrasedAsQuestions: Boolean get() =
notesPreferences.showOnlyNotesPhrasedAsQuestions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package de.westnordost.streetcomplete.data.overlays

import de.westnordost.streetcomplete.overlays.Overlay
import java.util.concurrent.CopyOnWriteArrayList
import de.westnordost.streetcomplete.util.Listeners

class SelectedOverlayController(
private val selectedOverlayStore: SelectedOverlayStore,
private val overlayRegistry: OverlayRegistry
) : SelectedOverlaySource {

private val listeners = CopyOnWriteArrayList<SelectedOverlaySource.Listener>()
private val listeners = Listeners<SelectedOverlaySource.Listener>()

override var selectedOverlay: Overlay?
set(value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import de.westnordost.streetcomplete.data.osmnotes.notequests.OsmNoteQuestSource
import de.westnordost.streetcomplete.data.overlays.SelectedOverlaySource
import de.westnordost.streetcomplete.data.visiblequests.TeamModeQuestFilter
import de.westnordost.streetcomplete.data.visiblequests.VisibleQuestTypeSource
import de.westnordost.streetcomplete.util.Listeners
import de.westnordost.streetcomplete.util.SpatialCache
import java.util.concurrent.CopyOnWriteArrayList

/** Access and listen to quests visible on the map */
class VisibleQuestsSource(
Expand All @@ -27,7 +27,7 @@ class VisibleQuestsSource(
fun onVisibleQuestsInvalidated()
}

private val listeners: MutableList<Listener> = CopyOnWriteArrayList()
private val listeners = Listeners<Listener>()

private val osmQuestSourceListener = object : OsmQuestSource.Listener {
override fun onUpdated(addedQuests: Collection<OsmQuest>, deletedQuestKeys: Collection<OsmQuestKey>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package de.westnordost.streetcomplete.data.upload

import java.util.concurrent.CopyOnWriteArrayList
import de.westnordost.streetcomplete.util.Listeners

class UploadProgressRelay : UploadProgressListener {
private val listeners = CopyOnWriteArrayList<UploadProgressListener>()
private val listeners = Listeners<UploadProgressListener>()

override fun onStarted() {
listeners.forEach { it.onStarted() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.SharedPreferences
import androidx.core.content.edit
import de.westnordost.osmapi.user.UserDetails
import de.westnordost.streetcomplete.Prefs
import java.util.concurrent.CopyOnWriteArrayList
import de.westnordost.streetcomplete.util.Listeners

/** Controller that handles user login, logout, auth and updated data */
class UserDataController(
Expand All @@ -19,7 +19,7 @@ class UserDataController(
}
}

private val listeners: MutableList<UserDataSource.Listener> = CopyOnWriteArrayList()
private val listeners = Listeners<UserDataSource.Listener>()

override val userId: Long get() = prefs.getLong(Prefs.OSM_USER_ID, -1)
override val userName: String? get() = prefs.getString(Prefs.OSM_USER_NAME, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import android.content.SharedPreferences
import androidx.core.content.edit
import de.westnordost.osmapi.OsmConnection
import de.westnordost.streetcomplete.Prefs
import de.westnordost.streetcomplete.util.Listeners
import oauth.signpost.OAuthConsumer
import java.util.concurrent.CopyOnWriteArrayList

class UserLoginStatusController(
private val oAuthStore: OAuthStore,
private val osmConnection: OsmConnection,
private val prefs: SharedPreferences,
) : UserLoginStatusSource {

private val listeners: MutableList<UserLoginStatusSource.Listener> = CopyOnWriteArrayList()
private val listeners = Listeners<UserLoginStatusSource.Listener>()

override val isLoggedIn: Boolean get() = oAuthStore.isAuthorized

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import de.westnordost.osmapi.user.UserApi
import de.westnordost.streetcomplete.data.osmnotes.AvatarsDownloader
import de.westnordost.streetcomplete.data.user.statistics.StatisticsController
import de.westnordost.streetcomplete.data.user.statistics.StatisticsDownloader
import de.westnordost.streetcomplete.util.Listeners
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import java.util.concurrent.CopyOnWriteArrayList

class UserUpdater(
private val userApi: UserApi,
Expand All @@ -23,7 +23,7 @@ class UserUpdater(
interface Listener {
fun onUserAvatarUpdated()
}
private val userAvatarListeners: MutableList<Listener> = CopyOnWriteArrayList()
private val userAvatarListeners = Listeners<Listener>()

fun update() = coroutineScope.launch(Dispatchers.IO) {
try {
Expand Down
Loading

0 comments on commit d74a517

Please sign in to comment.