musikr: additional api cleanup
This commit is contained in:
parent
47d5184e8d
commit
5a65a6aa25
6 changed files with 35 additions and 57 deletions
|
@ -31,7 +31,7 @@ internal sealed interface Divert<L, R> {
|
||||||
data class Right<L, R>(val value: R) : Divert<L, R>
|
data class Right<L, R>(val value: R) : Divert<L, R>
|
||||||
}
|
}
|
||||||
|
|
||||||
class DivertedFlow<L, R>(val manager: Flow<Nothing>, val left: Flow<L>, val right: Flow<R>)
|
internal class DivertedFlow<L, R>(val manager: Flow<Nothing>, val left: Flow<L>, val right: Flow<R>)
|
||||||
|
|
||||||
internal inline fun <T, L, R> Flow<T>.divert(
|
internal inline fun <T, L, R> Flow<T>.divert(
|
||||||
crossinline predicate: (T) -> Divert<L, R>
|
crossinline predicate: (T) -> Divert<L, R>
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
|
|
||||||
package org.oxycblt.musikr.tag
|
package org.oxycblt.musikr.tag
|
||||||
|
|
||||||
import org.oxycblt.musikr.tag.interpret.Token
|
import java.text.CollationKey
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The name of a music item.
|
* The name of a music item.
|
||||||
|
@ -66,6 +66,36 @@ sealed interface Name : Comparable<Name> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** An individual part of a name string that can be compared intelligently. */
|
||||||
|
data class Token(internal val collationKey: CollationKey, internal val type: Type) : Comparable<Token> {
|
||||||
|
val value: String get() = collationKey.sourceString
|
||||||
|
|
||||||
|
override fun compareTo(other: Token): Int {
|
||||||
|
// Numeric tokens should always be lower than lexicographic tokens.
|
||||||
|
val modeComp = type.compareTo(other.type)
|
||||||
|
if (modeComp != 0) {
|
||||||
|
return modeComp
|
||||||
|
}
|
||||||
|
|
||||||
|
// Numeric strings must be ordered by magnitude, thus immediately short-circuit
|
||||||
|
// the comparison if the lengths do not match.
|
||||||
|
if (type == Type.NUMERIC &&
|
||||||
|
collationKey.sourceString.length != other.collationKey.sourceString.length) {
|
||||||
|
return collationKey.sourceString.length - other.collationKey.sourceString.length
|
||||||
|
}
|
||||||
|
|
||||||
|
return collationKey.compareTo(other.collationKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Denotes the type of comparison to be performed with this token. */
|
||||||
|
enum class Type {
|
||||||
|
/** Compare as a digit string, like "65". */
|
||||||
|
NUMERIC,
|
||||||
|
/** Compare as a standard alphanumeric string, like "65daysofstatic" */
|
||||||
|
LEXICOGRAPHIC
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
enum class Placeholder {
|
enum class Placeholder {
|
||||||
ALBUM,
|
ALBUM,
|
||||||
ARTIST,
|
ARTIST,
|
||||||
|
|
|
@ -22,6 +22,7 @@ import java.text.CollationKey
|
||||||
import java.text.Collator
|
import java.text.Collator
|
||||||
import org.oxycblt.musikr.tag.Name
|
import org.oxycblt.musikr.tag.Name
|
||||||
import org.oxycblt.musikr.tag.Placeholder
|
import org.oxycblt.musikr.tag.Placeholder
|
||||||
|
import org.oxycblt.musikr.tag.Token
|
||||||
|
|
||||||
abstract class Naming {
|
abstract class Naming {
|
||||||
fun name(raw: String?, sort: String?, placeholder: Placeholder): Name =
|
fun name(raw: String?, sort: String?, placeholder: Placeholder): Name =
|
||||||
|
|
|
@ -1,51 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2024 Auxio Project
|
|
||||||
* Token.kt is part of Auxio.
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.oxycblt.musikr.tag.interpret
|
|
||||||
|
|
||||||
import java.text.CollationKey
|
|
||||||
|
|
||||||
/** An individual part of a name string that can be compared intelligently. */
|
|
||||||
data class Token(internal val collationKey: CollationKey, internal val type: Type) : Comparable<Token> {
|
|
||||||
val value: String get() = collationKey.sourceString
|
|
||||||
|
|
||||||
override fun compareTo(other: Token): Int {
|
|
||||||
// Numeric tokens should always be lower than lexicographic tokens.
|
|
||||||
val modeComp = type.compareTo(other.type)
|
|
||||||
if (modeComp != 0) {
|
|
||||||
return modeComp
|
|
||||||
}
|
|
||||||
|
|
||||||
// Numeric strings must be ordered by magnitude, thus immediately short-circuit
|
|
||||||
// the comparison if the lengths do not match.
|
|
||||||
if (type == Type.NUMERIC &&
|
|
||||||
collationKey.sourceString.length != other.collationKey.sourceString.length) {
|
|
||||||
return collationKey.sourceString.length - other.collationKey.sourceString.length
|
|
||||||
}
|
|
||||||
|
|
||||||
return collationKey.compareTo(other.collationKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Denotes the type of comparison to be performed with this token. */
|
|
||||||
enum class Type {
|
|
||||||
/** Compare as a digit string, like "65". */
|
|
||||||
NUMERIC,
|
|
||||||
/** Compare as a standard alphanumeric string, like "65daysofstatic" */
|
|
||||||
LEXICOGRAPHIC
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -20,6 +20,7 @@ package org.oxycblt.musikr.tag.parse
|
||||||
|
|
||||||
import org.oxycblt.musikr.fs.DeviceFile
|
import org.oxycblt.musikr.fs.DeviceFile
|
||||||
import org.oxycblt.musikr.metadata.Metadata
|
import org.oxycblt.musikr.metadata.Metadata
|
||||||
|
import org.oxycblt.musikr.util.unlikelyToBeNull
|
||||||
|
|
||||||
internal interface TagParser {
|
internal interface TagParser {
|
||||||
fun parse(file: DeviceFile, metadata: Metadata): ParsedTags
|
fun parse(file: DeviceFile, metadata: Metadata): ParsedTags
|
||||||
|
@ -29,8 +30,6 @@ internal interface TagParser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MissingTagError(what: String) : Error("missing tag: $what")
|
|
||||||
|
|
||||||
private data object TagParserImpl : TagParser {
|
private data object TagParserImpl : TagParser {
|
||||||
override fun parse(file: DeviceFile, metadata: Metadata): ParsedTags {
|
override fun parse(file: DeviceFile, metadata: Metadata): ParsedTags {
|
||||||
return ParsedTags(
|
return ParsedTags(
|
||||||
|
@ -38,7 +37,7 @@ private data object TagParserImpl : TagParser {
|
||||||
replayGainTrackAdjustment = metadata.replayGainTrackAdjustment(),
|
replayGainTrackAdjustment = metadata.replayGainTrackAdjustment(),
|
||||||
replayGainAlbumAdjustment = metadata.replayGainAlbumAdjustment(),
|
replayGainAlbumAdjustment = metadata.replayGainAlbumAdjustment(),
|
||||||
musicBrainzId = metadata.musicBrainzId(),
|
musicBrainzId = metadata.musicBrainzId(),
|
||||||
name = metadata.name() ?: file.path.name ?: throw MissingTagError("name"),
|
name = metadata.name() ?: unlikelyToBeNull(file.path.name),
|
||||||
sortName = metadata.sortName(),
|
sortName = metadata.sortName(),
|
||||||
track = metadata.track(),
|
track = metadata.track(),
|
||||||
disc = metadata.disc(),
|
disc = metadata.disc(),
|
||||||
|
|
|
@ -22,7 +22,6 @@ import org.junit.Assert.assertEquals
|
||||||
import org.junit.Assert.assertNotEquals
|
import org.junit.Assert.assertNotEquals
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.oxycblt.musikr.tag.interpret.Naming
|
import org.oxycblt.musikr.tag.interpret.Naming
|
||||||
import org.oxycblt.musikr.tag.interpret.Token
|
|
||||||
|
|
||||||
class NameTest {
|
class NameTest {
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in a new issue