Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View jaycobbcruz's full-sized avatar

Jaycobb Cruz jaycobbcruz

View GitHub Profile
@jaycobbcruz
jaycobbcruz / GuiceJUnitRunner.java
Created July 5, 2017 05:48
Guice - JUnit integration example
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
public class GuiceJUnitRunner extends BlockJUnit4ClassRunner {
public GuiceJUnitRunner(Class<?> klass) throws InitializationError {
super(klass);
@jaycobbcruz
jaycobbcruz / Permutations.java
Last active February 16, 2022 09:20
Find all permutations of given items using Java 8
public class Permutations {
public static <T> Stream<Stream<T>> of(final List<T> items) {
return IntStream.range(0, factorial(items.size())).mapToObj(i -> permutation(i, items).stream());
}
private static int factorial(final int num) {
return IntStream.rangeClosed(2, num).reduce(1, (x, y) -> x * y);
}
@jaycobbcruz
jaycobbcruz / Pair.java
Created April 30, 2017 10:20
A simple pair
public class Pair<L, R> {
private L left;
private R right;
public Pair(L left, R right) {
if (left == null) { throw new RuntimeException("Left must not be null"); }
if (right == null) { throw new RuntimeException("Right must not be null"); }
this.left = left;
this.right = right;
@jaycobbcruz
jaycobbcruz / Pairs.java
Created April 30, 2017 10:18
Create pairs/combinations of items
public class Pairs {
/**
* Create a list of pairs of the given items.
* e.g. ["A", "B", "C", "D"] will return ["A:B", "A:C", "A:D", "B:C", "B:D", "C:D"]
* @param items the items to create the pair
* @param <T> type of the items
* @return a list of pairs
*/
public static <T> List<Pair<T, T>> of(final List<T> items) {