Skip to content

Commit

Permalink
feat: #98 Started generalization of the NonStandardVocabularyTopExpor…
Browse files Browse the repository at this point in the history
…ter implementations - use minRacesCount and loggerName from cache. Excel data not yet.
  • Loading branch information
dmitry-weirdo committed Nov 27, 2024
1 parent f66734c commit 73f4594
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package ru.klavogonki.common
@Suppress("MagicNumber", "Unused")
enum class NonStandardDictionary(
@JvmField val code: String,
@JvmField val id: Int,
@JvmField val displayName: String,
@JvmField val displayNamePrepositional: String
) {
Expand Down Expand Up @@ -77,11 +78,13 @@ enum class NonStandardDictionary(
displayNamePrepositional: String
) : this(
DictionaryUtils.getDictionaryCode(code),
code,
displayName,
displayNamePrepositional
)

companion object {
@JvmStatic
fun getByDictionaryCode(code: String): NonStandardDictionary {
val values = entries.filter { it.code == code }

Expand All @@ -96,6 +99,7 @@ enum class NonStandardDictionary(
return values[0]
}

@JvmStatic
fun getByDictionaryId(dictionaryId: Int): NonStandardDictionary {
val code = DictionaryUtils.getDictionaryCode(dictionaryId)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@
public class DigitsOneHundredTopExporter extends NonStandardVocabularyTopExporterDefaultImpl {

@Override
public NonStandardDictionary vocabulary() {
return NonStandardDictionary.DIGITS_ONE_HUNDRED;
}

@Override
public int minRacesCount() {
return 100; // about 2582 users
}

@Override
public String loggerName() {
return this.getClass().getSimpleName();
public int dictionaryId() {
return NonStandardDictionary.DIGITS_ONE_HUNDRED.id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,8 @@
public class FrequencyVocabularyTopExporter extends NonStandardVocabularyTopExporterDefaultImpl {

@Override
public NonStandardDictionary vocabulary() {
return NonStandardDictionary.FREQUENCY_VOCABULARY;
}

@Override
public int minRacesCount() {
return 200; // about 2664 users
}

@Override
public String loggerName() {
return this.getClass().getSimpleName();
public int dictionaryId() {
return NonStandardDictionary.FREQUENCY_VOCABULARY.id;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,8 @@
public class MiniMarathonTopExporter extends NonStandardVocabularyTopExporterDefaultImpl {

@Override
public NonStandardDictionary vocabulary() {
return NonStandardDictionary.MINI_MARATHON;
}

@Override
public int minRacesCount() {
return 100; // about 916 users
}

@Override
public String loggerName() {
return this.getClass().getSimpleName();
public int dictionaryId() {
return NonStandardDictionary.MINI_MARATHON.id;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,70 @@
package ru.klavogonki.statistics.export.vocabulary.non_standard;

import ru.klavogonki.common.NonStandardDictionary;
import ru.klavogonki.statistics.dictionaries.NonStandardDictionariesCache;
import ru.klavogonki.statistics.dictionaries.NonStandardDictionaryData;
import ru.klavogonki.statistics.dictionaries.NonStandardDictionaryTopData;
import ru.klavogonki.statistics.export.vocabulary.VocabularyTopExporter;
import ru.klavogonki.statistics.export.vocabulary.VocabularyTopUtils;

import java.util.Objects;

public interface NonStandardVocabularyTopExporter extends VocabularyTopExporter {

@Override
default boolean isStandard() {
return false;
}

NonStandardDictionary vocabulary();
int dictionaryId();

@Deprecated(since = "use vocabularyData() instead")
default NonStandardDictionary vocabulary() {
return NonStandardDictionary.getByDictionaryId(
dictionaryId()
);
}

default NonStandardDictionaryData vocabularyData() {
int code = dictionaryId();

NonStandardDictionaryData dictionaryData = NonStandardDictionariesCache.INSTANCE.getDictionary(code);

assertDictionaryHasTopSet(dictionaryData);

return dictionaryData;
}

default NonStandardDictionaryTopData vocabularyTopData() {
return assertDictionaryHasTopSet(vocabularyData());
}

@Override
default int minRacesCount() {
return vocabularyTopData().minRacesCount;
}

@Override
default String loggerName() {
return vocabularyTopData().loggerName;
}

private static NonStandardDictionaryTopData assertDictionaryHasTopSet(NonStandardDictionaryData dictionaryData) {
Objects.requireNonNull(
dictionaryData.top,
String.format(
"Non-standard dictionary with code = %d, name = \"%s\" has no top configured.",
dictionaryData.code,
dictionaryData.displayName
)
);

return dictionaryData.top;
}

@Override
default String vocabularyCode() {
return vocabulary().code;
default String vocabularyCode() { // full code. in "voc-123" format
return vocabularyData().getFullCode();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,8 @@
public class NormalInEnglishTopExporter extends NonStandardVocabularyTopExporterDefaultImpl {

@Override
public NonStandardDictionary vocabulary() {
return NonStandardDictionary.NORMAL_IN_ENGLISH;
}

@Override
public int minRacesCount() {
return 200; // about 3248 users
}

@Override
public String loggerName() {
return this.getClass().getSimpleName();
public int dictionaryId() {
return NonStandardDictionary.NORMAL_IN_ENGLISH.id;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@
public class OneHundredRussianTopExporter extends NonStandardVocabularyTopExporterDefaultImpl {

@Override
public NonStandardDictionary vocabulary() {
return NonStandardDictionary.ONE_HUNDRED_RUSSIAN;
}

@Override
public int minRacesCount() {
return 500; // about 1984 users
}

@Override
public String loggerName() {
return this.getClass().getSimpleName();
public int dictionaryId() {
return NonStandardDictionary.ONE_HUNDRED_RUSSIAN.id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@
public class PinkiesPlusTopExporter extends NonStandardVocabularyTopExporterDefaultImpl {

@Override
public NonStandardDictionary vocabulary() {
return NonStandardDictionary.PINKIES_PLUS;
}

@Override
public int minRacesCount() {
return 100; // about 571 users
}

@Override
public String loggerName() {
return this.getClass().getSimpleName();
public int dictionaryId() {
return NonStandardDictionary.PINKIES_PLUS.id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@
public class RingFingersTopExporter extends NonStandardVocabularyTopExporterDefaultImpl {

@Override
public NonStandardDictionary vocabulary() {
return NonStandardDictionary.RING_FINGERS;
}

@Override
public int minRacesCount() {
return 50; // about 279 users
}

@Override
public String loggerName() {
return this.getClass().getSimpleName();
public int dictionaryId() {
return NonStandardDictionary.RING_FINGERS.id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,8 @@
public class ShortTextsTopExporter extends NonStandardVocabularyTopExporterDefaultImpl {

@Override
public NonStandardDictionary vocabulary() {
return NonStandardDictionary.SHORT_TEXTS;
}

@Override
public int minRacesCount() {
return 200; // about 2440 users
}

@Override
public String loggerName() {
return this.getClass().getSimpleName();
public int dictionaryId() {
return NonStandardDictionary.SHORT_TEXTS.id;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,8 @@
public class TrainingIndexFingersTopExporter extends NonStandardVocabularyTopExporterDefaultImpl {

@Override
public NonStandardDictionary vocabulary() {
return NonStandardDictionary.TRAINING_INDEX_FINGERS;
}

@Override
public int minRacesCount() {
return 100; // about 562 users
}

@Override
public String loggerName() {
return this.getClass().getSimpleName();
public int dictionaryId() {
return NonStandardDictionary.TRAINING_INDEX_FINGERS.id;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ object NonStandardDictionariesCache : Logging {
}

fun getDictionary(code: Int): NonStandardDictionaryData {
// todo: get it from a static map
return nonStandardDictionaries
.firstOrNull { it.code == code }
?: error("No dictionary found by code = $code.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ object NonStandardVocabularyTopExporterGenerator : Logging {
}

return object : NonStandardVocabularyTopExporterDefaultImpl() {
override fun dictionaryId() = dictionaryData.code

override fun minRacesCount() = dictionaryData.top.minRacesCount

override fun loggerName() = dictionaryData.top.loggerName

override fun vocabulary(): NonStandardDictionary {
// todo: change the parent signature to return NonStandardDictionaryData instead

return NonStandardDictionary.getByDictionaryId(dictionaryData.code)
}
// todo: think whether this override from NonStandardVocabularyTopExporter is required
override fun vocabularyData() = dictionaryData

override fun topByBestSpeedExcelSheetName() =
dictionaryData.top.topByBestSpeedExcelSheetName
Expand Down

0 comments on commit 73f4594

Please sign in to comment.