Skip to content

Instantly share code, notes, and snippets.

View jonikarppinen's full-sized avatar

Joni Karppinen jonikarppinen

View GitHub Profile
@jonikarppinen
jonikarppinen / onlyEvenInt.kt
Last active July 2, 2024 05:03
onlyEvenInt in Kotlin ("Write a function that takes an array of integers as an input, and returns a new array containing only even integers, sorted.")
// Run it at play.kotlinlang.org
fun onlyEvenInt(numbers: List<Int>) = numbers.filter { it % 2 == 0 }.sorted()
fun main() {
println(onlyEvenInt(listOf(1, 2, 3, 4, 5, 6, 7)))
println(onlyEvenInt(listOf(6, 7, 1, 2, 3, 4, 5)))
println(onlyEvenInt(listOf(1, 3, 5, 7)))
println(onlyEvenInt(listOf(-2, -3, -4, 0)))
}
@jonikarppinen
jonikarppinen / kapt-m1-error.txt
Created January 12, 2022 20:57
kaptGenerateStubsDebugKotlin error output
> Task :app:kaptGenerateStubsDebugKotlin FAILED
e: java.lang.IllegalAccessError: class org.jetbrains.kotlin.kapt3.base.KaptContext (in unnamed module @0x6a6abeee) cannot access class com.sun.tools.javac.util.Context (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.util to unnamed module @0x6a6abeee
at org.jetbrains.kotlin.kapt3.base.KaptContext.<init>(KaptContext.kt:28)
at org.jetbrains.kotlin.kapt3.KaptContextForStubGeneration.<init>(KaptContextForStubGeneration.kt:40)
at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.contextForStubGeneration(Kapt3Extension.kt:287)
at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.analysisCompleted(Kapt3Extension.kt:171)
at org.jetbrains.kotlin.kapt3.ClasspathBasedKapt3Extension.analysisCompleted(Kapt3Extension.kt:102)
at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$invokeExtensionsOnAnalysisComplete(TopDownAnalyzerFacadeForJVM.kt:112)
at org.jetbrains.kotlin.cli.jvm.com
@jonikarppinen
jonikarppinen / play-error-response.html
Last active September 30, 2016 16:08
HTML error response generated automatically by Play Framework (related to the question http://stackoverflow.com/q/39794500/56285)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bad request</title>
<link rel="shortcut icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAlFJREFUeNqUU8tOFEEUPVVdNV3dPe8xYRBnjGhmBgKjKzCIiQvBoIaNbly5Z+PSv3Aj7DSiP2B0rwkLGVdGgxITSCRIJGSMEQWZR3eVt5sEFBgTb/dN1yvnnHtPNTPG4PqdHgCMXnPRSZrpSuH8vUJu4DE4rYHDGAZDX62BZttHqTiIayM3gGiXQsgYLEvATaqxU+dy1U13YXapXptpNHY8iwn8KyIAzm1KBdtRZWErpI5lEWTXp5Z/vHpZ3/wyKKwYGGOdAYwR0EZwoezTYApBEIObyELl/aE1/83cp40Pt5mxqCKrE4Ck+mVWKKcI5tA8BLEhRBKJLjez6a7MLq7XZtp+yyOawwCBtkiBVZDKzRk4NN7NQBMYPHiZDFhXY+p9ff7F961vVcnl4R5I2ykJ5XFN7Ab7Gc61VoipNBKF+PDyztu5lfrSLT/wIwCxq0CAGtXHZTzqR2jtwQiXONma6hHpj9sLT7YaPxfTXuZdBGA02Wi7FS48YiTfj+i2NhqtdhP5RC8mh2/Op7y0v6eAcWVLFT8D7kWX5S9mepp+C450MV6aWL1cGnvkxbwHtLW2B9AOkLeUd9KEDuh9fl/7CEj7YH5g+3r/lWfF9In7tPz6T4IIwBJOr1SJyIGQMZQbsh5P9uBq5VJtqHh2mo49pdw5WFoEwKWqWHacaWOjQXWGcifKo6vj5RGS6zykI587XeUIQDqJSmAp+lE4qt19W5P9o8+Lma5DcjsC8JiT607lMVkdqQ0Vyh3lHhmh52tfNy78ajXv0rgYzv8
@jonikarppinen
jonikarppinen / ContactsFragment.java
Last active February 23, 2020 09:43
Fixes to "Retrieving a List of Contacts" Android tutorial by Google: https://developer.android.com/training/contacts-provider/retrieve-names.html
// Fixes to compilation errors in "Define the onItemClick() method" section
// Doesn't compile
Cursor cursor = parent.getAdapter().getCursor();
// Fixed (not sure if this is the cleanest way though)
Cursor cursor = ((SimpleCursorAdapter) parent.getAdapter()).getCursor();
// Doesn't compile
mContactId = getLong(CONTACT_ID_INDEX);
// Fixed
@jonikarppinen
jonikarppinen / 1_RxJava-connectivity-status-example.md
Last active November 23, 2022 18:16
Example of listening to connectivity status in Android and reacting to going offline. (Subscription, Observable, Observer, PublishSubject are from RxJava.)

Example of using RxJava to listen to connectivity status in Android

You could use the same approach to listen to any status, but this example includes network connectivity specifics too (ConnectionChangeReceiver and AndroidUtils.isConnected).

@jonikarppinen
jonikarppinen / Specs2-output.txt
Last active December 8, 2015 20:34
Specs2 output when run in IntelliJ IDEA: "Test framework quit unexpectedly" (see http://stackoverflow.com/questions/34159857/specs2-how-to-test-a-class-with-more-than-one-injected-dependency)
Testing started at 22:28 ...
services.ReportServiceSpec$
java.lang.ClassNotFoundException: services.ReportServiceSpec$
STACKTRACE
java.net.URLClassLoader.findClass(URLClassLoader.java:381)
java.lang.ClassLoader.loadClass(ClassLoader.java:424)
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
@jonikarppinen
jonikarppinen / Messages.java
Last active February 23, 2021 23:36
Example of using message resources in Spring Boot service layer code, in as simple way as possible (hopefully!). NOTE: this approach supports only a single locale, not dynamically changing it.
package com.company.project.components;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Locale;
@jonikarppinen
jonikarppinen / PdfOrErrorController.java
Last active November 17, 2021 11:49
Example of using ExceptionHandler in Spring Boot: a controller method that returns either binary data or error JSON
package com.company.project.controllers;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Random;
@jonikarppinen
jonikarppinen / StringUtils.java
Last active July 2, 2024 05:30
StringUtils – common utility methods for Java
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
@jonikarppinen
jonikarppinen / CustomObjectMapper.java
Last active June 28, 2018 21:28
Example of making Jackson behave more sensibly by default (also, more like Gson)
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
/**
* ObjectMapper customised for my tastes and most typical needs
*
* @author Joni Karppinen
*/