是的,有一种方法,使用方法引用。例如,语法是,
XsamRecord::getXs
.
关联参数的类型将为
Function<XsamRecord, Integer>
.
public class SingleLengthCriteria implements XsamCriteria {
private final FilterLength filterLength;
private final Function<XsamRecord, Interger> propertyValueGetter;
public SingleLengthCriteria(FilterLength filterLength, Function<XsamRecord, Interger> propertyValueGetter) {
this.filterLength = filterLength;
this.propertyValueGetter = propertyValueGetter;
}
@Override
public List<XsamRecord> meetRecordCriteria(List<XsamRecord> records) {
List<XsamRecord> spanLengthRecords = new ArrayList<>();
for(XsamRecord record : records){
final Integer value = propertyValueGetter.apply(record);
if(value >= filterLength.getMin() && value <= filterLength.getMax()){
spanLengthRecords.add(record);
}
}
return spanLengthRecords;
}
用作:
new SingleLengthCriteria(..., XsamRecord::getXs);
new SingleLengthCriteria(..., XsamRecord::getOtherProperty);