ActionUtils工具类
基于lambda表达式写的工具类
1、定义函数接口Action
package codeAssistant.service;
/**
* @author: yc
* @des: 自定义函数式接口
* @date: 2021/11/5 17:13
*/
@FunctionalInterface
public interface Action {
/**
* @author: yc
* @des: 执行动作
* @date: 2021/11/5 17:14
*/
void doAction();
}
2、Actionutils工具类
package codeAssistant.util;
import codeAssistant.service.Action;
import org.apache.commons.lang3.StringUtils;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
/**
* @author: yc
* @des: action工具类
* @date: 2021/11/5 17:15
*/
public class ActionUtils {
/**
* @author: yc
* @des: 字符串不为空动作
* @date: 2021/11/5 17:16
*/
public static void doIfStrNotBlank(String str, Action action){
if(StringUtils.isNotBlank(str)){
action.doAction();
}
}
/**
* @author: yc
* @des: 字符串为空动作
* @date: 2021/11/5 17:17
*/
public static void doIfStrBlank(String str,Action action){
if(StringUtils.isBlank(str)){
action.doAction();
}
}
/**
* @author: yc
* @des: 字符串不为空执行动作一或者二
* @date: 2021/11/5 17:19
*/
public static void doIfStrNotBlankOrNot(String str,Action action1,Action action2){
if(StringUtils.isNotBlank(str)){
action1.doAction();
}else{
action2.doAction();
}
}
/**
* @author: yc
* @des: 对象为空执行动作
* @date: 2021/11/5 17:22
*/
public static void doIfObjIsNull(Object obj,Action action){
if(Objects.isNull(obj)){
action.doAction();
}
}
/**
* @author: yc
* @des: 对象不为空执行动作
* @date: 2021/11/5 17:24
*/
public static void doIfObjIsNotNull(Object obj,Action action){
if(!Objects.isNull(obj)){
action.doAction();
}
}
/**
* @author: yc
* @des: 对象为空执行动作一否则二
* @date: 2021/11/5 17:26
*/
public static void doIfObjNullOrNot(Object obj,Action action1,Action action2){
if(Objects.isNull(obj)){
action1.doAction();
}else{
action2.doAction();
}
}
/**
* @author: yc
* @des: 集合为空执行动作
* @date: 2021/11/5 17:28
*/
public static void doIfCollectionIsEmpty(Collection<?> collection, Action action){
if(collection.isEmpty()){
action.doAction();
}
}
/**
* @author: yc
* @des: 集合不为空执行动作
* @date: 2021/11/5 17:30
*/
public static void doIfCollectionIsNotEmpty(Collection<?> collection,Action action){
if(!collection.isEmpty()){
action.doAction();
}
}
/**
* @author: yc
* @des: 集合为空执行动作一否则二
* @date: 2021/11/5 17:32
*/
public static void doIfCollectionIsEmptyOrNot(Collection<?> collection,Action action1,Action action2){
if(collection.isEmpty()){
action1.doAction();
}else{
action2.doAction();
}
}
/**
* @author: yc
* @des: map为空执行动作
* @date: 2021/11/5 17:34
*/
public static void doIfMapIsEmpty(Map<?,?> map, Action action){
if(map.isEmpty()){
action.doAction();
}
}
/**
* @author: yc
* @des: map不为空执行动作
* @date: 2021/11/5 17:35
*/
public static void doIfMapIsNotEmpty(Map<?,?> map,Action action){
if(!map.isEmpty()){
action.doAction();
}
}
/**
* @author: yc
* @des: map为空执行动作一否则二
* @date: 2021/11/5 17:36
*/
public static void ifMapIsEmptyOrNot(Map<?,?> map,Action action1,Action action2){
if(map.isEmpty()){
action1.doAction();
}else{
action2.doAction();
}
}
/**
* @author: yc
* @des: str1与st2忽视大小写equal执行动作
* @date: 2021/11/5 17:38
*/
public static void doIfStr1IgnoreSt2Equal(String str1,String str2,Action action){
if(str1.equalsIgnoreCase(str2)){
action.doAction();
}
}
/**
* @author: yc
* @des: 为true执行动作
* @date: 2021/11/5 17:40
*/
public static void doIfTrue(Boolean flag,Action action){
if(flag){
action.doAction();
}
}
/**
* @author: yc
* @des: 为false执行动作
* @date: 2021/11/5 17:41
*/
public static void doIfFalse(Boolean flag,Action action){
if(!false){
action.doAction();
}
}
/**
* @author: yc
* @des: 为true执行动作一否则二
* @date: 2021/11/5 17:43
*/
public static void doIfTrueOrNot(Boolean flag,Action action1,Action action2){
if(flag){
action1.doAction();
}else {
action2.doAction();
}
}
/**
* @author: yc
* @des: str不为手机号执行动作
* @date: 2021/11/9 15:12
*/
public static void doIfStrIsNotPhone(String str,Action action){
if(!Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$").matcher(str).matches()){
action.doAction();
}
}
/**
* @author: yc
* @des: str不为座机号执行动作
* @date: 2021/11/9 15:15
*/
public static void doIfStrIsNotMobile(String str,Action action){
String rule = str.length() > 9 ? "^[0][1-9]{2,3}-[0-9]{5,10}$" : "^[1-9]{1}[0-9]{5,8}$";
if(!Pattern.compile(rule).matcher(str).matches()){
action.doAction();
}
}
/**
* @author: yc
* @des: str既不是手机又不是座机号执行动作
* @date: 2021/11/9 15:21
*/
public static void doIfStrIsNotPhoneAndMobile(String str,Action action){
String rule = "^[1][3,4,5,7,8][0-9]{9}$";
if(Pattern.compile(rule).matcher(str).matches()){
return;
}
rule = str.length() > 9 ? "^[0][1-9]{2,3}-[0-9]{5,10}$" : "^[1-9]{1}[0-9]{5,8}$";
if(!Pattern.compile(rule).matcher(str).matches()){
action.doAction();
}
}
/**
* @author: yc
* @des: str中包含target执行动作
* @date: 2021/11/16 14:10
*/
public static void doIfStrContainsTarget(String str,String target,Action action){
if(str.contains(target)){
action.doAction();
}
}
}