用于为列表分配值的最短代码

时间:2018-11-13 16:00:57

标签: java

这可能是最短的代码:

public void update(final Product object, final Callback<Product> callback) {
    if(object.getIsDayDependent()) {
        Double priceDay0 = object.getSundayPrice();
        Double priceDay1 = object.getMondayPrice();
        Double priceDay2 = object.getTuesdayPrice();
        Double priceDay3 = object.getWednesdayPrice();
        Double priceDay4 = object.getThursdayPrice();
        Double priceDay5 = object.getFridayPrice();
        Double priceDay6 = object.getSaturdayPrice();
        List<DayPrice> dayPrices = new LinkedList<>();
        dayPrices.add(new DayPrice(0, priceDay0));
        dayPrices.add(new DayPrice(1, priceDay1));
        dayPrices.add(new DayPrice(2, priceDay2));
        dayPrices.add(new DayPrice(3, priceDay3));
        dayPrices.add(new DayPrice(4, priceDay4));
        dayPrices.add(new DayPrice(5, priceDay5));
        dayPrices.add(new DayPrice(6, priceDay6));
        object.setDayDependent(dayPrices);
    } else {
        object.setPrice(null);
        object.setDayDependent(new LinkedList<>());
    }
    callback.onSuccess(object);
}

2 个答案:

答案 0 :(得分:1)

以下内容并没有减少代码的数量,而是减少了数量,但也消除了不必要的变量:

sudo grep nginx /var/log/audit/audit.log | audit2allow -M nginx
Nothing to do

答案 1 :(得分:0)

在不修改关联类(产品类)的情况下,为此,我拥有的最短代码是:

        if(object.getIsDayDependent())) {
            Double priceDay0 = object.getSundayPrice();
            Double priceDay1 = object.getMondayPrice();
            Double priceDay2 = object.getTuesdayPrice();
            Double priceDay3 = object.getWednesdayPrice();
            Double priceDay4 = object.getThursdayPrice();
            Double priceDay5 = object.getFridayPrice();
            Double priceDay6 = object.getSaturdayPrice();
            object.setDayDependent(new LinkedList<>(Arrays.asList(
                    new DayPrice(0, priceDay0),
                    new DayPrice(1, priceDay1),
                    new DayPrice(2, priceDay2),
                    new DayPrice(3, priceDay3),
                    new DayPrice(4, priceDay4),
                    new DayPrice(5, priceDay5),
                    new DayPrice(6, priceDay6))));
        }

不多,但是也许有人可以回答一些更聪明的方法。