commons的Predicate和Transformer——高淇JAVA300讲笔记之commons

案例一:Predicate 断言

 1 package com.bjsxt.others.commons;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import org.apache.commons.collections4.Predicate;
 7 import org.apache.commons.collections4.PredicateUtils;
 8 import org.apache.commons.collections4.functors.EqualPredicate;
 9 import org.apache.commons.collections4.functors.NotNullPredicate;
10 import org.apache.commons.collections4.functors.UniquePredicate;
11 import org.apache.commons.collections4.list.PredicatedList;
12 
13 
14 /**
15  * 函数式编程之Predicate 断言
16  * 封装条件或判别式 if..else替代
17  * 1、new EqualPredicate<类型>(值)
18  *    EqualPredicate.equalPredicate(值)
19  *    
20  * 2、NotNullPredicate.INSTANCE
21  * 3、UniquePredicate.uniquePredicate()
22  * 4、自定义
23  *    new Predicate() + 重写evaluate()方法
24  * 
25  *       PredicateUtils.allPredicate andPredicate anyPredicate
26  *       PredicatedXxx.predicatedXxx(容器,判断)
27  * 
28  * 
29  */
30 public class Demo01 {
31     public static void main(String[] args) {
32         System.out.println("======自定义判断==========");
33         //自定义的判别式
34         Predicate<String> selfPre = new Predicate<String>() {
35             @Override
36             public boolean evaluate(String object) {
37                 return object.length() >=5 && object.length() <=20;
38                 
39             }
40             
41         };
42         Predicate notNull = NotNullPredicate.notNullPredicate();
43         
44         Predicate all = PredicateUtils.allPredicate(selfPre,notNull);
45         
46         List<String> list = PredicatedList.predicatedList(new ArrayList<String>(), all);
47         list.add("bjsxt");
48 //        list.add(null);  //会抛异常
49         list.add("bj");  //长度不符合,也会抛异常
50         
51         
52     }
53     
54     
55     /**
56      * 判断唯一
57      */
58     public static void unique() {
59         System.out.println("========唯一性判断========");
60         Predicate<Long> uniquePre = UniquePredicate.uniquePredicate();
61         List<Long> list = PredicatedList.predicatedList(new ArrayList<Long>(),uniquePre);
62         list.add(100L);
63         list.add(200L);
64         list.add(100L);  //出现重复值,抛出异常
65     }
66     
67     /**
68      * 判断非空
69      */
70     public static void notNull() {
71         System.out.println("=========非空判断========");
72 //        Predicate notNull = NotNullPredicate.INSTANCE;
73         Predicate notNull = NotNullPredicate.notNullPredicate();
74 //        String str = "bjs";
75         String str = null;
76         System.out.println(notNull.evaluate(str));  //如果非空为true,否则为false
77     
78         //添加容器值的判断
79         List<Long> list = PredicatedList.predicatedList(new ArrayList<Long>(),notNull);
80         list.add(1000L);
81 //        list.add(null);  //会抛异常
82     }
83     
84     
85     /**
86      * 比较相等判断
87      */
88     public static void equal() {
89         System.out.println("========相等判断=========");
90 //        Predicate<String> pre = new EqualPredicate<String>("bjsxt");
91         Predicate<String> pre = EqualPredicate.equalPredicate("bjsxt");
92         boolean flag = pre.evaluate("bj");
93         System.out.println(flag);
94     }
95 }

案例二:Transformer 类型转化

先写一个Employee类

 1 package com.bjsxt.others.commons;
 2 
 3 /**
 4  * 员工类
 5  *
 6  */
 7 public class Employee {
 8     private String name;
 9     private double salary;
10     //无参构造器
11     public Employee() {
12         super();
13     }
14     //有参构造器
15     public Employee(String name, double salary) {
16         super();
17         this.name = name;
18         this.salary = salary;
19     }
20     //setter和getter方法
21     public String getName() {
22         return name;
23     }
24     public void setName(String name) {
25         this.name = name;
26     }
27     public double getSalary() {
28         return salary;
29     }
30     public void setSalary(double salary) {
31         this.salary = salary;
32     }
33     @Override
34     public String toString() {
35         return "(码农:"+ this.name+",敲砖钱:"+this.salary+")";
36     }
37     
38 }

再写一个Level类

 1 package com.bjsxt.others.commons;
 2 
 3 /**
 4  * 等级类
 5  *
 6  */
 7 public class Level {
 8     private String name;
 9     private String level;
10     //无参构造器
11     public Level() {
12         super();
13     }
14     //有参构造器
15     public Level(String name, String level) {
16         super();
17         this.name = name;
18         this.level = level;
19     }
20     //setter和getter方法
21     public String getName() {
22         return name;
23     }
24     public void setName(String name) {
25         this.name = name;
26     }
27     public String getLevel() {
28         return level;
29     }
30     public void setLevel(String level) {
31         this.level = level;
32     }
33     @Override
34     public String toString() {
35         return "(码农:"+this.name+",水平:"+this.level+")";
36     }
37         
38 }

最后是主函数

  1 package com.bjsxt.others.commons;
  2 
  3 import java.text.SimpleDateFormat;
  4 import java.util.ArrayList;
  5 import java.util.Collection;
  6 import java.util.Iterator;
  7 import java.util.List;
  8 
  9 import org.apache.commons.collections4.CollectionUtils;
 10 import org.apache.commons.collections4.Predicate;
 11 import org.apache.commons.collections4.Transformer;
 12 import org.apache.commons.collections4.functors.SwitchTransformer;
 13 
 14 /**
 15  * 解耦:业务处理与判断进行分离
 16  * 函数式编程 Transformer 类型转化
 17  * 1、new Transformer() + 重写transform方法
 18  * 2、SwitchTransformer
 19  * CollectionUtils.collect(容器,转换器)
 20  * 
 21  */
 22 public class Demo02 {
 23     public static void main(String[] args) {
 24         System.out.println("===自定义类型转换===");
 25         //判别式
 26         Predicate<Employee> isLow = new Predicate<Employee>() {
 27 
 28             @Override
 29             public boolean evaluate(Employee emp) {
 30                 return emp.getSalary() < 10000;
 31             }
 32             
 33         };
 34         Predicate<Employee> isHigh = new Predicate<Employee>() {
 35 
 36             @Override
 37             public boolean evaluate(Employee emp) {
 38                 return emp.getSalary() >= 10000;
 39             }
 40             
 41         };
 42         Predicate[] pres = {isLow,isHigh};
 43         
 44         //转换
 45         Transformer<Employee,Level> lowTrans = new Transformer<Employee,Level>(){
 46 
 47             @Override
 48             public Level transform(Employee input) {
 49                 return new Level(input.getName(),"卖身中");
 50             }        
 51         };
 52         //转换
 53         Transformer<Employee,Level> highTrans = new Transformer<Employee,Level>(){
 54 
 55             @Override
 56             public Level transform(Employee input) {
 57                 return new Level(input.getName(),"养身中");
 58             }        
 59         };
 60         Transformer[] trans = {lowTrans,highTrans};
 61         
 62         //二者进行了关联
 63         Transformer switchTrans = new SwitchTransformer(pres, trans, null);
 64         
 65         //容器
 66         List<Employee> list = new ArrayList<Employee>();
 67         list.add(new Employee("老马",1000000));
 68         list.add(new Employee("老裴",999));
 69         
 70         Collection<Level> levelList = CollectionUtils.collect(list, switchTrans);
 71         
 72         //遍历容器
 73         Iterator<Level> levelIt = levelList.iterator();
 74         while(levelIt.hasNext()) {
 75             System.out.println(levelIt.next());
 76         }
 77         
 78     }
 79     
 80     /**
 81      * 内置类型的转换
 82      */
 83     public static void inner() {
 84         System.out.println("===内置类型转换 长整型时间日期,转成指定格式的字符串===");
 85         Transformer<Long,String> trans = new Transformer<Long,String>(){
 86 
 87             @Override
 88             public String transform(Long input) {
 89                 return new SimpleDateFormat("yyyy年MM月dd日").format(input);
 90             }        
 91         };
 92         //容器
 93         List<Long> list = new ArrayList<Long>();
 94         list.add(999999999999L);
 95         list.add(300000000L);
 96         
 97         //工具类 程序员出钱 --- 开发商 --- 农民工出力
 98         Collection<String> result = CollectionUtils.collect(list, trans);
 99         //遍历查看结果
100         for(String time:result) {
101             System.out.println(time);
102         }
103         
104     }
105 }

运行结果:

===自定义类型转换===
(码农:老马,水平:养身中)
(码农:老裴,水平:卖身中)
原文地址:https://www.cnblogs.com/swimminglover/p/8372555.html