Skip to content

Commit 2b28336

Browse files
veontomomaibin
authored andcommitted
Add the code for article "Kotlin Annotations" (BAEL-2579) (eugenp#6540)
1 parent 4df665a commit 2b28336

5 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.annotations
2+
3+
@Target(AnnotationTarget.FIELD)
4+
annotation class Positive
5+
6+
@Target(AnnotationTarget.FIELD)
7+
annotation class AllowedNames(val names: Array<String>)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.annotations
2+
3+
class Item(@Positive val amount: Float, @AllowedNames(["Alice", "Bob"]) val name: String)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.annotations
2+
3+
fun main(args: Array<String>) {
4+
val item = Item(amount = 1.0f, name = "Bob")
5+
val validator = Validator()
6+
println("Is instance valid? ${validator.isValid(item)}")
7+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.annotations
2+
3+
/**
4+
* Naive annotation-based validator.
5+
* @author A.Shcherbakov
6+
*/
7+
class Validator() {
8+
9+
/**
10+
* Return true if every item's property annotated with @Positive is positive and if
11+
* every item's property annotated with @AllowedNames has a value specified in that annotation.
12+
*/
13+
fun isValid(item: Item): Boolean {
14+
val fields = item::class.java.declaredFields
15+
for (field in fields) {
16+
field.isAccessible = true
17+
for (annotation in field.annotations) {
18+
val value = field.get(item)
19+
if (field.isAnnotationPresent(Positive::class.java)) {
20+
val amount = value as Float
21+
if (amount < 0) {
22+
return false
23+
}
24+
}
25+
if (field.isAnnotationPresent(AllowedNames::class.java)) {
26+
val allowedNames = field.getAnnotation(AllowedNames::class.java)?.names
27+
val name = value as String
28+
allowedNames?.let {
29+
if (!it.contains(name)) {
30+
return false
31+
}
32+
}
33+
}
34+
}
35+
}
36+
return true
37+
}
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.annotations
2+
3+
import org.junit.jupiter.api.Assertions.assertTrue
4+
import org.junit.jupiter.api.Assertions.assertFalse
5+
import org.junit.jupiter.api.Test
6+
7+
8+
class ValidationUnitTest {
9+
10+
@Test
11+
fun whenAmountIsOneAndNameIsAlice_thenTrue() {
12+
assertTrue(Validator().isValid(Item(1f, "Alice")))
13+
}
14+
15+
@Test
16+
fun whenAmountIsOneAndNameIsBob_thenTrue() {
17+
assertTrue(Validator().isValid(Item(1f, "Bob")))
18+
}
19+
20+
21+
@Test
22+
fun whenAmountIsMinusOneAndNameIsAlice_thenFalse() {
23+
assertFalse(Validator().isValid(Item(-1f, "Alice")))
24+
}
25+
26+
@Test
27+
fun whenAmountIsMinusOneAndNameIsBob_thenFalse() {
28+
assertFalse(Validator().isValid(Item(-1f, "Bob")))
29+
}
30+
31+
@Test
32+
fun whenAmountIsOneAndNameIsTom_thenFalse() {
33+
assertFalse(Validator().isValid(Item(1f, "Tom")))
34+
}
35+
36+
@Test
37+
fun whenAmountIsMinusOneAndNameIsTom_thenFalse() {
38+
assertFalse(Validator().isValid(Item(-1f, "Tom")))
39+
}
40+
41+
42+
}

0 commit comments

Comments
 (0)