-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add structural equality operations to
EmailAddress
(#635)
Adds the `equals(Any?)` and the `hashCode()` functions to the `EmailAddress` type in the `org.kotools.types` package.
- Loading branch information
Showing
7 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.kotools.types | ||
|
||
internal fun hashCodeOf(first: Any, vararg others: Any): Int { | ||
val prime = 31 | ||
var result: Int = prime + first.hashCode() | ||
others.forEach { result = prime * result + it.hashCode() } | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,71 @@ import kotools.types.experimental.ExperimentalKotoolsTypesApi | |
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertNotEquals | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNull | ||
import kotlin.test.assertTrue | ||
|
||
@OptIn(ExperimentalKotoolsTypesApi::class) | ||
class EmailAddressTest { | ||
@Test | ||
fun structural_equality_should_pass_with_the_same_instance() { | ||
val first: EmailAddress = EmailAddress.fromString("[email protected]") | ||
val second: Any = first | ||
val equality: Boolean = first.equals(second) | ||
assertTrue(equality) | ||
val firstHashCode: Int = first.hashCode() | ||
val secondHashCode: Int = second.hashCode() | ||
assertEquals(firstHashCode, secondHashCode) | ||
} | ||
|
||
@Test | ||
fun structural_equality_should_pass_with_another_EmailAddress_having_the_same_string_representation() { | ||
val value: Any = "[email protected]" | ||
val first: EmailAddress = EmailAddress.fromString(value) | ||
val second: Any = EmailAddress.fromString(value) | ||
val equality: Boolean = first.equals(second) | ||
assertTrue(equality) | ||
val firstHashCode: Int = first.hashCode() | ||
val secondHashCode: Int = second.hashCode() | ||
assertEquals(firstHashCode, secondHashCode) | ||
} | ||
|
||
@Test | ||
fun structural_equality_should_fail_with_null() { | ||
val first: EmailAddress = EmailAddress.fromString("[email protected]") | ||
val second: Any? = null | ||
val equality: Boolean = first.equals(second) | ||
assertFalse(equality) | ||
val firstHashCode: Int = first.hashCode() | ||
val secondHashCode: Int = second.hashCode() | ||
assertNotEquals(firstHashCode, secondHashCode) | ||
} | ||
|
||
@Test | ||
fun structural_equality_should_fail_with_another_object_having_another_type_than_EmailAddress() { | ||
val value: Any = "[email protected]" | ||
val first: EmailAddress = EmailAddress.fromString(value) | ||
val second: Any = value | ||
val equality: Boolean = first.equals(second) | ||
assertFalse(equality) | ||
val firstHashCode: Int = first.hashCode() | ||
val secondHashCode: Int = second.hashCode() | ||
assertNotEquals(firstHashCode, secondHashCode) | ||
} | ||
|
||
@Test | ||
fun structural_equality_should_fail_with_another_EmailAddress_having_another_string_representation() { | ||
val first: EmailAddress = EmailAddress.fromString("[email protected]") | ||
val second: Any = EmailAddress.fromString("[email protected]") | ||
val equality: Boolean = first.equals(second) | ||
assertFalse(equality) | ||
val firstHashCode: Int = first.hashCode() | ||
val secondHashCode: Int = second.hashCode() | ||
assertNotEquals(firstHashCode, secondHashCode) | ||
} | ||
|
||
@Test | ||
fun toString_should_pass() { | ||
val value: Any = "[email protected]" | ||
|
18 changes: 18 additions & 0 deletions
18
subprojects/samples/src/main/java/EmailAddressJavaSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,24 @@ | ||
package org.kotools.types; | ||
|
||
class EmailAddressJavaSample { | ||
void equals_override() { | ||
final Object value = "[email protected]"; | ||
final EmailAddress first = EmailAddress.fromString(value); | ||
final EmailAddress second = EmailAddress.fromString(value); | ||
final boolean result = first.equals(second); | ||
System.out.println(result); // true | ||
} // END | ||
|
||
void hashCode_override() { | ||
final Object value = "[email protected]"; | ||
final int first = EmailAddress.fromString(value) | ||
.hashCode(); // TABS: 2 | ||
final int second = EmailAddress.fromString(value) | ||
.hashCode(); // TABS: 2 | ||
final boolean result = first == second; | ||
System.out.println(result); // true | ||
} // END | ||
|
||
void toString_override() { | ||
final Object value = "[email protected]"; | ||
final EmailAddress address = EmailAddress.fromString(value); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,24 @@ import kotools.types.experimental.ExperimentalKotoolsTypesApi | |
|
||
@OptIn(ExperimentalKotoolsTypesApi::class) | ||
internal object EmailAddressKotlinSample { | ||
@Suppress("FunctionName") | ||
fun equals_override() { | ||
val value: Any = "[email protected]" | ||
val first: EmailAddress = EmailAddress.fromString(value) | ||
val second: EmailAddress = EmailAddress.fromString(value) | ||
val result: Boolean = first == second // or first.equals(second) | ||
println(result) // true | ||
} // END | ||
|
||
@Suppress("FunctionName") | ||
fun hashCode_override() { | ||
val value: Any = "[email protected]" | ||
val first: EmailAddress = EmailAddress.fromString(value) | ||
val second: EmailAddress = EmailAddress.fromString(value) | ||
val result: Boolean = first.hashCode() == second.hashCode() | ||
println(result) // true | ||
} // END | ||
|
||
@Suppress("FunctionName") | ||
fun toString_override() { | ||
val value: Any = "[email protected]" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters