Created
October 2, 2015 16:41
-
-
Save kencharos/1f6a95ec233d8d4d9327 to your computer and use it in GitHub Desktop.
xecute lanmda statement if value is not null or empty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.co.ulsystems.javafx; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.function.Consumer; | |
/** | |
* execute lanmda statement if value is not null or empty | |
* | |
* this replace following code | |
* | |
* if (bean.getHoge() != null) {bean2.setHoge(bean.getHoge());} | |
* | |
*/ | |
public class Main { | |
public static class IfAction { | |
public static IfAction of() { | |
return new IfAction(); | |
} | |
<I> IfAction then(I in, Consumer<I> cons) { | |
if (in != null) { | |
if (!(in instanceof String) || in.toString().trim().length() != 0) | |
cons.accept(in); | |
} | |
return this; | |
} | |
} | |
public static void main(String[] args) { | |
StringBuilder sb = new StringBuilder(); | |
Map<String , String> map = new HashMap<>(); | |
map.put("A", "AA"); | |
IfAction.of() | |
.then("222", in -> sb.append(in)) | |
.then(" ", in -> sb.append(in)) // not execute | |
.then(Long.valueOf("345"), in -> sb.append(in)) | |
.then(null, in -> sb.append(in)) // not execute | |
.then(map.get("A"), in -> sb.append(in)) | |
.then(map.get("B"), in -> sb.append(in)) // not execute | |
.then(Long.valueOf("567"), in -> sb.append(in)); | |
System.out.println(sb.toString()); // 22234AA5567 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment