Skip to content

Latest commit

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

lambda-type-references

Type references using lambdas

http://benjiweber.com/blog/2015/08/04/lambda-type-references/

publicstatic <T> Tcreate(TypeReference<T> type) {
returntype.newInstance();
}
@Testpublicvoidcreating_a_list() {
ArrayList<String> list = create(i->i);
list.add("hello");
list.add("world");
assertEquals(asList("hello","world"), list);
}
@Testpublicvoidcreating_a_map() {
HashMap<String, Integer> map = create(i->i);
map.put("hello",1);
map.put("world",2);
assertEquals(Integer.valueOf(1), map.get("hello"));
assertEquals(Integer.valueOf(2), map.get("world"));
}
@Testpublicvoidvariable_ref() {
TypeReference<String> ref = i->i;
assertEquals("java.lang.String", ref.type().getName());
}
@Testpublicvoidvariable_ref_generic_type() {
TypeReference<ArrayList<String>> ref = i->i;
ArrayList<String> strings = ref.newInstance();
assertEquals(Collections.emptyList(), strings);
}
@Testpublicvoidparameter_object() {
List<String> result = foo(list -> {
list.add("Hello");
list.add("World");
});
assertEquals(asList("Hello foo", "World foo"), result);
}
publicstaticList<String> foo(Parameters<ArrayList<String>> params) {
returnparams.get().stream().map(s -> s + " foo").collect(toList());
}

About

Type References using Lambdas

Resources

Stars

69 stars

Watchers

8 watching

Forks

Releases

Packages

Used by

Contributors

Languages