Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache Locale#{hashCode,toLanguageTag} #391

Merged
merged 2 commits into from
Sep 28, 2022
Merged
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
82 changes: 47 additions & 35 deletions core/shared/src/main/scala-2/java/util/Locale.scala
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,9 @@ class Locale private[util] (
private[this] val supportSpecialCases: Boolean = true
) {

private[this] var cachedLanguageTag: String = null
private[this] var cachedHashCode = 0

// Required by the javadocs
if (language == null || country == null || variant == null)
throw new NullPointerException("Null argument to constructor not allowed")
Expand Down Expand Up @@ -563,41 +566,46 @@ class Locale private[util] (
}

def toLanguageTag(): String = {
val language =
if (getLanguage().nonEmpty && Locale.checkLanguage(getLanguage()))
updateSpecialLanguages(getLanguage())
else
"und"
val country =
if (Locale.checkRegion(getCountry())) s"-${getCountry()}"
else ""
val variantSegments = getVariant().split("-|_")
val allSegmentsWellFormed =
variantSegments.forall(Locale.checkVariantSegment)
val allAcceptableSegments =
variantSegments.forall(Locale.checkAcceptableVariantSegment)
val variant =
if (allSegmentsWellFormed)
variantSegments.mkString("-", "-", "")
else if (allAcceptableSegments) {
val (wellFormed, acceptable) =
variantSegments.partition(Locale.checkVariantSegment)
val pre =
if (wellFormed.nonEmpty) wellFormed.take(1).mkString("-", "-", "-")
else "-"
pre + "x-lvariant" +
(acceptable ++ wellFormed.drop(1)).mkString("-", "-", "")
} else
""
val ext =
if (extensions.nonEmpty)
extensions.map { case (x, v) => s"$x-$v" }.mkString("-", "-", "")
else
""
val script = this.script.map(_ => s"-${getScript()}").getOrElse("")
if (cachedLanguageTag == null) {
val language =
if (getLanguage().nonEmpty && Locale.checkLanguage(getLanguage()))
updateSpecialLanguages(getLanguage())
else
"und"
val country =
if (Locale.checkRegion(getCountry())) s"-${getCountry()}"
else ""
val variantSegments = getVariant().split("-|_")
val allSegmentsWellFormed =
variantSegments.forall(Locale.checkVariantSegment)
val allAcceptableSegments =
variantSegments.forall(Locale.checkAcceptableVariantSegment)
val variant =
if (allSegmentsWellFormed)
variantSegments.mkString("-", "-", "")
else if (allAcceptableSegments) {
val (wellFormed, acceptable) =
variantSegments.partition(Locale.checkVariantSegment)
val pre =
if (wellFormed.nonEmpty) wellFormed.take(1).mkString("-", "-", "-")
else "-"
pre + "x-lvariant" +
(acceptable ++ wellFormed.drop(1)).mkString("-", "-", "")
} else
""
val ext =
if (extensions.nonEmpty)
extensions.map { case (x, v) => s"$x-$v" }.mkString("-", "-", "")
else
""
val script = this.script.map(_ => s"-${getScript()}").getOrElse("")

cachedLanguageTag =
if (getLanguage() == "no" && getCountry() == "NO" && getVariant() == "NY") "nn-NO"
else s"$language$script$country$ext$variant"
}

if (getLanguage() == "no" && getCountry() == "NO" && getVariant() == "NY") "nn-NO"
else s"$language$script$country$ext$variant"
cachedLanguageTag
}

def getISO3Country(): String =
Expand Down Expand Up @@ -663,5 +671,9 @@ class Locale private[util] (
case _ => false
}

override def hashCode(): Int = toLanguageTag().hashCode
override def hashCode(): Int = {
if (cachedHashCode == 0)
cachedHashCode = toLanguageTag().hashCode
cachedHashCode
}
}
87 changes: 48 additions & 39 deletions core/shared/src/main/scala-3/java/util/Locale.scala
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,9 @@ class Locale private[util] (
private[this] val supportSpecialCases: Boolean = true
) {

private[this] var cachedLanguageTag: String = null
private[this] var cachedHashCode = 0

// Required by the javadocs
if (language == null || country == null || variant == null)
throw new NullPointerException("Null argument to constructor not allowed")
Expand Down Expand Up @@ -549,44 +552,47 @@ class Locale private[util] (
}

def toLanguageTag(): String =
val language =
if (getLanguage().nonEmpty && Locale.checkLanguage(getLanguage()))
updateSpecialLanguages(getLanguage())
else
"und"

val country =
if (Locale.checkRegion(getCountry())) s"-${getCountry()}"
else ""
val variantSegments = getVariant().split("-|_")
val allSegmentsWellFormed =
variantSegments.forall(Locale.checkVariantSegment)
val allAcceptableSegments =
variantSegments.forall(Locale.checkAcceptableVariantSegment)
val variant =
if (allSegmentsWellFormed)
variantSegments.mkString("-", "-", "")
else if (allAcceptableSegments) {
val (wellFormed, acceptable) =
variantSegments.partition(Locale.checkVariantSegment)
val pre =
if (wellFormed.nonEmpty) wellFormed.take(1).mkString("-", "-", "-")
else "-"
pre + "x-lvariant" +
(acceptable ++ wellFormed.drop(1)).mkString("-", "-", "")
} else
""

val ext =
if (extensions.nonEmpty)
extensions.map { case (x, v) => s"$x-$v" }.mkString("-", "-", "")
else
""

val script = this.script.map(_ => s"-${getScript()}").getOrElse("")

if (getLanguage() == "no" && getCountry() == "NO" && getVariant() == "NY") "nn-NO"
else s"$language$script$country$ext$variant"
if (cachedLanguageTag == null) {
val language =
if (getLanguage().nonEmpty && Locale.checkLanguage(getLanguage()))
updateSpecialLanguages(getLanguage())
else
"und"

val country =
if (Locale.checkRegion(getCountry())) s"-${getCountry()}"
else ""
val variantSegments = getVariant().split("-|_")
val allSegmentsWellFormed =
variantSegments.forall(Locale.checkVariantSegment)
val allAcceptableSegments =
variantSegments.forall(Locale.checkAcceptableVariantSegment)
val variant =
if (allSegmentsWellFormed)
variantSegments.mkString("-", "-", "")
else if (allAcceptableSegments) {
val (wellFormed, acceptable) =
variantSegments.partition(Locale.checkVariantSegment)
val pre =
if (wellFormed.nonEmpty) wellFormed.take(1).mkString("-", "-", "-")
else "-"
pre + "x-lvariant" +
(acceptable ++ wellFormed.drop(1)).mkString("-", "-", "")
} else
""

val ext =
if (extensions.nonEmpty)
extensions.map { case (x, v) => s"$x-$v" }.mkString("-", "-", "")
else
""

val script = this.script.map(_ => s"-${getScript()}").getOrElse("")

cachedLanguageTag = if (getLanguage() == "no" && getCountry() == "NO" && getVariant() == "NY") "nn-NO"
else s"$language$script$country$ext$variant"
}
cachedLanguageTag
end toLanguageTag

def getISO3Country(): String =
Expand Down Expand Up @@ -651,5 +657,8 @@ class Locale private[util] (
case l: Locale => isEqual(l)
case _ => false

override def hashCode(): Int = toLanguageTag().hashCode
override def hashCode(): Int =
if (cachedHashCode == 0)
cachedHashCode = toLanguageTag().hashCode
cachedHashCode
}