UserByNameLike.java
上传用户:hensond
上传日期:2021-12-27
资源大小:817k
文件大小:1k
- package com.cbf4life.common5;
- /**
- * @author cbf4Life cbf4life@126.com
- * I'm glad to share my knowledge with you all.
- */
- public class UserByNameLike implements IUserSpecification {
- //like的标记
- private final static String LIKE_FLAG = "%";
- //基准的like字符串
- private String likeStr;
- //构造函数传递基准姓名
- public UserByNameLike(String _likeStr){
- this.likeStr = _likeStr;
- }
-
- //检验用户是否满足条件
- public boolean IsSatisfiedBy(User user) {
- boolean result = false;
- String name = user.getName();
- //替换掉%后的干净字符串
- String str = likeStr.replace("%","");
- //是以名字开头,如'国庆%'
- if(likeStr.endsWith(LIKE_FLAG) && !likeStr.startsWith(LIKE_FLAG)){
- result = name.startsWith(str);
- }else if(likeStr.startsWith(LIKE_FLAG) && !likeStr.endsWith(LIKE_FLAG)){ //类似 '%国庆'
- result = name.endsWith(str);
- }else{
- result = name.contains(str); //类似于'%国庆%'
- }
-
- return result;
- }
- }