Skip to content

Instantly share code, notes, and snippets.

@mojo2012
Created July 5, 2019 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mojo2012/da4e8193d25b1cb92bf416493022a09d to your computer and use it in GitHub Desktop.
Save mojo2012/da4e8193d25b1cb92bf416493022a09d to your computer and use it in GitHub Desktop.
MiscUtils.java:
public static <T, C extends T> void with(T object, Class<C> asCastType, Consumer<C> consumer) {
consumer.accept((C) object);
}
I can be used like this:
BaseOpeningDay dayToBeSet = new OpeningDay();
MiscUtil.with(dayToBeSet, OpeningDay.class, d -> {
d.setWeekday(DayOfWeek.SUNDAY);
});
It allows you to both cast and assign at the same time AND it shortens the variable name to the assigned lamda argument name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment