kotlin

Idiomatic way to create n-ary cartesian product (combinations of several sets of parameters)

*爱你&永不变心* 提交于 2020-06-27 08:15:11
问题 To create all possible combinations of two sets of parameters and perform an action on them, you can do: setOf(foo, bar, baz).forEach { a -> setOf(0, 1).forEach { b -> /* use a and b */ } } However, if you have (potentially many) more parameters, this quickly turns into a pyramid of doom: setOf(foo, bar, baz).forEach { a -> setOf(0, 1).forEach { b -> setOf(true, false, null).forEach { c -> setOf("Hello,", "World!").forEach { d -> /* use a, b, c and d */ } } } } You could write this similarly

swagger annotation content-type not set

杀马特。学长 韩版系。学妹 提交于 2020-06-27 07:35:28
问题 I have this spring rest controller: @RestController @RequestMapping("/communications") class CommunicationController(private val service: CommunicationService) { @ApiOperation( produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE ) @GetMapping( consumes = [APPLICATION_JSON_VALUE], produces = [APPLICATION_JSON_VALUE] ) fun findAll( criterias: CommunicationCriterias, page: Pageable ): List<CommunicationDTO> = service.findCommunications(criterias, page) } When I test this

How to mock lambda with mockito in kotlin

痞子三分冷 提交于 2020-06-27 07:26:59
问题 I have a kotlin Android app. There is a function that loads compositions from the backend and returns them to a callback: getCompositons(callback: (Array<Composition>) -> Unit) How can I mock the callback using mockito. So that I then can do something like this: var callback = //mockito mock getCompositons(callback) verify(callback, timeout(10000)).apply() I read that lambda are matched to the java type function and therefore I assume apply could be the method invoked. Maybe I could mock a

How to mock lambda with mockito in kotlin

雨燕双飞 提交于 2020-06-27 07:26:14
问题 I have a kotlin Android app. There is a function that loads compositions from the backend and returns them to a callback: getCompositons(callback: (Array<Composition>) -> Unit) How can I mock the callback using mockito. So that I then can do something like this: var callback = //mockito mock getCompositons(callback) verify(callback, timeout(10000)).apply() I read that lambda are matched to the java type function and therefore I assume apply could be the method invoked. Maybe I could mock a

Converting a byte array into a hex string

蹲街弑〆低调 提交于 2020-06-27 06:49:09
问题 Surprisingly (to me), this code does not do what I want: fun ByteArray.toHexString() : String { return this.joinToString("") { it.toString(16) } } Turns out Byte is signed , so you get negative hex representations for individual bytes, which leads to a completely bogus end result. Also, Byte.toString won't pad leading zeroes, which you'd want here. What is the simplest (no additional libraries, ideally no extensions) resp. most efficient fix? 回答1: As I am on Kotlin 1.3 you may also be

Converting a byte array into a hex string

两盒软妹~` 提交于 2020-06-27 06:49:08
问题 Surprisingly (to me), this code does not do what I want: fun ByteArray.toHexString() : String { return this.joinToString("") { it.toString(16) } } Turns out Byte is signed , so you get negative hex representations for individual bytes, which leads to a completely bogus end result. Also, Byte.toString won't pad leading zeroes, which you'd want here. What is the simplest (no additional libraries, ideally no extensions) resp. most efficient fix? 回答1: As I am on Kotlin 1.3 you may also be

Can I reference a specific function overload?

一世执手 提交于 2020-06-27 06:48:13
问题 Given the following code fun example() {} fun example(name: String) {} How can I reference a specific function? I.e. example() or example(String) ? Using [example] I can't specify which exactly function I want. 回答1: At this time you can't. Dokka generates all overloads of a function on a single page, and the link points to that page, so you can simply specify the overload you need as text: "the one-argument overload of [example]". There is an open issue for adding the possibility to link to a

How to start a Activity of Kotlin from Java android

拈花ヽ惹草 提交于 2020-06-26 21:11:22
问题 Can I do this below prossess in android studio? I have a Kotlin project that I create another activity with its Java class and I want to start activity of Kotlin with clicking botton in activity of java then it start Kotlin activity 回答1: Kotlin is interoperable with Java. Just start the activity using an Intent like you would normally do in Java. 回答2: Yes you can start activity from java to Kotlin and vice versa. from java startActivity(new Intent(context,DestinationActivity.class)) from

How to pass data between fragments when using bottomNavView

落花浮王杯 提交于 2020-06-26 14:15:36
问题 I have an app with one activity and two fragments, And I navigate using jetpack navigation. In my "Main" fragment, I've a button that trigger function inside the viewModel: edit_text_button.setOnClickListener { homeViewModel.onButtonClicked() } onButtonClicked inside viewModel , basically shuffle the list and trigger observer in main fragment. fun onButtonClicked() { initList = (1..9).shuffled() _list.value = initList } My question is: How can I pass every time the updated list to the second

How to pass data between fragments when using bottomNavView

三世轮回 提交于 2020-06-26 14:14:29
问题 I have an app with one activity and two fragments, And I navigate using jetpack navigation. In my "Main" fragment, I've a button that trigger function inside the viewModel: edit_text_button.setOnClickListener { homeViewModel.onButtonClicked() } onButtonClicked inside viewModel , basically shuffle the list and trigger observer in main fragment. fun onButtonClicked() { initList = (1..9).shuffled() _list.value = initList } My question is: How can I pass every time the updated list to the second