| structions [1] = maxSpeedSelectInstruction;
3.创建两个条件对象,分别对应于用户选择的条件:
final Color color = getColorFromUserInput();
Condition colorCondition = new Condition {
public boolean passes(Object o){
return ((Car)o).getColor().equals(color);
}
};
final int maxSpeed = getSpeedFromUserInput();
Condition maxSpeedCondition = new Condition {
public boolean passes(Object o){
return ((Car)o).getMaxSpeed() >= maxSpeed;
}
}
然后把这些Condition对象加入一个Condition[]:
Condition[] conditions = new Condition[2];
conditions[0] = colorCondition;
conditions[1] = maxSpeedCondition;
4.创建两个OrderInstruction对象,分别对应于各个排序标准:
//Note: We supply a null Comparator for this OrderInstruction, since
//the default ordering behavior of String suits our purposes.
//We can also reuse the invokers we defined for the SelectInstructions.
OrderInstruction colorOrderInstruction = new OrderInstruction(colorInvoker, null);
//Note: We supply a custom Comparator for this OrderInstruction, since the
//default ordering behavior of Integer (lower number first) does not suit our purposes.
//We want to show the higher speed at the top! (This corresponds to specifying DESC for order by
//in SQL.)
OrderInstruction speedOrderInstruction = new OrderInstruction(speedInvoker, new Comparator(){
public int compare(Object o1, Object o2){
if(o1 == null && o2 == null)return 0;
if(o1 == null) return - 1;
if(o2 == null) return 1;
//By comparing o2 to o1, we get the opposite order.
return ((Integer)o2.compareTo(o1);
}
});
上一页 [1] [2] [3] [4] [5] [6] [7] [8] 下一页 |