Skip to content

Instantly share code, notes, and snippets.

View corneil's full-sized avatar

Corneil du Plessis corneil

View GitHub Profile
@corneil
corneil / preconditions.kt
Created August 28, 2019 20:49
precondition extension functions with @ResponseStatus on Exception for Spring MVC.
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.ResponseStatus
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
@ResponseStatus(HttpStatus.PRECONDITION_FAILED)
class PreconditionException(message: String) : Exception(message)
@ExperimentalContracts
@Throws(EntityNotFoundException::class)
@corneil
corneil / SpelTests.kt
Created August 12, 2019 14:18
Spring Spel fails
import org.junit.Assert.assertEquals
import org.junit.Test
import org.springframework.core.convert.TypeDescriptor
import org.springframework.expression.spel.SpelParserConfiguration
import org.springframework.expression.spel.standard.SpelExpressionParser
import org.springframework.expression.spel.support.SimpleEvaluationContext
class PropertyHolder(val properties: Map<String, Any>)
class SpelTests {
@corneil
corneil / if-apply.kt
Last active August 3, 2019 21:32
Kotlin extension functions to provide logic in association with same behaviour as apply
inline fun <T> T.ifApply(expression: Boolean, block: T.() -> Unit, otherwise: T.() -> Unit) : T {
return if(expression) {
this.apply(block)
} else {
this.apply(otherwise)
}
}
inline fun <T> T.ifApply(expression: Boolean, block: T.() -> Unit) : T {
if(expression) {
@corneil
corneil / kotlin-get-required.kt
Last active August 3, 2019 21:34
A Kotlin expression function to get a required value from a map alternatively throw an IllegalArgumentException like require
inline fun <K, V> Map<K, V>.getRequired(key: K): V = this.get(key) ?: throw IllegalArgumentException("$key not found in $keys")
fun main() {
val aMap = mapOf("a" to 1, "b" to 2, "c" to 3)
val anA = aMap.get("a") ?: error("a not found in ${aMap.keys}")
// while line reads easier
val guaranteedB = aMap.getRequired("b")
@corneil
corneil / kotlin-compare.kt
Last active August 4, 2019 09:13
Kotlin extension function to use in compareTo when sorting requires multiple items.
inline fun <T> T.ifZero(exp: Int, block: T.() -> Int): Int = if (exp == 0) block() else exp
// using
data class User(
val firstName: String,
val lastName: String,
val dateOfBirth: Date
) : Comparable<User> {
// classic comparator.
@corneil
corneil / pom.xml
Last active July 25, 2019 06:17
Kotlin Maven Dependencies and plugins
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.neo4j.examples</groupId>
<artifactId>sdn5-movies</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

Keybase proof

I hereby claim:

  • I am corneil on github.
  • I am corneil (https://keybase.io/corneil) on keybase.
  • I have a public key ASDB40W4Wve9epF06pwJoXAKLcK4H5ivgHUjJsOct3FAlAo

To claim this, I am signing this object:

@corneil
corneil / CrudMethodMetadataPostProcessor.java
Last active July 2, 2018 16:21
differences in getResource
public Object getTarget() throws Exception {
MethodInvocation invocation = ExposeInvocationInterceptor.currentInvocation();
return TransactionSynchronizationManager.getResource(invocation.getMethod()); // this call has different parameter.
}
@corneil
corneil / stack-trace.log
Created June 25, 2018 13:01
stack-trace
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy148.getLockModeType(Unknown Source)
at org.springframework.data.jpa.repository.support.QueryDslJpaRepository.createQuery(QueryDslJpaRepository.java:180)
at org.springframework.data.jpa.repository.support.QueryDslJpaRepository.findAll(QueryDslJpaRepository.java:101)
@corneil
corneil / removeUnresolvedDeps.groovy
Created July 13, 2017 14:59
Groovy script to remove 'unresolved' dependencies from local Maven Repository
import groovy.io.FileType
def showOnly = true
def repoHome = new File(System.getProperty('user.home'), '.m2/repository')
def searchFolders = []
args.each { arg ->
if(arg == '-s') {
showOnly = true
} else if(arg == '-d') {
showOnly = false
} else {