Skip to content

Instantly share code, notes, and snippets.

Avatar
🤒
Out sick

Hwan Jo FrancescoJo

🤒
Out sick
View GitHub Profile
@FrancescoJo
FrancescoJo / ROData-SpringDataJpa.kt
Last active Oct 11, 2019
Encapsulating a read-only JPA entity via interface under Spring-JPA
View ROData-SpringDataJpa.kt
// ---------------------------------------
// Person.kt
// ---------------------------------------
interface Person {
val id: Long
val name: String
val age: Int
@FrancescoJo
FrancescoJo / fancy-console.js
Created Aug 2, 2019
Facebook "STOP!" code for Google chrome
View fancy-console.js
console.log("%cGood to go!", "font: 2em roboto; color: yellow; background-color: green;");
@FrancescoJo
FrancescoJo / AnimalClassfication.java
Last active Jun 27, 2019
An example to demonstrate animal classification system in Java 1.8.x.
View AnimalClassfication.java
class Animal { }
class Reptile extends Animal { }
class Bird extends Animal { }
interface MobileLife {
// Every classes derived from this interface should implement common features on it.
void move(double displacement);
double getDistance();
@FrancescoJo
FrancescoJo / AnimalClassfication.dart
Created Jun 17, 2019
An example to demonstrate animal classification system in Dart 2.x.
View AnimalClassfication.dart
class Animal { }
class Reptile extends Animal { }
class Bird extends Animal { }
mixin MobileLife {
// We don't need to implement _move on every classes using this mixin, since mixin can include state.
var _distance = 0.0;
void _move(double displacement) {
@FrancescoJo
FrancescoJo / spring-jpa-disable-automatic-method-derivation.kt
Last active Jun 5, 2019
How to disable spring-jpa automatic method derivation
View spring-jpa-disable-automatic-method-derivation.kt
interface UserRepository : JpaRepository<User, Long>, UserRepositoryExtension
interface UserRepositoryExtension {
/*
* Here we get exception as following:
*
* Caused by: java.lang.IllegalArgumentException: Failed to create query for method
* public abstract User UserRepositoryExtension.getByRoleCriteria(java.lang.String,java.util.Set)!
* At least 2 parameter(s) provided but only 1 parameter(s) present in query.
*
View KeyMappedEnum.kt
interface EnumWithKey<T, K> {
val T.key: K
}
/*
* The reified type parameter lets you call the function without explicitly
* passing the Class-object.
*/
inline fun <reified T : Enum<T>, R> EnumWithKey<T, R>.byKey(key: R): T? {
return enumValues<T>().find { it.key == key }
@FrancescoJo
FrancescoJo / RandomHelper.kt
Created Jan 23, 2019
Fast random value generation used for testing logic in Kotlin-JVM
View RandomHelper.kt
fun randomBoolean() = ThreadLocalRandom.current().nextBoolean()
fun randomByte() = ThreadLocalRandom.current().nextInt().toByte()
fun randomShort() = ThreadLocalRandom.current().nextInt().toShort()
fun randomInt() = ThreadLocalRandom.current().nextInt()
fun randomLong() = ThreadLocalRandom.current().nextLong()
fun randomFloat() = ThreadLocalRandom.current().nextFloat()
fun randomDouble() = ThreadLocalRandom.current().nextDouble()
fun randomChar() = randomStr(1)[0]
fun randomStr(maxLength: Int) = StringBuffer().apply {
for (i in 0 until maxLength) {
@FrancescoJo
FrancescoJo / Combinations.kt
Last active Feb 16, 2019
Logic to yield all `nCr` combinations
View Combinations.kt
/**
* This logic demonstrates how to draw all nCr combinations from given set.
* The time complexity of this logic is O(n²).
* Actual logic invocations are 1/2((n - 2)² + r(n - 2) + 2), where n > 2.
*
* Imagine selecting 3 different numbers from a set which has 4 different
* numbers. Assume that we've given a set as follows:
*
* let dataSet = {1, 2, 3, 4}
*
@FrancescoJo
FrancescoJo / EvenOddPickGame.kt
Last active Jun 27, 2018
Unit testing with JUnit+Mockito vs. Spock. Have a look how Spock usage is concise than traditional JUnit side.
View EvenOddPickGame.kt
class EvenOddPickGame(private val randomIntGen : RandomValueGenerator<Integer>) {
fun pick(times: Int): List<String> {
return ArrayList<String>().apply {
for (i in 0 until times) {
val rolled = randomIntGen.nextRandom(lowerBound = 1, upperBound = 2) % 2
if (rolled == 0) {
add("EVEN")
} else {
add("ODD")
}
@FrancescoJo
FrancescoJo / AndroidAESCipherHelper.kt
Last active Mar 6, 2020
Android AES cipher helper with system generated key. No more static final String key in our class - an approach which is very vulnerable to reverse engineering attack. Available only API Level 23(M)+
View AndroidAESCipherHelper.kt
import android.annotation.TargetApi
import android.os.Build
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import android.util.Base64
import timber.log.Timber
import java.security.GeneralSecurityException
import java.security.KeyStore
import javax.crypto.Cipher
import javax.crypto.KeyGenerator
You can’t perform that action at this time.