public class Pattern extends Object
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static PatternString |
CONTAIN |
|
static PatternDefault |
DEFAULT |
|
static PatternString |
ICCONTAIN |
|
static PatternString |
ICPREFIX |
|
static PatternString |
ICSUFFIX |
|
static PatternString |
IGNORECASE |
|
static None |
NONE |
|
static PatternString |
PREFIX |
|
static PatternString |
STRING |
|
static PatternString |
SUFFIX |
|
static PatternType |
TYPE |
|
static PatternValue |
VALUE |
|
static PatternNull1 |
VALUE1 |
|
static PatternNull2 |
VALUE2 |
|
static PatternNull3 |
VALUE3 |
|
static PatternNull4 |
VALUE4 |
|
static PatternNull5 |
VALUE5 |
|
static PatternNull6 |
VALUE6 |
|
static PatternNull7 |
VALUE7 |
|
static PatternNull8 |
VALUE8 |
| 构造器 | 说明 |
|---|---|
Pattern() |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
static <T> PatternIn<T> |
in(T... values) |
|
static BooleanMatcherMapping<None> |
match() |
There is multiple
if statements, but they're not related,
use match() without value. |
static BooleanMatcherMapping<None> |
match(None value) |
|
static <T> ActionNoneMatcherMapping<T> |
match(R1<? super T,Boolean> preAction,
Class<T> clazz) |
|
static ClassValueMatcherMapping<Class<?>> |
match(Class<?> clazz) |
|
static StringMatcherMapping |
match(String value,
PatternString patternString) |
Pattern matching for String.
|
static <V> ValueMatcherMapping<V> |
match(V value) |
match can instead of switch statement or if statement. |
static <V,T> ActionValueMatcherMapping<V,T> |
match(V value,
R1<? super T,V> preAction,
Class<T> clazz) |
|
static <V> ActionValueMatcherMapping<V,V> |
match(V value,
R1<? super V,V> preAction) |
The values in
.when(value) are preprocessed by preAction and then match. |
static <V> ValueMatcherMapping<V> |
match(V value,
PatternDefault patternDefault) |
|
static <V> TypeMatcherMapping<V> |
match(V value,
PatternType patternType) |
Use Type Matcher instead of
instanceof. |
static <V> ValueMatcherMapping<V> |
match(V value,
PatternValue patternValue) |
|
static NullMatcherMapping<None> |
matchNull() |
适用于判断多个值是否为null值(或其他终止条件),只要其中一个值满足终止条件,则立即
return 方法,终止后续语句运算。 |
public static final PatternDefault DEFAULT
public static final PatternValue VALUE
public static final PatternType TYPE
public static final PatternString STRING
public static final PatternString IGNORECASE
public static final PatternString CONTAIN
public static final PatternString PREFIX
public static final PatternString SUFFIX
public static final PatternString ICCONTAIN
public static final PatternString ICPREFIX
public static final PatternString ICSUFFIX
public static None NONE
public static final PatternNull1 VALUE1
public static final PatternNull2 VALUE2
public static final PatternNull3 VALUE3
public static final PatternNull4 VALUE4
public static final PatternNull5 VALUE5
public static final PatternNull6 VALUE6
public static final PatternNull7 VALUE7
public static final PatternNull8 VALUE8
public static <V> ValueMatcherMapping<V> match(V value)
match can instead of switch statement or if statement. String s = "5"; String result = match(s) .when("1", v -> v + v) .when("2", v -> v + "a") .when(in("3", "4", "5", "6"), v -> v + " - abcd") .orElse(v -> "no match"); System.out.println("match result: " + result);
V - value typevalue - valuepublic static <V> ValueMatcherMapping<V> match(V value, PatternDefault patternDefault)
public static <V> ValueMatcherMapping<V> match(V value, PatternValue patternValue)
public static BooleanMatcherMapping<None> match()
if statements, but they're not related,
use match() without value. int i = 10; String s = "abc"; Object o = new Object(); String res = match() .when(i == 5, v -> "i == 5") .when(s.equals("abc"), v -> "abc") .when(o == null, v -> "object is null") .orElse(v -> null);
match(None)public static BooleanMatcherMapping<None> match(None value)
public static <V> TypeMatcherMapping<V> match(V value, PatternType patternType)
instanceof. match() 函数可替代 instanceof 类型检测与类型转换功能。It is equivalent to the code below:Object o = Tuple.of("zs", 20); Integer result = match(o, TYPE) .when(Integer.class, v -> v + 10) .when(Tuple2.class, v -> v.arity()) .when(String.class, v -> v.contains("abc") ? 20 : 30) .orElse(v -> 40);
Integer ifResult; if (o instanceof Integer) { ifResult = (Integer) o + 10; } else if (o instanceof Tuple2) { ifResult = ((Tuple2) o).arity(); } else if (o instanceof String) { ifResult = ((String) o).contains("abc") ? 20 : 30; } else { ifResult = 40; }
V - value typevalue - valuepatternType - PatternTypepublic static StringMatcherMapping match(String value, PatternString patternString)
String str = "aBcdE123.$fGHIj"; // ignore case match String res1 = match(str, IGNORECASE) .when((String) null, v -> "match null") .when("abcd", v -> "match abcd") .when("abcde123.$fGHIj", v -> "ignore case match") // match this .orElse(v -> "no match"); // CONTAIN match String res2 = match(str, CONTAIN) .when("abcd", v -> "abcd") .when("E123", v -> "E123") // match this .orElse(v -> "no match"); // ignore case for contain String res3 = match(str, ICCONTAIN) .when("abcd1", v -> "abcd1") .when(in(null, "aaa", ".$fghi", "123"), v -> ".$fghi") // match this .orElse(v -> "no match"); // PREFIX String res4 = match(str, PREFIX) .when("abcd", v -> "abcd") .when("aBcd", v -> "aBcd") // match this .orElse(v -> "no match"); // ignore case for suffix String res5 = match(str, ICSUFFIX) .when("fghij", v -> "fGHIj") // match this .when("aBcd", v -> "aBcd") .orElse(v -> "no match");
value - valuepatternString - PatternStringpublic static ClassValueMatcherMapping<Class<?>> match(Class<?> clazz)
public static <V> ActionValueMatcherMapping<V,V> match(V value, R1<? super V,V> preAction)
.when(value) are preprocessed by preAction and then match. String str = "123abc"; R1<String, String> preAction = s -> "123" + (s == null ? null : s.toLowerCase()); String res1 = match(str, preAction, String.class) .when("123", v -> "1 " + v + "-- 123") .when("123ABC", v -> "2 " + v + "-- 123ABC") .when("ABC", v -> "4 " + v + "-- ABC") // will be matched .orElse(v -> "orElse " + v); System.out.println(res1); // output: 4 123abc-- ABC
V - value typevalue - valuepreAction - Preprocess for value in .when(value)public static <V,T> ActionValueMatcherMapping<V,T> match(V value, R1<? super T,V> preAction, Class<T> clazz)
public static <T> ActionNoneMatcherMapping<T> match(R1<? super T,Boolean> preAction, Class<T> clazz)
public static NullMatcherMapping<None> matchNull()
return 方法,终止后续语句运算。public String patternCheckNull() { Account account = new Account("12345", "", "aaaabbbb"); Order order = new Order("order_123456", 10.5, new User("zs", 10, account)); Tuple7<User, Account, String, Double, String, Integer, String> values = matchNull() .whenV(order, v -> v.buyer, "order is null or order.buyer is null!") .whenW(VALUE1, v -> v.account, v -> "user " + v._1.name + "'s account is null!") .whenV(order, v -> v.orderId, G::isBlank, "order.orderId is blank!") .whenV(order, v -> v.price, v -> v < 0, "order.price < 0!") .whenW(VALUE2, v -> v.userName, G::isEmpty, "order.buyer.account.userName is empty!") .whenW(VALUE1, v -> v.age, v -> v < 0, "order.buyer.age < 0!") .orElse(null); User user = values._1; Account account1 = values._2; String orderId = values._3; Double price = values._4; String userName = values._5; Integer age = values._6; String msg = values._7; return msg; } </pre> <b>It is equivalent to the code below: </b><br><br> <pre> public String commonCheckNull() { Account account = new Account("12345", "", "aaaabbbb"); Order order = new Order("order_123456", 10.5, new User("zs", 10, account)); String msg = null; if (order == null || order.buyer == null) { return "order is null or order.buyer is null!"; } User user = order.buyer; if (user.account == null) { return "user " + user.name + "'s account is null!"; } if (G.isBlank(order.orderId)) { return "order.orderId is blank!"; } if (order.price < 0) { return "order.price < 0!"; } if (G.isEmpty(user.account.userName)) { return "order.buyer.account.userName is empty!"; } if (user.age < 0) { return "order.buyer.age < 0!"; } return null; }
@SafeVarargs public static <T> PatternIn<T> in(T... values)
Copyright © 2023 io-fairy. All rights reserved.