Skip to content
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
17 changes: 0 additions & 17 deletions app/src/test/java/omega_r/com/omegatypesexample/ExampleUnitTest.kt

This file was deleted.

45 changes: 45 additions & 0 deletions app/src/test/java/omega_r/com/omegatypesexample/TextUnitTest.kt
Original file line number Diff line number Diff line change
@@ -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())
}
}
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
60 changes: 59 additions & 1 deletion omegatypes/src/main/java/com/omega_r/libs/omegatypes/Text.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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() {
Expand All @@ -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
}

}

}
Expand All @@ -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() }
}