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
import org.springframework.jdbc.core.JdbcTemplate; | |
import org.springframework.jdbc.core.RowMapper; | |
import org.springframework.stereotype.Repository; | |
import java.util.List; | |
import java.util.Optional; | |
@Repository | |
public class OrderRepository { | |
private static final RowMapper<Order> rowMapper = (rs, i) -> new Order(rs.getLong("id")); |
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
import org.springframework.jdbc.core.JdbcTemplate | |
import org.springframework.jdbc.core.RowMapper | |
import org.springframework.stereotype.Repository | |
import java.util.* | |
@Repository | |
class OrderRepository(private val jdbcTemplate: JdbcTemplate) { | |
private val rowMapper: RowMapper<Order> = RowMapper { rs, i -> Order(rs.getLong("id")) } | |
fun findOrderById(id: Long): Optional<Order> { |
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
import org.springframework.jdbc.core.JdbcTemplate | |
import org.springframework.jdbc.core.RowMapper | |
import org.springframework.stereotype.Repository | |
@Repository | |
class OrderRepository(private val jdbcTemplate: JdbcTemplate) { | |
private val rowMapper: RowMapper<Order> = RowMapper { rs, i -> Order(rs.getLong("id")) } | |
fun findOrderById(id: Long): Order? { | |
val result = jdbcTemplate.query<Order>("select * from orders where id=:id", rowMapper, id) |
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
public void doStuffWithOrder(Long id) { | |
Optional<Order> orderById = orderRepository.findOrderById(id); | |
orderById.ifPresent( order -> { | |
// do stuff with order | |
}); | |
} |
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
fun doStuffWithOrder(id: Long) { | |
val orderById: Order? = orderRepository.findOrderById(id) | |
orderById?.run { | |
// do stuff with order | |
} | |
orderById.id // Compiler error: Only safe ?. or non-null asserted calls are allowed on a nullable receiver of type Order? | |
} |
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
val orderOrElse: Order = orderById?.apply { | |
// do stuff with order | |
} ?: Order() // orElse with default | |
val orderOrElseThrow: Order = orderById?.apply { | |
// do stuff with order | |
} ?: throw RuntimeException("orElseThrow") // orElseThrow | |
val orderId: Long? = orderById?.let { | |
// do stuff with order |
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
import kotlinx.coroutines.experimental.GlobalScope | |
import kotlinx.coroutines.experimental.async | |
import kotlinx.coroutines.experimental.runBlocking | |
import java.util.* | |
data class GiftCard(val id: Int, val code: String) | |
class GiftCardService { | |
/** |
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
import org.slf4j.LoggerFactory | |
import org.springframework.jdbc.core.JdbcTemplate | |
import org.springframework.stereotype.Component | |
import org.springframework.transaction.annotation.Propagation | |
import org.springframework.transaction.annotation.Transactional | |
import java.time.Duration | |
interface LockManager { | |
fun <T> tryWithLock(key: Long, timeout: Duration, function: () -> T): T | |
} |
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
<project> | |
... | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<source>9</source> |
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
<project> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.jetbrains.kotlin</groupId> | |
<artifactId>kotlin-maven-plugin</artifactId> | |
<executions> | |
<execution> | |
<id>compile</id> | |
<phase>process-sources</phase> |