Skip to content

Instantly share code, notes, and snippets.

View jon-ruckwood's full-sized avatar

Jonathan Ruckwood jon-ruckwood

  • London, England
View GitHub Profile
@jon-ruckwood
jon-ruckwood / ImmutableListCollector.java
Created August 5, 2015 09:35
Java 8 Collector for a Guava ImmutableList
public final class ImmutableListCollector {
public static <T> Collector<T, ImmutableList.Builder<T>, ImmutableList<T>> toImmutableList() {
return Collector.of(
ImmutableList::builder,
ImmutableList.Builder::add,
(left, right) -> { left.addAll(right.build()); return left; },
ImmutableList.Builder::build);
}
@jon-ruckwood
jon-ruckwood / TypeSafeConfigExample.scala
Created March 25, 2012 18:02
Example of loading properties using Typesafe's Config project
import com.typesafe.config.ConfigFactory
val conf = ConfigFactory.load()
val accessToken = conf.getString("accessToken")
val accessTokenSecret = conf.getString("accessTokenSecret")
@jon-ruckwood
jon-ruckwood / ConfigrityExample.scala
Created March 30, 2012 08:28
Example of using Configrity to load properties
import org.streum.configrity._
val config = Configuration.load(
System.getProperty("appConfigPropertyFile"))
val accessToken = config[String]("accessToken")
public class RandomString {
public static String randomString(int len) {
final StringBuilder buffer = new StringBuilder(len);
ThreadLocalRandom.current()
.ints(len, 'A', 'Z')
.mapToObj(Character::toChars)
.forEach(buffer::append);
return buffer.toString();
}
}
@jon-ruckwood
jon-ruckwood / FizzBuzz.java
Last active December 22, 2015 00:39
Naïve Java 8 FizzBuzz implementation
import java.util.stream.IntStream;
public class FizzBuzz {
public static void main(String[] args) {
IntStream.range(1, 100).mapToObj(num -> {
final StringBuilder sb = new StringBuilder(8);
if (num % 3 == 0) {
sb.append("Fizz");
}
if (num % 5 == 0) {
IntStream.range(1, 100).mapToObj(num -> {
final StringBuilder sb = new StringBuilder(8);
if (num % 3 == 0) {
sb.append("Fizz");
}
if (num % 5 == 0) {
sb.append("Buzz");
}
if (sb.length() == 0) {
sb.append(num);
@jon-ruckwood
jon-ruckwood / module-info.java
Last active February 9, 2017 11:38
Example Java 9 module definition with explanations
/*
Modules are not required, although usage is encouraged. This allows existing code to work within Java 9.
Code defined outside of a module is put in an `unnamed module`
Unnamed is a special module that is able to read from *all* other modules, *but* only the exported code (see below)
All of this is automatic when your code compiles. It allows your existing code to continue working as expected.
This is an example `module descriptor`.
It should be placed directly under the `src` dir, e.g. for in Maven `src/main/java/`
Must be named `module-info.java`.
*/
@jon-ruckwood
jon-ruckwood / zaw.zsh
Created June 30, 2017 07:43
zaw config
zstyle ':prezto:module:zaw' default-history-search 'yes'
zstyle ':filter-select:highlight' matched fg=green
zstyle ':filter-select' max-lines 3
zstyle ':filter-select' case-insensitive yes # enable case-insensitive
zstyle ':filter-select' extended-search yes # see below
@jon-ruckwood
jon-ruckwood / intellij.md
Last active February 1, 2018 09:39
Intellij bits and pieces

Live Templates

ee – JUnit ExpectedException rule
@org.junit.Rule
public org.junit.rules.ExpectedException expectedException = org.junit.rules.ExpectedException.none();$CURSOR$
mk – JUnit Mockito rule
@jon-ruckwood
jon-ruckwood / Java9AndJava10TheKeyPartsNotes.java
Last active May 9, 2018 07:59
Notes on Venkat Subramaniam's Java 9 and Java 10: The Key Parts
// Java 9
// JDK improvements
Stream#takeWhile // `limit` using a predicate, i.e. `break`
Stream#dropWhile // `skip` using a predicate, i.e. `continue`
Stream#iterate // emulate a for-loop with a stream, e.g. (not previously possible)