-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e51c077
commit 299934b
Showing
8 changed files
with
137 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
60 changes: 60 additions & 0 deletions
60
...oroutines-crums-group-1/src/main/kotlin/org/jesperancinha/ktd/DSLContractsSmartCasting.kt
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,60 @@ | ||
package org.jesperancinha.ktd | ||
|
||
import kotlin.contracts.ExperimentalContracts | ||
import kotlin.contracts.contract | ||
|
||
class DSLContractsSmartCasting { | ||
fun showContract(id: Long?): Long? { | ||
if (isValidIDE(id)) { | ||
doWithId(id) | ||
} | ||
return id | ||
} | ||
|
||
fun doWithId(id: Long): Long { | ||
println("Made contract with id $id") | ||
return id | ||
} | ||
|
||
@OptIn(ExperimentalContracts::class) | ||
fun isValidIDE(id: Long?): Boolean { | ||
contract { returns(true) implies (id != null) } | ||
return id != null && id != 0L | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
fun main(args: Array<String> = emptyArray()) { | ||
println("${DSLContractsSmartCasting().showContract(123)}") | ||
println("${DSLContractsSmartCasting().showContract(null)}") | ||
} | ||
} | ||
} | ||
class ContractsExtension { | ||
fun showContract(id: Long?): Long? { | ||
if (id.isValidID()) { | ||
doWithId(id!!) | ||
} | ||
return id | ||
} | ||
|
||
fun doWithId(id: Long): Long { | ||
println("Made contract with id $id") | ||
return id | ||
} | ||
|
||
@OptIn(ExperimentalContracts::class) | ||
fun Long?.isValidID(): Boolean { | ||
contract { returns(true) implies (this@isValidID != null) } | ||
return this != null && this != 0L | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
fun main(args: Array<String> = emptyArray()) { | ||
DSLContractsSmartCasting.main() | ||
println("${DSLContractsSmartCasting().showContract(123)}") | ||
println("${DSLContractsSmartCasting().showContract(null)}") | ||
} | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
...es/coroutines-crums-group-1/src/main/kotlin/org/jesperancinha/ktd/ValidationUsersEmail.kt
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,24 @@ | ||
package org.jesperancinha.ktd | ||
|
||
import jakarta.validation.Validation | ||
import jakarta.validation.constraints.Email | ||
|
||
|
||
data class User ( | ||
@field:Email(message = "Please insert a valid email") | ||
val email:String | ||
) | ||
|
||
class ValidationUsersEmail { | ||
companion object { | ||
@JvmStatic | ||
fun main(args: Array<String> = emptyArray()) { | ||
val factory = Validation.buildDefaultValidatorFactory() | ||
val validator = factory.validator | ||
val validate1 = validator.validate(User("bad")) | ||
println(validate1) | ||
val validate2 = validator.validate(User("user@someemail.com")) | ||
println(validate2) | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...-coroutines/coroutines-crums-group-1/src/main/kotlin/org/jesperancinha/ktd/java/User.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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.jesperancinha.ktd.java; | ||
|
||
import jakarta.validation.constraints.Email; | ||
|
||
public class User { | ||
|
||
@Email(message = "Please insert a valid email") | ||
private String email; | ||
|
||
public User(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...utines-crums-group-1/src/main/kotlin/org/jesperancinha/ktd/java/ValidationUsersEmail.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.jesperancinha.ktd.java; | ||
|
||
import static jakarta.validation.Validation.buildDefaultValidatorFactory; | ||
|
||
public class ValidationUsersEmail { | ||
public static void main(String[] args) { | ||
try(var factory = buildDefaultValidatorFactory()) { | ||
var validator = factory.getValidator(); | ||
var validate1 = validator.validate(new User("bad")); | ||
System.out.println(validate1); | ||
var validate2 = validator.validate(new User("user@someemail.com")); | ||
System.out.println(validate2); | ||
} | ||
} | ||
} |
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