I have a method create(Environment env) in Java 8, which have multiple statements. Now I need to rewrite method to add support of new migration functionality.
To add support of migration just means:
If
Environment envobject has fieldmigrationset totruedon't execute some code increatefunction.
That's why I wrap these code blocks:
protected Environment create(Environment env)
{
statements;
if (!env.isForMigrate()) {
// executed only if it's NOT a migration
statements;
// for example: imh.create(ve)
// or: newEnv.setAps(env.getAps());
}
...
statements;
if (!env.isForMigrate()) {
// executed only if it's NOT a migration
statements;
}
...
and so on...
}
These code blocks I widespread all over create function.
Thus I have to add conditional execution for multiple code blocks.
Can I get some advantages in case of using lambda expressions for this? Is there any pattern in Java 8?
My intention is to write something like this:
final Predicate<T> forMigrate = (func) -> {
// closure for Environment env
if (env.isForMigrate()) {
func(); // execute passed statements
}
}
...
forMigrate({
Environment newEnv = apsh().envh().im2aps(ve);
newEnv.setAps(env.getAps());
newEnv.setOsId(env.getOsId());
});
Thus I want to get lambda expression, to which I could pass any block of code. And lambda expression will execute these statements only if it's not a migration.
- How can I write this
forMigratelambda function? - Is there any advantages of using lambda expressions vs old
if (...) {}statements in this example?
Note:
- I don't control
Environmentclass, it's auto-generated from XML file. - I want to maximum restrict scope of
forMigration- only insidecreate(don't make it visible anywhere) - that's why I want to assign lambda expression to variable:final ... forMigrate = (...) -> { ... }. - I want to use lexical scoping for
Environment, dont' pass it directly to lambda. Use it from where lambda is defined.
Original function create:
protected Environment create(Environment env)
{
if(env.getHostname()!=null && env.getHostname().endsWith(".")){
String normalizedHostname = env.getHostname().substring(0, env.getHostname().length() - 1);
env.setHostname(normalizedHostname);
}
Ve ve = apsh().envh().aps2im(env);
if (ve.getHostname() == null) {
ve.setHostname(ve.getName());
}
List<String> apps = env.getApps();
Boolean passwordSet = false;
imh.create(ve);
ve = imh.getVe(ve.getCustomerId().intValue(), ve.getName());
if(env.getPassword()!= null && !env.getPassword().isEmpty()){
try{
imh.setVePassword(ve.getCustomerId(), ve.getName(), env.getPassword());
passwordSet = true;
} catch(Exception ex){
logger.error("Failed to set password for VE: " + env.getName(), ex);
}
}
if (!apps.isEmpty()) {
try {
imh.setVeApps(ve.getCustomerId().intValue(), ve.getName(), apps);
} catch (Exception ex) {
logger.error("Failed to install applications VE: {}", ex);
}
}
VeFacade vef = vehFactory.create(ve.getCustomerId(), ve.getName());
vef.operation("start");
ve = imh.getVe(ve.getCustomerId().intValue(), ve.getName());
Environment newEnv = apsh().envh().im2aps(ve);
newEnv.setAps(env.getAps());
newEnv.setOsId(env.getOsId());
newEnv.setSample(env.getSample());
newEnv.setHosting(env.getHosting());
newEnv.setDomain(env.getDomain());
newEnv.getStatus().setUptime(Long.valueOf(new Date().getTime()));
newEnv.setPassword(null); //prevent password from being saved in DB
newEnv.setPasswordSet(passwordSet);
apsh().envh().fillOsData(newEnv, apsh().teh().getOs(newEnv.getOsId()));
apsh().envh().synchPublicAddresses(newEnv, ve);
apsh().dnsh().synchDomainRecords(newEnv);
logger.info("Environment '{}' successfully created", newEnv.getName());
return newEnv;
}
How I would rewrite it in old Java 7 style:
protected Environment create(Environment env)
{
if(env.getHostname()!=null && env.getHostname().endsWith(".")){
String normalizedHostname = env.getHostname().substring(0, env.getHostname().length() - 1);
env.setHostname(normalizedHostname);
}
Ve ve = apsh().envh().aps2im(env);
if (ve.getHostname() == null) {
ve.setHostname(ve.getName());
}
List<String> apps = env.getApps();
Boolean passwordSet = false;
// NOTE: Wrap block of code
if (env.isForMigrate() == false) {
imh.create(ve);
}
ve = imh.getVe(ve.getCustomerId().intValue(), ve.getName());
if(env.getPassword()!= null && !env.getPassword().isEmpty()){
try{
imh.setVePassword(ve.getCustomerId(), ve.getName(), env.getPassword());
passwordSet = true;
} catch(Exception ex){
logger.error("Failed to set password for VE: " + env.getName(), ex);
}
}
if (!apps.isEmpty()) {
try {
imh.setVeApps(ve.getCustomerId().intValue(), ve.getName(), apps);
} catch (Exception ex) {
logger.error("Failed to install applications VE: {}", ex);
}
}
// NOTE: Wrap block of code
if (env.isForMigrate() == false) {
VeFacade vef = vehFactory.create(ve.getCustomerId(), ve.getName());
vef.operation("start");
}
ve = imh.getVe(ve.getCustomerId().intValue(), ve.getName());
Environment newEnv = apsh().envh().im2aps(ve);
// NOTE: Wrap block of code
if (env.isForMigrate() == false) {
newEnv.setAps(env.getAps());
newEnv.setOsId(env.getOsId());
newEnv.setSample(env.getSample());
}
newEnv.setHosting(env.getHosting());
newEnv.setDomain(env.getDomain());
newEnv.getStatus().setUptime(Long.valueOf(new Date().getTime()));
newEnv.setPassword(null); //prevent password from being saved in DB
newEnv.setPasswordSet(passwordSet);
apsh().envh().fillOsData(newEnv, apsh().teh().getOs(newEnv.getOsId()));
apsh().envh().synchPublicAddresses(newEnv, ve);
apsh().dnsh().synchDomainRecords(newEnv);
logger.info("Environment '{}' successfully created", newEnv.getName());
return newEnv;
}