diff --git a/app/src/test/java/omega_r/com/omegatypesexample/ExampleUnitTest.kt b/app/src/test/java/omega_r/com/omegatypesexample/ExampleUnitTest.kt deleted file mode 100644 index 8dd8c19..0000000 --- a/app/src/test/java/omega_r/com/omegatypesexample/ExampleUnitTest.kt +++ /dev/null @@ -1,17 +0,0 @@ -package omega_r.com.omegatypesexample - -import org.junit.Test - -import org.junit.Assert.* - -/** - * Example local unit test, which will execute on the development machine (host). - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -class ExampleUnitTest { - @Test - fun addition_isCorrect() { - assertEquals(4, 2 + 2) - } -} diff --git a/app/src/test/java/omega_r/com/omegatypesexample/TextUnitTest.kt b/app/src/test/java/omega_r/com/omegatypesexample/TextUnitTest.kt new file mode 100644 index 0000000..021e4da --- /dev/null +++ b/app/src/test/java/omega_r/com/omegatypesexample/TextUnitTest.kt @@ -0,0 +1,45 @@ +package omega_r.com.omegatypesexample + +import com.omega_r.libs.omegatypes.Text +import org.junit.Test + +import org.junit.Assert.* + +class TextUnitTest { + @Test + fun text_equals() { + assertEquals(Text.empty(), Text.empty()) + assertNotEquals(Text.empty(), Text.from("")) + assertNotEquals(Text.empty(), null) + assertNotEquals(Text.empty(), "") + + assertEquals(Text.from("string"), Text.from("string")) + assertNotEquals(Text.from("string"), Text.empty()) + assertNotEquals(Text.from("string"), Text.from(12)) + assertNotEquals(Text.from("string"), Text.from(12, "string", 12)) + assertNotEquals(Text.from("string"), null) + assertNotEquals(Text.from("string"), "") + + assertEquals(Text.from(12), Text.from(12)) + assertNotEquals(Text.from(12), Text.empty()) + assertNotEquals(Text.from(12), Text.from("12")) + assertNotEquals(Text.from(12), Text.from(12, "12")) + assertNotEquals(Text.from(12), null) + assertNotEquals(Text.from(12), "") + + assertEquals(Text.from(12, 11, Text.empty()), Text.from(12, 11, Text.empty())) + assertNotEquals(Text.from(12, 11, Text.empty()), Text.empty()) + assertNotEquals(Text.from(12, 11, Text.empty()), Text.from("12")) + assertNotEquals(Text.from(12, 11, Text.empty()), Text.from(12)) + assertNotEquals(Text.from(12, 11, Text.empty()), null) + assertNotEquals(Text.from(12, 11, Text.empty()), "") + } + + @Test + fun text_hash() { + assertEquals(Text.empty().hashCode(), Text.empty().hashCode()) + assertEquals(Text.from("hello").hashCode(), Text.from("hello").hashCode()) + assertEquals(Text.from(12).hashCode(), Text.from(12).hashCode()) + assertEquals(Text.from(12, "hello", 11).hashCode(), Text.from(12, "hello", 11).hashCode()) + } +} diff --git a/build.gradle b/build.gradle index 7eadb15..cf43c2b 100644 --- a/build.gradle +++ b/build.gradle @@ -1,17 +1,17 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.2.60' + ext.kotlin_version = '1.2.71' ext.supportVersion = '27.1.1' - ext.compileSdkVersion = 27 - ext.targetSdkVersion = 27 + ext.compileSdkVersion = 28 + ext.targetSdkVersion = 28 repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.4' + classpath 'com.android.tools.build:gradle:3.2.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0' // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index c1ad53b..6f7c1cd 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Tue Jun 26 10:22:26 MSK 2018 +#Wed Oct 03 15:12:40 MSK 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip diff --git a/omegatypes/src/main/java/com/omega_r/libs/omegatypes/Text.kt b/omegatypes/src/main/java/com/omega_r/libs/omegatypes/Text.kt index 0b2629c..a4f84e7 100644 --- a/omegatypes/src/main/java/com/omega_r/libs/omegatypes/Text.kt +++ b/omegatypes/src/main/java/com/omega_r/libs/omegatypes/Text.kt @@ -8,12 +8,26 @@ import android.widget.EditText import android.widget.TextView import android.widget.Toast import java.io.Serializable +import java.util.* open class Text private constructor() : Serializable { open fun isEmpty(): Boolean = true open fun getString(resources: Resources): String? = null + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as Text + + return other.isEmpty() + } + + override fun hashCode(): Int { + return 31 * 17 + "".hashCode() + } + companion object { @JvmStatic fun empty(): Text = Text() @@ -39,6 +53,20 @@ open class Text private constructor() : Serializable { override fun getString(resources: Resources): String? { return string } + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as StringText + + return string == other.string + } + + override fun hashCode(): Int { + return 31 * 17 + (string?.hashCode() ?: 0) + } + } private class ResourceText internal constructor(@StringRes private val stringRes: Int) : Text() { @@ -49,16 +77,45 @@ open class Text private constructor() : Serializable { return resources.getString(stringRes) } + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as ResourceText + + return stringRes == other.stringRes + } + + override fun hashCode(): Int { + return 31 * 17 + stringRes + } + } private class FormatResourceText internal constructor(@StringRes private val stringRes: Int, - private vararg val formatArgs: Any) : Text() { + private vararg val formatArgs: Any) : Text() { override fun isEmpty(): Boolean = stringRes <= 0 override fun getString(resources: Resources): String { return resources.getString(stringRes, *formatArgs) } + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as FormatResourceText + + return stringRes == other.stringRes && Arrays.equals(formatArgs, other.formatArgs) + } + + override fun hashCode(): Int { + var result = 31 * 17 + stringRes + result = 31 * result + Arrays.hashCode(formatArgs) + return result + } + } } @@ -82,6 +139,7 @@ fun Text.applyErrorTo(editText: EditText) { fun Activity.setTitle(text: Text) { title = text.getString(resources) } + fun Context.toast(text: Text, duration: Int = Toast.LENGTH_SHORT): Toast { return Toast.makeText(this, text.getString(resources), duration).apply { show() } } \ No newline at end of file