Last active
April 26, 2017 01:00
-
-
Save 7tsuno/aed68e64bfa5e80d8a0dfaf8960fb3b1 to your computer and use it in GitHub Desktop.
流れるようなインタフェースで条件分岐 ref: http://qiita.com/7tsuno/items/33289d031fcba6952c77
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
public class AllMatch<T> { | |
private T obj; | |
private boolean match; | |
protected AllMatch(T obj, Predicate<T> checkMethod) { | |
this.match = checkMethod.test(obj); | |
this.obj = obj; | |
} | |
public AllMatch<T> and(Predicate<T> checkMethod) { | |
if (!match) { | |
return this; | |
} | |
this.match = checkMethod.test(obj); | |
return this; | |
} | |
public boolean check() { | |
return match; | |
} | |
} |
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
public final class AnyMatch<T> { | |
... | |
public <A> Else<A> then(Supplier<A> a1) { | |
return (Supplier<A> a2) -> match ? a1.get() : a2.get(); | |
} | |
} |
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
public final class Assume<T> { | |
private T obj; | |
public static <T> Assume<T> that(T obj) { | |
Assume<T> assume = new Assume<>(); | |
assume.obj = obj; | |
return assume; | |
} | |
public AnyMatch<T> satisfiesAnyOf(Predicate<T> method) { | |
AnyMatch<T> anyOf = new AnyMatch<>(obj, method); | |
return anyOf; | |
} | |
public AllMatch<T> satisfiesAllOf(Predicate<T> method) { | |
AllMatch<T> allOf = new AllMatch<>(obj, method); | |
return allOf; | |
} | |
} |
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
public interface Else<A> { | |
A orElse(Supplier<A> a2); | |
} |
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
// 文字列が空ではない、かつアルファベット小文字である場合 | |
// かつ | |
// 処理区分が登録・変更のいずれかである場合 | |
if ((StringUtils.isNotEmpty(str) && StringUtils.isAlpha(str) | |
&& StringUtils.isAllLowerCase(str)) | |
&& (processDivision.isRegister() || processDivision.isChange())) { | |
// なんか処理 | |
} |
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
// 文字列が空ではない、かつアルファベット小文字である場合 | |
// かつ | |
// 処理区分が登録・変更のいずれかである場合 | |
if (Assume.that(str) | |
.satisfiesAllOf(StringUtils::isNotEmpty) | |
.and(StringUtils::isAlpha) | |
.and(StringUtils::isAllLowerCase).check() | |
&& Assume.that(processDivision) | |
.satisfiesAnyOf(ProcessDivision::isRegister) | |
.or(ProcessDivision::isChange).check()) { | |
// なんか処理 | |
} |
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
String hoge = Assume.that(str) | |
.satisfiesAllOf(StringUtils::isNotEmpty) | |
.and(StringUtils::isAlpha) | |
.and(StringUtils::isAllLowerCase).then(() -> "aaa").orElse(() -> "bbb"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment