Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ import com.ichi2.utils.ClipboardUtil.getText
import com.ichi2.utils.Computation
import com.ichi2.utils.HandlerUtils.executeFunctionWithDelay
import com.ichi2.utils.HandlerUtils.newHandler
import com.ichi2.utils.HashUtil.HashSetInit
import com.ichi2.utils.HashSetInit
import com.ichi2.utils.KotlinCleanup
import com.ichi2.utils.WebViewDebugging.initializeDebugging
import com.ichi2.utils.iconAttr
Expand Down
9 changes: 1 addition & 8 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package com.ichi2.anki
import com.ichi2.anki.servicelayer.NoteService.isMarked
import com.ichi2.libanki.Card
import com.ichi2.libanki.Note
import com.ichi2.utils.HashUtil.HashSetInit
import java.util.*

/**
Expand All @@ -14,13 +13,7 @@ object CardUtils {
/**
* @return List of corresponding notes without duplicates, even if the input list has multiple cards of the same note.
*/
fun getNotes(cards: Collection<Card>): Set<Note> {
val notes: MutableSet<Note> = HashSetInit(cards.size)
for (card in cards) {
notes.add(card.note())
}
return notes
}
fun getNotes(cards: Collection<Card>) = cards.map { it.note() }.toSet()

/**
* @return All cards of all notes
Expand Down
8 changes: 4 additions & 4 deletions AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
private var mAllModelIds: ArrayList<Long>? = null
@KotlinCleanup("this ideally should be Int, Int?")
private var mModelChangeFieldMap: MutableMap<Int, Int>? = null
private var mModelChangeCardMap: HashMap<Int, Int?>? = null
private var mModelChangeCardMap: MutableMap<Int, Int?>? = null
private val mCustomViewIds = ArrayList<Int>()

/* indicates if a new note is added or a card is edited */
Expand Down Expand Up @@ -1735,7 +1735,7 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
private val toolbarButtons: ArrayList<CustomToolbarButton>
get() {
val set = AnkiDroidApp.getSharedPrefs(this)
.getStringSet(PREF_NOTE_EDITOR_CUSTOM_BUTTONS, HashUtil.HashSetInit(0))
.getStringSet(PREF_NOTE_EDITOR_CUSTOM_BUTTONS, setOf())
return CustomToolbarButton.fromStringSet(set!!)
}

Expand Down Expand Up @@ -1989,13 +1989,13 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
}
// Initialize mapping between fields of old model -> new model
val itemsLength = mEditorNote!!.items().size
mModelChangeFieldMap = HashUtil.HashMapInit(itemsLength)
mModelChangeFieldMap = HashMapInit(itemsLength)
for (i in 0 until itemsLength) {
mModelChangeFieldMap!![i] = i
}
// Initialize mapping between cards new model -> old model
val templatesLength = tmpls.length()
mModelChangeCardMap = HashUtil.HashMapInit(templatesLength)
mModelChangeCardMap = HashMapInit(templatesLength)
for (i in 0 until templatesLength) {
if (i < mEditorNote!!.numberOfCards()) {
mModelChangeCardMap!![i] = i
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import com.ichi2.libanki.Consts.DYN_PRIORITY
import com.ichi2.libanki.Deck
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.backend.exception.DeckRenameException
import com.ichi2.utils.HashUtil.HashMapInit
import com.ichi2.utils.JSONArray
import com.ichi2.utils.JSONObject
import com.ichi2.utils.KotlinCleanup
Expand Down Expand Up @@ -157,7 +156,7 @@ class CustomStudyDialog(private val collection: Collection, private val customSt
}

@KotlinCleanup("make this use enum instead of Int")
fun getValuesFromKeys(map: HashMap<Int, String>, keys: IntArray): Array<String?> {
fun getValuesFromKeys(map: Map<Int, String>, keys: IntArray): Array<String?> {
val values = arrayOfNulls<String>(keys.size)
for (i in keys.indices) {
values[i] = map[keys[i]]
Expand Down Expand Up @@ -292,21 +291,21 @@ class CustomStudyDialog(private val collection: Collection, private val customSt
return dialog
}

private val keyValueMap: HashMap<Int, String>
private val keyValueMap: Map<Int, String>
get() {
val res = resources
val keyValueMap = HashMapInit<Int, String>(10)
keyValueMap[STANDARD.value] = res.getString(R.string.custom_study)
keyValueMap[STUDY_NEW.value] = res.getString(R.string.custom_study_increase_new_limit)
keyValueMap[STUDY_REV.value] = res.getString(R.string.custom_study_increase_review_limit)
keyValueMap[STUDY_FORGOT.value] = res.getString(R.string.custom_study_review_forgotten)
keyValueMap[STUDY_AHEAD.value] = res.getString(R.string.custom_study_review_ahead)
keyValueMap[STUDY_RANDOM.value] = res.getString(R.string.custom_study_random_selection)
keyValueMap[STUDY_PREVIEW.value] = res.getString(R.string.custom_study_preview_new)
keyValueMap[STUDY_TAGS.value] = res.getString(R.string.custom_study_limit_tags)
keyValueMap[DECK_OPTIONS.value] = res.getString(R.string.menu__deck_options)
keyValueMap[MORE_OPTIONS.value] = res.getString(R.string.more_options)
return keyValueMap
return mapOf(
STANDARD.value to res.getString(R.string.custom_study),
STUDY_NEW.value to res.getString(R.string.custom_study_increase_new_limit),
STUDY_REV.value to res.getString(R.string.custom_study_increase_review_limit),
STUDY_FORGOT.value to res.getString(R.string.custom_study_review_forgotten),
STUDY_AHEAD.value to res.getString(R.string.custom_study_review_ahead),
STUDY_RANDOM.value to res.getString(R.string.custom_study_random_selection),
STUDY_PREVIEW.value to res.getString(R.string.custom_study_preview_new),
STUDY_TAGS.value to res.getString(R.string.custom_study_limit_tags),
DECK_OPTIONS.value to res.getString(R.string.menu__deck_options),
MORE_OPTIONS.value to res.getString(R.string.more_options)
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.ichi2.anki.noteeditor
import android.text.TextUtils
import com.ichi2.anki.noteeditor.Toolbar.TextWrapper
import com.ichi2.libanki.Consts
import com.ichi2.utils.HashUtil.HashSetInit
import timber.log.Timber
import java.util.*

Expand Down Expand Up @@ -63,16 +62,7 @@ class CustomToolbarButton(var index: Int, var buttonText: ButtonText, val prefix
return buttons
}

fun toStringSet(buttons: ArrayList<CustomToolbarButton>): Set<String> {
val ret = HashSetInit<String>(buttons.size)
for (b in buttons) {
val values = arrayOf(b.index.toString(), b.buttonText, b.prefix, b.suffix)
for (i in values.indices) {
values[i] = values[i].replace(Consts.FIELD_SEPARATOR, "")
}
ret.add(TextUtils.join(Consts.FIELD_SEPARATOR, values))
}
return ret
}
fun toStringSet(buttons: ArrayList<CustomToolbarButton>) =
buttons.map { b -> TextUtils.join(Consts.FIELD_SEPARATOR, listOf(b.index.toString(), b.buttonText, b.prefix, b.suffix).map { it.replace(Consts.FIELD_SEPARATOR, "") }) }.toSet()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import android.view.MenuItem
import androidx.annotation.IdRes
import com.ichi2.anki.R
import com.ichi2.themes.Themes
import com.ichi2.utils.HashUtil.HashMapInit
import com.ichi2.utils.HashMapInit
import timber.log.Timber

// loads of unboxing issues, which are safe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import android.text.TextUtils
import com.ichi2.anki.AnkiDroidApp
import com.ichi2.anki.AnkiFont
import com.ichi2.libanki.Utils
import com.ichi2.utils.HashUtil.HashMapInit

class ReviewerCustomFonts(context: Context) {
private val mCustomStyle: String
Expand All @@ -37,7 +36,7 @@ class ReviewerCustomFonts(context: Context) {
*
* @return the default font style, or the empty string if no default font is set
*/
private fun getDefaultFontStyle(context: Context, customFontsMap: Map<String?, AnkiFont>): String? {
private fun getDefaultFontStyle(context: Context, customFontsMap: Map<String, AnkiFont>): String? {
if (mDefaultFontStyle == null) {
val preferences = AnkiDroidApp.getSharedPrefs(context)
val defaultFont = customFontsMap[preferences.getString("defaultFont", null)]
Expand All @@ -56,7 +55,7 @@ class ReviewerCustomFonts(context: Context) {
*
* @return the override font style, or the empty string if no override font is set
*/
private fun getOverrideFontStyle(context: Context, customFontsMap: Map<String?, AnkiFont>): String? {
private fun getOverrideFontStyle(context: Context, customFontsMap: Map<String, AnkiFont>): String? {
if (mOverrideFontStyle == null) {
val preferences = AnkiDroidApp.getSharedPrefs(context)
val defaultFont = customFontsMap[preferences.getString("defaultFont", null)]
Expand All @@ -76,7 +75,7 @@ class ReviewerCustomFonts(context: Context) {
*
* @return the font style, or the empty string if none applies
*/
private fun getDominantFontStyle(context: Context, customFontsMap: Map<String?, AnkiFont>): String? {
private fun getDominantFontStyle(context: Context, customFontsMap: Map<String, AnkiFont>): String? {
if (mDominantFontStyle == null) {
mDominantFontStyle = getOverrideFontStyle(context, customFontsMap)
if (TextUtils.isEmpty(mDominantFontStyle)) {
Expand Down Expand Up @@ -104,7 +103,7 @@ class ReviewerCustomFonts(context: Context) {
* <p>
* Each font is mapped to the font family by the same name as the name of the font without the extension.
*/
private fun getCustomFontsStyle(customFontsMap: Map<String?, AnkiFont>): String {
private fun getCustomFontsStyle(customFontsMap: Map<String, AnkiFont>): String {
val builder = StringBuilder()
for (font in customFontsMap.values) {
builder.append(font.declaration)
Expand All @@ -118,14 +117,7 @@ class ReviewerCustomFonts(context: Context) {
* <p>
* The list of constructed lazily the first time is needed.
*/
private fun getCustomFontsMap(context: Context): Map<String?, AnkiFont> {
val fonts = Utils.getCustomFonts(context)
val customFontsMap: MutableMap<String?, AnkiFont> = HashMapInit(fonts.size)
for (f in fonts) {
customFontsMap[f.name] = f
}
return customFontsMap
}
private fun getCustomFontsMap(context: Context): Map<String, AnkiFont> = Utils.getCustomFonts(context).associateBy { it.name }
}

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import com.ichi2.anki.reviewer.MappableBinding
import com.ichi2.anki.web.CustomSyncServer
import com.ichi2.libanki.Consts
import com.ichi2.themes.Themes
import com.ichi2.utils.HashUtil.HashSetInit
import timber.log.Timber

private typealias VersionIdentifier = Int
Expand Down Expand Up @@ -215,7 +214,7 @@ object PreferenceUpgradeService {

private fun getNewToolbarButtons(preferences: SharedPreferences): ArrayList<CustomToolbarButton> {
// get old toolbar prefs
val set = preferences.getStringSet("note_editor_custom_buttons", HashSetInit<String>(0)) as Set<String?>
val set = preferences.getStringSet("note_editor_custom_buttons", setOf()) as Set<String?>
// new list with buttons size
val buttons = ArrayList<CustomToolbarButton>(set.size)

Expand Down
26 changes: 10 additions & 16 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Collection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import com.ichi2.libanki.utils.Time
import com.ichi2.libanki.utils.TimeManager
import com.ichi2.upgrade.Upgrade
import com.ichi2.utils.*
import com.ichi2.utils.HashMapInit
import net.ankiweb.rsdroid.Backend
import net.ankiweb.rsdroid.RustCleanup
import org.jetbrains.annotations.Contract
Expand Down Expand Up @@ -768,11 +769,11 @@ open class Collection(
): ArrayList<Long>? where T : ProgressSender<Int>?, T : CancelListener? {
val nbCount = noteCount()
// For each note, indicates ords of cards it contains
val have = HashUtil.HashMapInit<Long, HashMap<Int, Long>>(nbCount)
val have = HashMapInit<Long, HashMap<Int, Long>>(nbCount)
// For each note, the deck containing all of its cards, or 0 if siblings in multiple deck
val dids = HashUtil.HashMapInit<Long, Long>(nbCount)
val dids = HashMapInit<Long, Long>(nbCount)
// For each note, an arbitrary due of one of its due card processed, if any exists
val dues = HashUtil.HashMapInit<Long, Long>(nbCount)
val dues = HashMapInit<Long, Long>(nbCount)
var nodes: List<ParsedNode?>? = null
if (model.getInt("type") != Consts.MODEL_CLOZE) {
nodes = model.parsedNodes()
Expand Down Expand Up @@ -1078,7 +1079,7 @@ open class Collection(
tags: String,
flist: Array<String>,
flags: Int
): HashMap<String, String> {
): Map<String, String> {
return _renderQA(cid, model, did, ord, tags, flist, flags, false, null, null)
}

Expand All @@ -1094,17 +1095,13 @@ open class Collection(
browser: Boolean,
qfmtParam: String?,
afmtParam: String?
): HashMap<String, String> {
): Map<String, String> {
// data is [cid, nid, mid, did, ord, tags, flds, cardFlags]
// unpack fields and create dict
var qfmt = qfmtParam
var afmt = afmtParam
val fmap = Models.fieldMap(model)
val maps: Set<Map.Entry<String, Pair<Int, JSONObject>>> = fmap.entries
val fields: MutableMap<String, String> = HashUtil.HashMapInit(maps.size + 8)
for ((key, value) in maps) {
fields[key] = flist[value.first]
}
val fields: MutableMap<String, String> = fmap.mapValues { flist[it.value.first] }.toMutableMap()
val cardNum = ord + 1
fields["Tags"] = tags.trim { it <= ' ' }
fields["Type"] = model.getString("name")
Expand All @@ -1120,7 +1117,7 @@ open class Collection(
fields["Card"] = template.getString("name")
fields[String.format(Locale.US, "c%d", cardNum)] = "1"
// render q & a
val d = HashUtil.HashMapInit<String, String>(2)
val d = HashMapInit<String, String>(2)
d["id"] = cid.toString()
qfmt = if (qfmt.isNullOrEmpty()) template.getString("qfmt") else qfmt
afmt = if (afmt.isNullOrEmpty()) template.getString("afmt") else afmt
Expand Down Expand Up @@ -1390,7 +1387,7 @@ open class Collection(
} else {
c.did
}
val qa: HashMap<String, String> = if (browser) {
val qa: Map<String, String> = if (browser) {
val bqfmt = t.getString("bqfmt")
val bafmt = t.getString("bafmt")
_renderQA(
Expand Down Expand Up @@ -1694,10 +1691,7 @@ open class Collection(

// obtain a list of all valid dconf IDs
val allConf = decks.allConf()
val configIds = HashUtil.HashSetInit<Long>(allConf.size)
for (conf in allConf) {
configIds.add(conf.getLong("id"))
}
val configIds = allConf.map { conf -> conf.getLong("id") }.toSet()
notifyProgress.run()
@KotlinCleanup("use count { }")
var changed = 0
Expand Down
29 changes: 10 additions & 19 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Decks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ import com.ichi2.libanki.backend.exception.DeckRenameException
import com.ichi2.libanki.utils.TimeManager.time
import com.ichi2.utils.*
import com.ichi2.utils.CollectionUtils.addAll
import com.ichi2.utils.HashUtil.HashMapInit
import com.ichi2.utils.HashMapInit
import net.ankiweb.rsdroid.RustCleanup
import org.intellij.lang.annotations.Language
import timber.log.Timber
import java.text.Normalizer
import java.util.*
import java.util.regex.Pattern
import kotlin.collections.HashMap

// fixmes:
// - make sure users can't set grad interval < 1
Expand All @@ -51,9 +52,9 @@ class Decks(private val col: Collection) : DeckManager() {
@get:RustCleanup("This exists in Rust as DecksDictProxy, but its usage is warned against")
@KotlinCleanup("lateinit")
@get:VisibleForTesting
var decks: HashMap<Long, Deck>? = null
var decks: MutableMap<Long, Deck>? = null
private set
private var mDconf: HashMap<Long, DeckConfig>? = null
private var mDconf: MutableMap<Long, DeckConfig>? = null

// Never access mNameMap directly. Uses byName
@KotlinCleanup("lateinit (it's set in load)")
Expand All @@ -66,7 +67,7 @@ class Decks(private val col: Collection) : DeckManager() {
*/
@KotlinCleanup("nullability")
private class NameMap private constructor(size: Int) {
private val mNameMap: HashMap<String?, Deck>
private val mNameMap: MutableMap<String?, Deck>

/**
* @param name A name of deck to get
Expand Down Expand Up @@ -150,24 +151,14 @@ class Decks(private val col: Collection) : DeckManager() {
override fun load(@Language("JSON") decks: String, dconf: String) {
val decksarray = JSONObject(decks)
var ids = decksarray.names()
this.decks = HashMapInit(decksarray.length())
if (ids != null) {
for (id in ids.stringIterable()) {
val o = Deck(decksarray.getJSONObject(id))
val longId = id.toLong()
this.decks!!.put(longId, o)
}
}
this.decks =
ids?.stringIterable()?.associate { id -> id.toLong() to Deck(decksarray.getJSONObject(id)) }
?.toMutableMap()
?: HashMap()
mNameMap = NameMap.constructor(this.decks!!.values)
val confarray = JSONObject(dconf)
ids = confarray.names()
mDconf = HashMapInit(confarray.length())
if (ids != null) {
for (id in ids.stringIterable()) {
mDconf!![id.toLong()] =
DeckConfig(confarray.getJSONObject(id), DeckConfig.Source.DECK_CONFIG)
}
}
mDconf = ids?.stringIterable()?.associate { id -> id.toLong() to DeckConfig(confarray.getJSONObject(id), DeckConfig.Source.DECK_CONFIG) }?.toMutableMap() ?: HashMap()
mChanged = false
}

Expand Down
Loading