先引入实例中需要的实体类,实体类中的get/set方法省略掉了
public class State {
private String name;
private String code;
private String region;
private Set<City> mainCities=new HashSet<City>();
public State(){}
public State(String name){
this.name=name;
}
}
public class City { private String name; private String zipCode; private Integer population; private String climate; private double averageRainfall; public City(){} public City(String name){ this(name,null,null); } public City(String name,String zipCode,Integer population){ this.name=name; this.zipCode=zipCode; this.population=population; }}
一个Function接口的实现:
public class StateToCityString implements Function<State,String> {
@Override
public String apply(State input) {
return Joiner.on(",").join(input.getMainCities());
}
}
Function TestCase:
public class FunctionTest {
@Test
public void test(){
Map<String,State> stateMap=new HashMap<String,State>();
State s=new State("New York");
s.addCity("new");
s.addCity("york");
stateMap.put("NY",s);
//Functions.forMap 接收一个Map集合作为参数,返回一个Function,用于执行Map集合的查找
Function<String,State> lookup= Functions.forMap(stateMap);
Function<State,String> stateFunction=new StateToCityString();
//Function<A, C> compose(Function<B, C> g, Function<A, ? extends B> f)
//接收两个Function作为参数,返回两个Function的组合
//f的输出会作为g的输入,g输出为最终作为compose的输出
Function<String,String> composed=Functions.compose(stateFunction,lookup);
String str=composed.apply("NY");
System.out.println(str);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Predicate
先来几个Predicate接口的实现:
public class LowRainfallPredicate implements Predicate<City> {
@Override
public boolean apply(City input) {
return input.getAverageRainfall()<45.7;
}
}
public class PopulationPredicate implements Predicate<City> {
@Override
public boolean apply(City input) {
return input.getPopulation()<=50000;
}
}
public class TemperateClimatePredicate implements Predicate<City>{ @Override public boolean apply(City input) { return input.getClimate().equals("gogo"); }}
public class SouthwestOrMidwestRegionPredicate implements Predicate<State> { @Override public boolean apply(State input) { return input.getRegion().equals("MIDEWEST")||input.getRegion().equals("SOUTHWEST"); }}
Predicate TestCase:
public class PredicateDemo {
@Test
public void test(){
TemperateClimatePredicate temperateClimatePredicate=new TemperateClimatePredicate();
LowRainfallPredicate lowRainfallPredicate=new LowRainfallPredicate();
PopulationPredicate smallPopulationPredicate=new PopulationPredicate();
//Predicates.and
Predicate smallAndDry= Predicates.and(smallPopulationPredicate,lowRainfallPredicate);
//Predicates.or
Predicate smallTemperate=Predicates.or(smallPopulationPredicate,temperateClimatePredicate);
//Predicates.not
Predicate largeCityPredicate=Predicates.not(smallPopulationPredicate);
//Predicates.compose
//Predicate<A> compose(Predicate<B> predicate, Function<A, ? extends B> function)
//底层调用:predicate.apply(function.apply(A))
Map<String,State> stateMap=new HashMap<String,State>();
State s=new State("New York");
s.setRegion("MIDEWEST");
s.addCity("new");
s.addCity("york");
stateMap.put("NY",s);
Function<String,State> lookup= Functions.forMap(stateMap);
SouthwestOrMidwestRegionPredicate southwestOrMidwestRegionPredicate=new SouthwestOrMidwestRegionPredicate();
Predicate<String> predicate=Predicates.compose(southwestOrMidwestRegionPredicate,lookup);
System.out.println(predicate.apply("NY"));
}
}
Function和Predicate是非常相似的接口,在实现Function和Predicate的apply方法时,不要对输入数据进行修改。
Function和Predicate都有下面两个方法:
T apply(@Nullable F input);
boolean equals(@Nullable Object object);
Functions.compose(FunctionA,FunctionB) 用途:可以对不同的函数进行组合,将FunctionB的输出作为FunctionA的输入进行再处理。可以实现嵌套的数据处理操作。
实现不同的Predicate,可以对简单的校验操作进行复用,可以用于数据过滤等。
Predicates.compose 将Predicate和Function进行组合。将Function的结果作为Predicate的输入,然后进行判断过滤操作。
仅理解到此程度,欢迎大家拍砖!深入的学习还是推荐大家看源码!
参考资料:《Getting started with google guava》