Skip to content

Instantly share code, notes, and snippets.

Avatar

Jaycobb Cruz jaycobbcruz

View GitHub Profile
@jaycobbcruz
jaycobbcruz / GuiceJUnitRunner.java
Created Jul 5, 2017
Guice - JUnit integration example
View GuiceJUnitRunner.java
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 Feb 17, 2021
Find all permutations of given items using Java 8
View Permutations.java
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);
}
View Pair.java
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 Apr 30, 2017
Create pairs/combinations of items
View Pairs.java
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) {