-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] Kotlin

, 20 2017 . 13:34 +
Kotlin, . , Kotlin , .

: Kotlin . .

image

1. Annotation Processors


, Kotlin Java, Java , , JPA , , QueryDsl. annotation processor ( ).

:

  • , annotation processor .
  • annotation processor Java ( Kotlin). maven, : Kotlin, annotation processor, Java.
  • kapt ( QueryDsl )

2.


. , :

class UserWithField(param: String) {
    @NotEmpty var field: String = param
}

:
class UserWithConstructor(
    @NotEmpty var paramAndField: String
)

(ElementType.PARAMETER), . :

class UserWithFixedConstructor(
    @field:NotEmpty var paramAndField: String
)

JetBrains, . . .
: @field: , ( javax.persistence.Column), .

3. setter


. , , ( ?). :

class NotDefaultSetterTest {
    @Test fun customSetter() {
        val ivan = User("Ivan")
        assertEquals("Ivan", ivan.name)
        ivan.name = "Ivan"
        assertEquals("IVAN", ivan.name)
    }

    class User(
            nameParam: String
    ) {
        var name: String = nameParam
            set(value) {
                field = value.toUpperCase()
            }
    }
}

, setter, , , , setter. ( -, , ):

class User(
        nameParam: String
) {
    var name: String = nameParam.toUpperCase()
        set(value) {
            field = value.toUpperCase()
        }
}

4.


Spring Hibernate, , . not final .

JSF. , Java-, getter-setter. , , , . , JSF , setter . , JSF, mutable. DTO.

JSF . , , , . view.

: . , / .

, .

5. ,


. get-set, null-safe, , extensions .

:

fun getBalance(group: ClassGroup, month: Date, payments: Map>): Balance {
    val errors = mutableListOf()
    fun tryGetBalanceItem(block: () -> Balance.Item) = try {
        block()
    } catch(e: LackOfInformation) {
        errors += e.message!!
        Balance.Item.empty
    }

    val credit = tryGetBalanceItem {
        creditBalancePart(group, month, payments)
    }
    val salary = tryGetBalanceItem {
        salaryBalancePart(group, month)
    }
    val rent = tryGetBalanceItem {
        rentBalancePart(group, month)
    }
    return Balance(credit, salary, rent, errors)
}

. , ( , ).

, try, if when , ( ). try/catch, , Java- :

val result: String
try {
    //some code
    result = "first"
    //some other code
} catch (e: Exception) {
    result = "second"
}

, result , immutable.

: fun tryGetBalanceItem . JavaScript, .

, tryGetBalanceItem try. , .

6.


. , .

, , User , . , , DTO.

data class User (
        val name: String,
        val birthDate: Date,
        val created: Date = Date()
)
fun usageVersion1() {
    val newUser = User("Ivan", SEPTEMBER_1990)
    val userFromDto = User(userDto.name, userDto.birthDate, userDto.created)
}

disabled, , created, User :

data class User (
        val name: String,
        val birthDate: Date,
        val created: Date = Date(),
        val disabled: Boolean = false
)
fun usageVersion2() {
    val newUser = User("Ivan", SEPTEMBER_1990)
    val userFromDto = User(userDto.name, userDto.birthDate, userDto.created, userDto.disabled)
}

: usageVersion1 . . , , . , ,

7. ,


val months: List = ...
val hallsRents: Map> = months
        .map { month ->
            month to halls
                    .map { it.name to rent(month, it) }
                    .toMap()
        }
        .toMap()

Map Map. , . it, - , . , .

, , . : hallsRents MutableMap, .

. :

val months: List = ...
val hallsRents: Map> = months
        .map { it to rentsByHallNames(it) }
        .toMap()

, . - , , .

: 8500 , Kotlin ( ). , , . prod , : NPE ( ) ehcache ( ).

PS. , Kotlin.
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/331280/

:  

: [1] []
 

:
: 

: ( )

:

  URL