@@ -8,8 +8,9 @@ import android.widget.TextView
88import android.widget.Toast
99import java.io.Serializable
1010import java.util.*
11+ import kotlin.text.StringBuilder
1112
12- open class Text private constructor() : Serializable {
13+ open class Text : Serializable {
1314
1415 open fun isEmpty (): Boolean = true
1516 open fun getString (resources : Resources ): String? = null
@@ -45,6 +46,13 @@ open class Text private constructor() : Serializable {
4546
4647 @JvmStatic
4748 fun from (throwable : Throwable ): Text = StringText (throwable.message)
49+
50+ @JvmStatic
51+ fun from (vararg texts : Text ): Text = ArrayText (* texts)
52+
53+ @JvmStatic
54+ fun from (texts : List <Text >): Text = ArrayText (* texts.toTypedArray())
55+
4856 }
4957
5058 private class StringText internal constructor(private val string : String? ) : Text() {
@@ -65,7 +73,9 @@ open class Text private constructor() : Serializable {
6573 }
6674
6775 override fun hashCode (): Int {
68- return 31 * 17 + (string?.hashCode() ? : 0 )
76+ var result = super .hashCode()
77+ result = 31 * result + (string?.hashCode() ? : 0 )
78+ return result
6979 }
7080
7181 }
@@ -88,7 +98,9 @@ open class Text private constructor() : Serializable {
8898 }
8999
90100 override fun hashCode (): Int {
91- return 31 * 17 + stringRes
101+ var result = super .hashCode()
102+ result = 31 * result + stringRes
103+ return result
92104 }
93105
94106 }
@@ -112,13 +124,54 @@ open class Text private constructor() : Serializable {
112124 }
113125
114126 override fun hashCode (): Int {
115- var result = 31 * 17 + stringRes
127+ var result = super .hashCode() + stringRes
116128 result = 31 * result + Arrays .hashCode(formatArgs)
117129 return result
118130 }
119131
120132 }
121133
134+ private class ArrayText internal constructor(private vararg val texts : Text ): Text() {
135+
136+ override fun isEmpty (): Boolean {
137+ if (texts.isEmpty()) return false
138+ for (text in texts) {
139+ if (! text.isEmpty()) {
140+ return false
141+ }
142+ }
143+ return true
144+ }
145+
146+ override fun getString (resources : Resources ): String? {
147+ val stringBuilder = StringBuilder ()
148+ for (text in texts) {
149+ stringBuilder.append(text.getString(resources))
150+ }
151+ return stringBuilder.toString()
152+ }
153+
154+ override fun equals (other : Any? ): Boolean {
155+ if (this == = other) return true
156+ if (javaClass != other?.javaClass) return false
157+ if (! super .equals(other)) return false
158+
159+ other as ArrayText
160+
161+ if (! texts.contentEquals(other.texts)) return false
162+
163+ return true
164+ }
165+
166+ override fun hashCode (): Int {
167+ var result = super .hashCode()
168+ result = 31 * result + texts.contentHashCode()
169+ return result
170+ }
171+
172+
173+ }
174+
122175 interface StringHolder {
123176
124177 fun getStringText (): String?
@@ -135,6 +188,10 @@ fun EditText.setError(text: Text) {
135188 this .error = text.getString(this .resources)
136189}
137190
191+ fun EditText.setHint (text : Text ) {
192+ this .hint = text.getString(this .resources)
193+ }
194+
138195fun Text.applyTo (textView : TextView ) {
139196 textView.setText(this )
140197}
@@ -149,4 +206,20 @@ fun Activity.setTitle(text: Text) {
149206
150207fun Context.toast (text : Text , duration : Int = Toast .LENGTH_SHORT ): Toast {
151208 return Toast .makeText(this , text.getString(resources), duration).apply { show() }
152- }
209+ }
210+
211+ operator fun Text.plus (text : Text ) : Text {
212+ if (this is TextBuilder .BuilderText ) {
213+ return this + text
214+ }
215+ return TextBuilder .BuilderText (this ) + text
216+ }
217+
218+ operator fun Text.plus (string : String ) : Text {
219+ if (this is TextBuilder .BuilderText ) {
220+ return this + string
221+ }
222+ return TextBuilder .BuilderText (this ) + string
223+ }
224+
225+ fun String.toText () = Text .from(this )
0 commit comments