inject()方法有强大的累计功能,以闭包作为参数处理一个数据结构并返回一个值。inject()方法的第一个参数是第二个参数(闭包)的立即返回值,无需引入额外的变量就能实现累积计算,例如统计图书馆所有丛书库存,每套丛书有不同套数,每套丛书有不同册数和库存数量,业务模型如下
public class Library {
private List bookSeriesList;
// getter and setter
}
public class BookSeries {
private int suitNum;
private List books;
// getter and setter
}
public class Book {
private String name;
private int store;
private double price;
// getter and setter
}
因了inject()的强大累积计算功能,我写了如下测试:
class BookSeriesTest extends Specification {
def "验证丛书总数计算正确"() {
given: "图书馆丛书列表"
def library = new Library(
bookSeriesList: [
new BookSeries(suitNum: 1, books: [
new Book(name: "水浒传(上)", store: 1, price: 100.00),
new Book(name: "水浒传(中)", store: 2, price: 120.00),
new Book(name: "水浒传(下)", store: 3, price: 150.00),
]),
new BookSeries(suitNum: 2, books: [
new Book(name: "三国演义(上)", store: 4, price: 100.00),
new Book(name: "三国演义(中)", store: 5, price: 120.00),
new Book(name: "三国演义(下)", store: 6, price: 150.00),
]),
new BookSeries(suitNum: 3, books: [
new Book(name: "西游记(上)", store: 7, price: 100.00),
new Book(name: "西游记(中)", store: 8, price: 120.00),
new Book(name: "西游记(下)", store: 9, price: 150.00),
]),
new BookSeries(suitNum: 4, books: [
new Book(name: "红楼梦(上)", store: 10, price: 100.00),
new Book(name: "红楼梦(中)", store: 11, price: 120.00),
new Book(name: "红楼梦(下)", store: 12, price: 150.00),
]),
new BookSeries(suitNum: 0, books: [
new Book(name: "大秦帝国(上)", store: 10, price: 100.00),
new Book(name: "大秦帝国(中)", store: 10, price: 120.00),
new Book(name: "大秦帝国(下)", store: 10, price: 150.00),
]),
]
)
when:"统计图书馆丛书总库存"
def totalStore = library.bookSeriesList.inject(0) { libraryTotalStore, bookSeries ->
libraryTotalStore + (bookSeries.suitNum > 0 ? bookSeries.books.store.sum() * bookSeries.suitNum : 0)
}
then:"验证库存统计正确"
totalStore == (1 + 2 + 3) * 1 + (4 + 5 + 6) * 2 + (7 + 8 + 9) * 3 + (10 + 11 + 12) * 4
println totalStore
}
欢迎交流,请加公众号: