Instantly share code, notes, and snippets.

View ParameterTypeConverterFactory.java
private Iterable<Object[]> convert(Iterable<Object[]> data) {
List<Object[]> parameters = new ArrayList<>();
for (Object[] args : data) {
Object[] params = new Object[args.length];
for (int i = 0; i < args.length; i++) {
Object param;
Class<?> parameterClazz = method.getMethod()
.getParameterTypes()[i];
if (parameterClazz.equals(String.class)) {
param = buildString(args[i]);
View ParameterConverterFinder.java
private Iterable<Object[]> convert(final Iterable<Object[]> data) {
List<Object[]> parameters = new ArrayList<>();
for (Object[] args : data) {
Object[] params = new Object[args.length];
for (int i = 0; i < args.length; i++) {
final Class<?> parameterClazz = method.getMethod()
.getParameterTypes()[i];
params[i] = convertParameter(args[i], parameterClazz);
}
parameters.add(params);
View ParameterConverterMapping.java
private final Map<Class<?>, ParameterConverter<Object, ?>> convertersByClass = new ImmutableMap.Builder<Class<?>, ParameterConverter<Object, ?>>()
.put(String.class, this::buildString)
.put(Integer.TYPE, this::buildPrimitiveInt)
.put(Integer.class, this::buildInteger)
.put(Long.TYPE, this::buildPrimitiveLong)
.put(Long.class, this::buildLong)
.put(Boolean.TYPE, this::buildPrimitiveBoolean)
.put(Boolean.class, this::buildBoolean)
.put(Short.TYPE, this::buildPrimitiveShort)
.put(Short.class, this::buildShort)
View ParameterConverter.java
@FunctionalInterface
public interface ParameterConverter<T, R> {
R convert(T t);
}
View ListOfPairsJava9.java
public class ListOfPairsJava9 {
public static void main(String[] args) {
final Map<String, List<String>> values = new HashMap<>();
values.put("a", Arrays.asList("1", "2", "3"));
values.put("b", Arrays.asList("4", "5", "6"));
values.put("c", Collections.singletonList("7"));
final List<Map.Entry<String, String>> result = values
.entrySet()
View AllPeopleCarsJava9.java
Collection<Car> allPeopleCars = people
.stream()
.map(Person::getCars)
.flatMap(Optional::stream)
.flatMap(Collection::stream)
.collect(Collectors.toList());
View OptionalPeopleCars.java
Collection<Car> allPeopleCars = people
.stream()
.map(Person::getCars)
.flatMap(mayHaveCars -> mayHaveCars.map(Collection::stream).orElseGet(Stream::empty))
.collect(Collectors.toList());
View AllPeopleCars.java
Collection<Car> allPeopleCars = people
.stream()
.map(Person::getCars)
.flatMap(mayHaveCars -> mayHaveCars.map(Collection::stream).orElse(Stream.empty()))
.collect(Collectors.toList());
View PeopleCars.java
Collection<Car> allPeopleCars = people
.stream()
.map(Person::getCars)
.flatMap(mayHaveCars -> mayHaveCars.isPresent() ? mayHaveCars.get().stream() : Stream.empty())
.collect(Collectors.toList());
View Cars.java
Collection<Car> markCars = people
.stream()
.filter(person -> "Mark".equals(person.getName()))
.findFirst()
.map(Person::getCars)
.filter(Optional::isPresent)
.map(Optional::get)
.orElse(Collections.emptyList());