Skip to content

Instantly share code, notes, and snippets.

View SnuktheGreat's full-sized avatar

Snuk the Great SnuktheGreat

  • @timeseriesnl
  • Utrecht, The Netherlands
View GitHub Profile
@SnuktheGreat
SnuktheGreat / UtilException
Created July 15, 2015 11:42
Type safe work-around of throwing typed exceptions from within Streams. It builds upon the accepted answer of this question on stack overflow: http://stackoverflow.com/questions/27644361/how-can-i-throw-checked-exceptions-from-inside-java-8-streams
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public class UtilException<E extends Exception> {
private static final UtilException INSTANCE = new UtilException();
@SuppressWarnings("unchecked")
public static <E extends Exception> UtilException<E> create() throws E {
return INSTANCE;
@SnuktheGreat
SnuktheGreat / GenericTypeErasureTest.java
Last active October 16, 2015 09:38
This test shows how generic type erasures work.
package com.impressiveinteractive.playground;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@SnuktheGreat
SnuktheGreat / ಠ_ಠ.java
Created November 9, 2015 09:31
Pointless, but fun way to throw exceptions!
ಠ_ಠ(new IOException("Disk blew up."))
...
public <T extends Throwable> void ಠ_ಠ(T exception) throws T{
throw exception;
}
@SnuktheGreat
SnuktheGreat / ParametrizedString.java
Last active February 25, 2016 16:41
ParameterizedString - Utility for replacing named markers in a String.
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.stream.Collectors;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@SnuktheGreat
SnuktheGreat / collectionEqualsTest.java
Created February 3, 2016 14:48
Simple test to proof equals works for similar collection types.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.google.common.collect.ImmutableList;
@SnuktheGreat
SnuktheGreat / FormattedException.java
Created February 19, 2016 16:40
Exception type that allows developers to use log4j style message formats and arguments for both the message and exception cause.
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;
/**
* Base {@link Exception} class that allows developers to use the log4j message style formats to create exception messages
* and pass along exception causes.
*/
public class FormattedException extends Exception {
public FormattedException(String format, Object... arguments) {
@SnuktheGreat
SnuktheGreat / caesar
Created May 3, 2016 15:22
Simple caesar cipher bash script
#!/bin/sh
if [ $# -ne 2 ] || [ $1 -lt -36 ] || [ $1 -gt 35 ]
then
echo 'Please specify how many random words would you like to generate !' 1>&2
echo 'example: caesar 5 "mjh5i9n 9pio 8jhpn"' 1>&2
echo 'This will do a caesar cipher of 5 places on the given string, returning romanes eunt domus' 1>&2
exit 0
fi
@SnuktheGreat
SnuktheGreat / vault_copy
Created September 28, 2016 10:28
vault bash scripts
#!/bin/sh
if [ $# -gt 2 ] || [ $# -lt 1 ]
then
echo 'Specify the path to the value first and optionally the field name second.' 1>&2
exit 0
fi
if [ $# -eq 2 ]
then
@SnuktheGreat
SnuktheGreat / StreamFiles.java
Created March 25, 2017 08:33
List files in a Stream recursively (depth-first)
import java.io.File;
import java.util.Arrays;
import java.util.stream.Stream;
public class StreamFiles {
public static Stream<File> recursive(File file) {
File[] listed = file.listFiles();
if (listed == null) {
return Stream.of(file);
}
@SnuktheGreat
SnuktheGreat / git_replace_commit
Created April 26, 2017 08:45
This very specific bash script is used to amend commits in a git tree and then rebase all affected branches and move all affected tags to the new commit. Crazy and dangerous, but better than doing it manually all the time.
#!/usr/bin/env bash
set -e
OLD_BASE=$1 # v1.0.0-step-1
ADDITION=$2
N=$'\n'
ME="$( basename ${BASH_SOURCE[0]} )"
DRY_RUN=false