package Ġio . fluid sonic . i 1 8 n . data . generator Ċ Ċ import Ġcom . squareup . kotlin po et .* Ċ import Ġio . fluid sonic . cl dr .* Ċ import Ġio . fluid sonic . locale .* Ċ import Ġjava . nio . file .* ĊĊ Ċ public Ġobject ĠRegion Name File Generator Ġ{ ĊĊ ĉ public Ġfun Ġgenerate ( destination : ĠPath , Ġalternative : ĠC ldr Region Name Alternative ) Ġ{ Ċĉ ĉ val Ġlanguage Fun Specs Ġ= ĠIdent ifiers . language Tags Ċĉĉ ĉ . groupBy Ġ{ Ġit . language Ġ} Ċĉĉ ĉ . map NotNull Ġ{ Ġ( language , Ġtags ) Ġ-> Ċĉĉĉ ĉ generate Function For Language ( Ċĉĉĉĉ ĉ language Ġ= Ġlanguage ?. take If Ġ{ Ġit Ġ!= ĠLanguage Tag . un determin ed Prefix Ġ}, Ċĉĉĉĉ ĉ tags Ġ= Ġtags , Ċĉĉĉĉ ĉ alternative Ġ= Ġalternative Ċĉĉĉ ĉ ) Ċĉĉĉĉ ĉ ?. let Ġ{ Ġlanguage Ġto Ġit Ġ} Ċĉĉ ĉ } ĊĊĉ ĉ Files . new OutputStream ( destination ). writer (). use Ġ{ Ġwriter Ġ-> Ċĉĉ ĉ File Spec . builder ( packageName Ġ= Ġ" io . fluid sonic . i 1 8 n . data ", ĠfileName Ġ= Ġdestination . fileName . toString ()) Ċĉĉĉ ĉ . add Comment ( Generation . file Comment ) Ċĉĉĉ ĉ . addFunction ( Fun Spec . builder (" localized Name For Region _$ alternative ") Ċĉĉĉĉ ĉ . add Parameter (" query ", ĠKotlin Types . int ) Ċĉĉĉĉ ĉ . add Parameter (" language ", ĠKotlin Types . int ) Ċĉĉĉĉ ĉ . add Parameter (" script ", ĠKotlin Types . int ) Ċĉĉĉĉ ĉ . add Parameter (" region ", ĠKotlin Types . int ) Ċĉĉĉĉ ĉ . add Parameter (" variant ", ĠKotlin Types . int ) Ċĉĉĉĉ ĉ . returns ( Kotlin Types . string Nullable ) Ċĉĉĉĉ ĉ . add Code ( build Code Block Ġ{ Ċĉĉĉĉĉ ĉ begin Control Flow (" return Ġwhen Ġ( language )") Ċĉĉĉĉĉ ĉ run Ġ{ Ċĉĉĉĉĉĉ ĉ language Fun Specs . forEach Ġ{ Ġ( language , Ġfun Spec ) Ġ-> Ċĉĉĉĉĉĉĉ ĉ add ( Ċĉĉĉĉĉĉĉĉ ĉ "% L Â· -> Â· % N ( query , Â· script , Â· region , Â· variant )\ n ", Ċĉĉĉĉĉĉĉĉ ĉ Identifiers . index By Language ( language ), Ċĉĉĉĉĉĉĉĉ ĉ fun Spec . name Ċĉĉĉĉĉĉĉ ĉ ) Ċĉĉĉĉĉĉ ĉ } Ċĉĉĉĉĉĉ ĉ add (" else Ġ-> Ġnull \ n ") Ċĉĉĉĉĉ ĉ } Ċĉĉĉĉĉ ĉ end Control Flow () Ċĉĉĉĉ ĉ }) Ċĉĉĉĉ ĉ . build () Ċĉĉĉ ĉ ) Ċĉĉĉ ĉ . apply Ġ{ Ċĉĉĉĉ ĉ language Fun Specs . forEach Ġ{ Ġ(_, Ġfun Spec ) Ġ-> Ġadd Function ( fun Spec ) Ġ} Ċĉĉĉ ĉ } Ċĉĉĉ ĉ . build () Ċĉĉĉ ĉ . writeTo ( writer ) Ċĉ ĉ } Ċ ĉ } ĊĊĊ ĉ private Ġfun Ġgenerate Block For Names ( names : ĠList < Pair < String , ĠString >> ): ĠCode Block Ġ= Ġbuild Code Block Ġ{ Ċĉ ĉ begin Control Flow (" when Ġ( query )") Ċĉ ĉ run Ġ{ Ċĉĉ ĉ names . forEach Ġ{ Ġ( region Id , Ġname ) Ġ-> Ċĉĉĉ ĉ add ("% L Â· -> Â· % S Â· // Â· $ region Id \ n ", ĠIdent ifiers . index By Region ( region Id ), Ġname ) Ċĉĉ ĉ } Ċĉĉ ĉ add (" else Ġ-> Ġnull \ n ") Ċĉ ĉ } Ċĉ ĉ end Control Flow () Ċ ĉ } ĊĊĊ ĉ private Ġfun Ġgenerate Block For Region ( region : ĠString ?, Ġnames By Language Tag : ĠList < Pair < Language Tag , ĠList < Pair < String , ĠString >>>> ): ĠCode Block ? Ġ{ Ċĉ ĉ return Ġbuild Code Block Ġ{ Ċĉĉ ĉ val Ġsingle Entry Ġ= Ġnames By Language Tag . single OrNull () Ċĉĉ ĉ if Ġ( single Entry Ġ!= Ġnull ) Ġ{ Ċĉĉĉ ĉ val Ġ( tag , Ġnames ) Ġ= Ġsingle Entry ĊĊĉĉĉ ĉ add ("// Â· $ tag \ n ") Ċĉĉĉ ĉ begin Control Flow ( Ċĉĉĉĉ ĉ "% L Ġ-> Ġwhen Ġ( script Ġ== Ġ% L Ġ&& Ġvariant Ġ== Ġ% L )", Ċĉĉĉĉ ĉ Identifiers . index By Region ( region ), Ċĉĉĉĉ ĉ Identifiers . index By Script ( tag . script ), Ċĉĉĉĉ ĉ Identifiers . index By Variant ( tag . variants . single OrNull ()) Ċĉĉĉ ĉ ) Ċĉĉĉ ĉ run Ġ{ Ċĉĉĉĉ ĉ add (" true Ġ-> Ġ") Ċĉĉĉĉ ĉ add ( generate Block For Names ( names )) Ċĉĉĉĉ ĉ add (" false Ġ-> Ġnull \ n ") Ċĉĉĉ ĉ } Ċĉĉĉ ĉ end Control Flow () Ċĉĉ ĉ } Ċĉĉ ĉ else Ġ{ Ċĉĉĉ ĉ add ("// Â· region Â· = Â· ${ region Ġ?: Ġ"< none > "}\ n ") Ċĉĉĉ ĉ begin Control Flow ("% L Ġ-> Ġwhen Ġ( script )", ĠIdent ifiers . index By Region ( region )) Ċĉĉĉ ĉ run Ġ{ Ċĉĉĉĉ ĉ names By Language Tag . groupBy Ġ{ Ġit . first . script Ġ} Ċĉĉĉĉĉ ĉ . map NotNull Ġ{ Ġ( script , Ġnames By Language Tag For Script ) Ġ-> Ċĉĉĉĉĉĉ ĉ generate Block For Script ( script , Ġnames By Language Tag Ġ= Ġnames By Language Tag For Script ) Ċĉĉĉĉĉ ĉ } Ċĉĉĉĉĉ ĉ . if Empty Ġ{ Ġreturn Ġnull Ġ} Ċĉĉĉĉĉ ĉ . forEach Ġ{ Ġadd ( it ) Ġ} ĊĊĉĉĉĉ ĉ add (" else Ġ-> Ġnull \ n ") Ċĉĉĉ ĉ } Ċĉĉĉ ĉ end Control Flow () Ċĉĉ ĉ } Ċĉ ĉ } Ċ ĉ } ĊĊĊ ĉ private Ġfun Ġgenerate Block For Script ( script : ĠString ?, Ġnames By Language Tag : ĠList < Pair < Language Tag , ĠList < Pair < String , ĠString >>>> ): ĠCode Block ? Ġ{ Ċĉ ĉ return Ġbuild Code Block Ġ{ Ċĉĉ ĉ val Ġsingle Entry Ġ= Ġnames By Language Tag . single OrNull () Ċĉĉ ĉ if Ġ( single Entry Ġ!= Ġnull ) Ġ{ Ċĉĉĉ ĉ val Ġ( tag , Ġnames ) Ġ= Ġsingle Entry ĊĊĉĉĉ ĉ add ("// Â· $ tag \ n ") Ċĉĉĉ ĉ begin Control Flow ( Ċĉĉĉĉ ĉ "% L Ġ-> Ġwhen Ġ( variant Ġ== Ġ% L )", Ċĉĉĉĉ ĉ Identifiers . index By Script ( script ), Ċĉĉĉĉ ĉ Identifiers . index By Variant ( tag . variants . single OrNull ()) Ċĉĉĉ ĉ ) Ċĉĉĉ ĉ run Ġ{ Ċĉĉĉĉ ĉ add (" true Ġ-> Ġ") Ċĉĉĉĉ ĉ add ( generate Block For Names ( names )) Ċĉĉĉĉ ĉ add (" false Ġ-> Ġnull \ n ") Ċĉĉĉ ĉ } Ċĉĉĉ ĉ end Control Flow () Ċĉĉ ĉ } Ċĉĉ ĉ else Ġ{ Ċĉĉĉ ĉ add ("// Â· script Â· = Â· ${ script Ġ?: Ġ"< none > "}\ n ") Ċĉĉĉ ĉ begin Control Flow ("% L Ġ-> Ġwhen Ġ( variant )", ĠIdent ifiers . index By Script ( script )) Ċĉĉĉ ĉ run Ġ{ Ċĉĉĉĉ ĉ names By Language Tag . forEach Ġ{ Ġ( tag , Ġnames By Language For Variant ) Ġ-> Ċĉĉĉĉĉ ĉ add ("% L Ġ-> Ġ", ĠIdent ifiers . index By Variant ( tag . variants . single OrNull ())) Ċĉĉĉĉĉ ĉ add ( generate Block For Names ( names By Language For Variant )) Ċĉĉĉĉĉ ĉ add (" false Ġ-> Ġnull \ n ") Ċĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ add (" else Ġ-> Ġnull \ n ") Ċĉĉĉ ĉ } Ċĉĉĉ ĉ end Control Flow () Ċĉĉ ĉ } Ċĉ ĉ } Ċ ĉ } ĊĊĊ ĉ private Ġfun Ġgenerate Function For Language ( language : ĠString ?, Ġtags : ĠList < Language Tag >, Ġalternative : ĠC ldr Region Name Alternative ): ĠFun Spec ? Ġ{ Ċĉ ĉ val Ġnames By Language Tag Ġ= Ġtags Ċĉĉ ĉ . map NotNull Ġ{ Ġtag Ġ-> Ċĉĉĉ ĉ Identifiers . region Ids Ċĉĉĉĉ ĉ . map NotNull Ġ{ Ġregion Id Ġ-> Ċĉĉĉĉĉ ĉ region Id ?. let Ġ{ Ċĉĉĉĉĉĉ ĉ C ldr . region Name ( locale Id Ġ= Ġtag . toLocale Id (), Ġregion Id Ġ= Ġregion Id , Ġalternative Ġ= Ġalternative )?. let Ġ{ Ġregion Id Ġto Ġit Ġ} Ċĉĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ . if Empty Ġ{ Ġnull Ġ} Ċĉĉĉĉ ĉ ?. let Ġ{ Ġtag Ġto Ġit Ġ} Ċĉĉ ĉ } Ċĉĉ ĉ . if Empty Ġ{ Ġreturn Ġnull Ġ} ĊĊĉ ĉ return ĠFun Spec . builder (" localized Name For Region _${ alternative }_${ language Ġ?: Ġ" root "} ") Ċĉĉ ĉ . add Modifiers ( K Modifier . PRIVATE ) Ċĉĉ ĉ . add Parameter (" query ", ĠKotlin Types . int ) Ċĉĉ ĉ . add Parameter (" script ", ĠKotlin Types . int ) Ċĉĉ ĉ . add Parameter (" region ", ĠKotlin Types . int ) Ċĉĉ ĉ . add Parameter (" variant ", ĠKotlin Types . int ) Ċĉĉ ĉ . returns ( Kotlin Types . string Nullable ) Ċĉĉ ĉ . add Code ( build Code Block Ġ{ Ċĉĉĉ ĉ val Ġsingle Entry Ġ= Ġnames By Language Tag . single OrNull () Ċĉĉĉ ĉ if Ġ( single Entry Ġ!= Ġnull ) Ġ{ Ċĉĉĉĉ ĉ val Ġ( tag , Ġnames ) Ġ= Ġsingle Entry ĊĊĉĉĉĉ ĉ begin Control Flow ( Ċĉĉĉĉĉ ĉ " if Ġ( region Ġ!= Ġ% L Ġ|| Ġscript Ġ!= Ġ% L Ġ|| Ġvariant Ġ!= Ġ% L )", Ċĉĉĉĉĉ ĉ Identifiers . index By Region ( tag . region ), Ċĉĉĉĉĉ ĉ Identifiers . index By Script ( tag . script ), Ċĉĉĉĉĉ ĉ Identifiers . index By Variant ( tag . variants . single OrNull ()) Ċĉĉĉĉ ĉ ) Ċĉĉĉĉ ĉ run Ġ{ Ċĉĉĉĉĉ ĉ add (" return Ġnull \ n ") Ċĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ end Control Flow () ĊĊĉĉĉĉ ĉ add (" return Ġ") Ċĉĉĉĉ ĉ add ( generate Block For Names ( names )) Ċĉĉĉ ĉ } Ċĉĉĉ ĉ else Ġ{ Ċĉĉĉĉ ĉ begin Control Flow (" return Ġwhen Ġ( region )", ĠIdent ifiers . index By Language ( language )) Ċĉĉĉĉ ĉ run Ġ{ Ċĉĉĉĉĉ ĉ names By Language Tag . groupBy Ġ{ Ġit . first . region Ġ} Ċĉĉĉĉĉĉ ĉ . map NotNull Ġ{ Ġ( region , Ġnames By Language Tag For Region ) Ġ-> Ċĉĉĉĉĉĉĉ ĉ generate Block For Region ( region , Ġnames By Language Tag Ġ= Ġnames By Language Tag For Region ) Ċĉĉĉĉĉĉ ĉ } Ċĉĉĉĉĉĉ ĉ . if Empty Ġ{ Ġreturn Ġnull Ġ} Ċĉĉĉĉĉĉ ĉ . forEach Ġ{ Ġadd ( it ) Ġ} ĊĊĉĉĉĉĉ ĉ add (" else Ġ-> Ġnull \ n ") Ċĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ end Control Flow () Ċĉĉĉ ĉ } Ċĉĉ ĉ }) Ċĉĉ ĉ . build () Ċ ĉ } Ċ } Ċ