@@ -99,8 +99,8 @@ open class OudsAnnotatedString<T> internal constructor(annotatedString: Annotate
9999 *
100100 * @see AnnotatedString.plus
101101 */
102- operator fun plus (other : OudsAnnotatedString < T > ): OudsAnnotatedString < T > {
103- return OudsAnnotatedString (_annotatedString .plus(other._annotatedString ))
102+ operator fun plus (other : T ): T {
103+ return createInstance (_annotatedString .plus(other._annotatedString ))
104104 }
105105
106106 override fun equals (other : Any? ): Boolean {
@@ -128,7 +128,7 @@ open class OudsAnnotatedString<T> internal constructor(annotatedString: Annotate
128128 * If empty locale list is passed, use the current locale instead.
129129 * @return An uppercase transformed string.
130130 */
131- fun toUpperCase (localeList : LocaleList = LocaleList .current): OudsAnnotatedString < T > = OudsAnnotatedString (_annotatedString .toUpperCase(localeList))
131+ fun toUpperCase (localeList : LocaleList = LocaleList .current): T = createInstance (_annotatedString .toUpperCase(localeList))
132132
133133 /* *
134134 * Create lower case transformed [OudsAnnotatedString].
@@ -144,7 +144,7 @@ open class OudsAnnotatedString<T> internal constructor(annotatedString: Annotate
144144 * If empty locale list is passed, use the current locale instead.
145145 * @return A lowercase transformed string.
146146 */
147- fun toLowerCase (localeList : LocaleList = LocaleList .current): OudsAnnotatedString < T > = OudsAnnotatedString (_annotatedString .toLowerCase(localeList))
147+ fun toLowerCase (localeList : LocaleList = LocaleList .current): T = createInstance (_annotatedString .toLowerCase(localeList))
148148
149149 /* *
150150 * Create capitalized [OudsAnnotatedString].
@@ -161,7 +161,7 @@ open class OudsAnnotatedString<T> internal constructor(annotatedString: Annotate
161161 * currently ignored since underlying Kotlin method is experimental.
162162 * @return A capitalized string.
163163 */
164- fun capitalize (localeList : LocaleList = LocaleList .current): OudsAnnotatedString < T > = OudsAnnotatedString (_annotatedString .capitalize(localeList))
164+ fun capitalize (localeList : LocaleList = LocaleList .current): T = createInstance (_annotatedString .capitalize(localeList))
165165
166166 /* *
167167 * Create decapitalized [OudsAnnotatedString].
@@ -178,7 +178,12 @@ open class OudsAnnotatedString<T> internal constructor(annotatedString: Annotate
178178 * locale is currently ignored since underlying Kotlin method is experimental.
179179 * @return A decapitalized string.
180180 */
181- fun decapitalize (localeList : LocaleList = LocaleList .current): OudsAnnotatedString <T > = OudsAnnotatedString (_annotatedString .decapitalize(localeList))
181+ fun decapitalize (localeList : LocaleList = LocaleList .current): T = createInstance(_annotatedString .decapitalize(localeList))
182+
183+ private fun createInstance (annotatedString : AnnotatedString ): T {
184+ @Suppress(" UNCHECKED_CAST" )
185+ return createOudsAnnotatedString(annotatedString, this ::class .java as Class <T >)
186+ }
182187
183188 @Composable
184189 private fun getTextLinkStyles (linkStyle : TextStyle ): TextLinkStyles {
@@ -279,8 +284,7 @@ open class OudsAnnotatedString<T> internal constructor(annotatedString: Annotate
279284 * @return The constructed annotated string.
280285 */
281286 open fun toAnnotatedString (): T {
282- val constructor : (AnnotatedString ) -> T = { clazz.getConstructor(AnnotatedString ::class .java).newInstance(it) }
283- return constructor (builder.toAnnotatedString())
287+ return createOudsAnnotatedString(builder.toAnnotatedString(), clazz)
284288 }
285289
286290 protected fun addStrongImpl (start : Int , end : Int ) {
@@ -481,3 +485,8 @@ internal fun <T, U> buildOudsAnnotatedString(
481485 val constructor : () -> T = { builderClass.getConstructor().newInstance() }
482486 return constructor ().apply { builder() }.toAnnotatedString()
483487}
488+
489+ private fun <T > createOudsAnnotatedString (annotatedString : AnnotatedString , clazz : Class <T >): T where T : OudsAnnotatedString <T > {
490+ val constructor : (AnnotatedString ) -> T = { clazz.getConstructor(AnnotatedString ::class .java).newInstance(it) }
491+ return constructor (annotatedString)
492+ }
0 commit comments