【问题标题】:Java: Commons-Collections generics: How to get custom transformer to workJava:Commons-Collections 泛型:如何让自定义转换器工作
【发布时间】:2012-05-17 23:32:59
【问题描述】:

您好,我正在使用 commons collections generics 4.01。

我有一个 dto 对象。

Class PricingDto {
   private Double tax;
   private Double price;
   private Double tip;

   // getters and setters
}

我有List<PricingDto> pricingDtos = this.pricingService.getAllPricings();的列表

比我有一个私有静态类。

import org.apache.commons.collections15.Transformer;
import org.apache.commons.collections15.list.TransformedList;

class TotalServiceImpl implements TotalService {
    public static final PricingDtoTransformer PRICING_DTO_TRANSFORMER =
        new PricingDtoTransformer();
    private static class PricingDtoTransformer
        implements Transformer<PricingDto, Double> {
        public PricingDtoTransformer() {}

        @Override
        public Double transform(final PricingDto pricingDto) {
            return pricingDto.getTax()
                     + pricingDto.getPrice()
                     + pricingDto.getTips();
        }
    }

    @Override
    public List<Double> getListDouble(final List<PricingDto> pricingDtos) {
        final List<Double> totalList = 
            TransformedList.decorate(pricingDtos, PRICING_DTO_TRANSFORMER);
            for (Double d : totalList) {
                // print them. 
            }
        }
    }
}

我的问题是我得到类转换异常,因为 totalList 中的每个项目都是 PricingDto 而不是 Double。

2.) 我做错了什么。为泛型公共集合实现自定义转换器的正确方法是什么。

【问题讨论】:

  • getListDouble 实际上必须做什么?它返回一个List&lt;Double&gt;,但我不明白listDouble 的真正含义......它必须做什么?

标签: java generics transformer apache-commons-collection


【解决方案1】:

javadocs 明确指出:

如果已经有任何元素 正在装饰的列表,它们不是 变身了。

请尝试以下方法:

CollectionUtils.transform(pricingDtos, PRICING_DTO_TRANSFORMER);

它将通过将 Transformer 应用于每个元素来转换集合。

【讨论】:

  • CollectionUtils.transform 不起作用,因为我正在将 PricingDto 类型的对象转换为 Double。做完改造后。当我为 (Double totalPrice : resultTotals) { .... } 做时,我得到类转换异常,说里面的对象是 PricingDto 而不是 Double。
【解决方案2】:

对我来说,就地转换集合似乎是一个可怕的黑客行为。我建议改用Google GuavaLists.transform(List,Function) 返回一个由原始 List 和映射函数支持的视图,因此您实际上并没有更改任何内容。

您的代码如下所示:

class TotalServiceImpl implements TotalService{

    private static final Function<PricingDto, Double> PRICING_DTO_TRANSFORMER =
        new PricingDtoTransformer();

    private static class PricingDtoTransformer implements
        Function<PricingDto, Double>{

        public PricingDtoTransformer(){
        }

        @Override
        public Double apply(final PricingDto pricingDto){
            return pricingDto.getTax() + pricingDto.getPrice()
                + pricingDto.getTips();
        }
    }

    public List<Double> getListDouble(final List<PricingDto> pricingDtos){
        final List<Double> totalList =
            Lists.transform(pricingDtos, PRICING_DTO_TRANSFORMER);
        for(final Double d : totalList){
            // print them.
        }
        return totalList;
    }

}

Commons-Collections 可能也有类似的机制,但我第一眼找不到。

【讨论】:

  • 我找到了答案。我必须使用 CollectionUtils.collect(pricingDtos, PricingDtoTransformer) 才能使事情正常进行。
  • 啊,是的。听起来好多了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多