Predicate 的使用 (主要用于資料的邏輯判斷)
唯一性判斷:當插入相同的資料時,則報錯
非空判斷: 當插入的資料為空時,則報錯
Predicate notNullPredicate = NotNullPredicate.notNullPredicate();
相等判斷:判斷值是否相同
自定義判斷:(重寫函數)實作自定義的邏輯
//自定義謂詞
Predicate<String> selfPredicate = new Predicate<String>() {
public boolean evaluate(String s) {
return (s.length() > );
}
};
package cn.others;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.PredicateUtils;
import org.apache.commons.collections4.functors.EqualPredicate;
import org.apache.commons.collections4.functors.NotNullPredicate;
import org.apache.commons.collections4.functors.UniquePredicate;
import org.apache.commons.collections4.list.PredicatedList;
import java.util.ArrayList;
import java.util.List;
/**
* @author Duoduo
* @version 1.0
* @date 2017/4/15 12:31
*/
public class Test1 {
public static void main(String[] args) {
//自定義謂詞
Predicate<String> selfPredicate = new Predicate<String>() {
public boolean evaluate(String s) {
return (s.length() > );
}
};
Predicate notNullPredicate = NotNullPredicate.notNullPredicate();
Predicate allPredicate = PredicateUtils.allPredicate(selfPredicate, notNullPredicate);
List<String> list = PredicatedList.predicatedList(new ArrayList<String>(), allPredicate);
list.add(null);
list.add("1212");
}
public static void uniquePredicate() {
System.out.println("************** 唯一判斷 ***************");
Predicate uniquePredicate = UniquePredicate.uniquePredicate();
List<String> list = PredicatedList.predicatedList(new ArrayList<String>(), uniquePredicate);
list.add("1");
list.add("1");
}
public static void equalPredicate() {
System.out.println("************** 相等判斷 ***************");
Predicate predicate = EqualPredicate.equalPredicate("Test");
System.out.println(predicate.evaluate("Test1111111"));
}
public static void notNullPredicate() {
System.out.println("************** 非空判斷 ***************");
Predicate notNullPredicate = NotNullPredicate.notNullPredicate();
List<String> list = PredicatedList.predicatedList(new ArrayList<String>(), notNullPredicate);
list.add("11111");
list.add(null);
}
}
Transformer 類型轉化函數(例如:日期格式化)
package cn.others;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Transformer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* @author Duoduo
* @version 1.0
* @date 2017/4/15 13:00
*/
public class Test2 {
public static void main(String[] args) {
System.out.println("内置類型轉化, 日期格式化輸出");
Transformer<Long, String> transformer = new Transformer<Long, String>() {
public String transform(Long aLong) {
return new SimpleDateFormat("yyyy-MM-dd").format(aLong);
}
};
List<Long> list = new ArrayList<Long>();
list.add(L);
list.add(L);
Collection<String> collections = CollectionUtils.collect(list, transformer);
System.out.println(list);
for (String string:collections){
System.out.println(string);
}
}
}
運作結果如下:
内置類型轉化, 日期格式化輸出 [1000000000, 2000000000] 1970-01-12 1970-01-24
Predicate 和 Transformer 的結合使用(主要用于業務的轉化)
需求:當收入 < 1000 時,輸出 賣身中。。。
當收入 >= 1000 時,輸出 養身中。。。
實作思路:
1. 利用Predicate來進行資料判斷大小
2. 利用Transformer來完成類型的轉化
優勢:完成業務判斷和資料的處理的解耦
定義Employee類
package cn.others;
/**
* @author Duoduo
* @version 1.0
* @date 2017/4/15 13:08
*/
public class Employee {
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", salary=" + salary +
'}';
}
}
定義Level類
package cn.others;
/**
* @author Duoduo
* @version 1.0
* @date 2017/4/15 13:09
*/
public class Level {
private String name;
private String level;
public Level(String name, String level) {
this.name = name;
this.level = level;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
@Override
public String toString() {
return "Level{" +
"name='" + name + '\'' +
", level='" + level + '\'' +
'}';
}
}
程式處理部分
package cn.others;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.functors.SwitchTransformer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* 解耦,處理資料和判斷資料分離
* 實作思路:
* 1.定義兩個Predicate,一個為 <1000 一個為 >=1000
* 2.定義兩個Transformer,一個為 <1000 時需要轉義的處理, 一個為 >=1000 時需要轉義的處理
* 3.利用 SwitchTransformer 把 Predicate 和 Transformer 關聯起來(特别需要注意順序)
* @author Duoduo
* @version 1.0
* @date 2017/4/15 13:12
*/
public class Test3 {
public static void main(String[] args){
//定義判斷謂詞
Predicate<Employee> lowSalaryPre = new Predicate<Employee>() {
public boolean evaluate(Employee employee) {
return employee.getSalary()<;
}
};
Predicate<Employee> highSalaryPre = new Predicate<Employee>() {
public boolean evaluate(Employee employee) {
return employee.getSalary()>=;
}
};
//定義轉換器
Transformer<Employee, Level> lowTrans = new Transformer<Employee, Level>() {
public Level transform(Employee employee) {
return new Level(employee.getName(), "賣身中。。。。。");
}
};
Transformer<Employee, Level> highTrans = new Transformer<Employee, Level>() {
public Level transform(Employee employee) {
return new Level(employee.getName(), "養身中。。。。。");
}
};
Predicate[] allPre = {lowSalaryPre, highSalaryPre};
Transformer[] allTrans = {lowTrans, highTrans};
//把轉化器和判斷謂詞進行關聯
Transformer switchTrans = new SwitchTransformer(allPre, allTrans, null);
List<Employee> list = new ArrayList<Employee>();
list.add(new Employee("test1",));
list.add(new Employee("test2",));
//記性轉化
Collection<Level> collections = CollectionUtils.collect(list, switchTrans);
for (Level level : collections){
System.out.println(level);
}
}
}
運作結果
Level{name=’test1’, level=’賣身中。。。。。’}
Level{name=’test2’, level=’養身中。。。。。’}