首页 > 解决方案 > java 通用问题。为什么会这样?

问题描述

public interface IPage<T> extends Serializable {
    /** @deprecated */
    @Deprecated
    default String[] descs() {
        return null;
    }

    /** @deprecated */
    @Deprecated
    default String[] ascs() {
        return null;
    }

    List<OrderItem> orders();

    default Map<Object, Object> condition() {
        return null;
    }

    default boolean optimizeCountSql() {
        return true;
    }

    default boolean isSearchCount() {
        return true;
    }

    default long offset() {
        return this.getCurrent() > 0L ? (this.getCurrent() - 1L) * this.getSize() : 0L;
    }

    default long getPages() {
        if (this.getSize() == 0L) {
            return 0L;
        } else {
            long pages = this.getTotal() / this.getSize();
            if (this.getTotal() % this.getSize() != 0L) {
                ++pages;
            }

            return pages;
        }
    }

    default IPage<T> setPages(long pages) {
        return this;
    }

    default void hitCount(boolean hit) {
    }

    default boolean isHitCount() {
        return false;
    }

    List<T> getRecords();

    IPage<T> setRecords(List<T> records);

    long getTotal();

    IPage<T> setTotal(long total);

    long getSize();

    IPage<T> setSize(long size);

    long getCurrent();

    IPage<T> setCurrent(long current);

    default <R> IPage<R> convert(Function<? super T, ? extends R> mapper) {
        List<R> collect = (List)this.getRecords().stream().map(mapper).collect(Collectors.toList());
        return this.setRecords(collect);
    }

    default String cacheKey() {
        StringBuilder key = new StringBuilder();
        key.append(this.offset()).append(":").append(this.getSize());
        List<OrderItem> orders = this.orders();
        if (CollectionUtils.isNotEmpty(orders)) {
            Iterator var3 = orders.iterator();

            while(var3.hasNext()) {
                OrderItem item = (OrderItem)var3.next();
                key.append(":").append(item.getColumn()).append(":").append(item.isAsc());
            }
        }

        return key.toString();
    }
}

这是一个框架的源代码,当我使用convert()函数时

default <R> IPage<R> convert(Function<? super T, ? extends R> mapper) {
        List<R> collect = (List)this.getRecords().stream().map(mapper).collect(Collectors.toList());
        return this.setRecords(collect);
    }

让我想知道的是,返回类型是新类型变量R ,他只是调用 this.setRecords(collect); 但是setRecords() 函数只接收 List < T >!

IPage<T> setRecords(List<T> records);

为了验证这一点,我自己写了一个接口,但是编译失败

public interface IPage<T> {
    
    IPage<T> setRecords(List<T> list);

    default <R> IPage<R> convert() {
        List<R> collect = new ArrayList<>();
        return this.setRecords(collect);  //error
    }
    
}

有人可以帮我解决我的疑惑吗?非常感谢!

标签: javaspringgenericsmybatis-plus

解决方案


my-batis plus 的源代码包含对 IPage 的附加转换,以更改this.

方法代码为:

default <R> IPage<R> convert(Function<? super T, ? extends R> mapper) {
    List<R> collect = this.getRecords().stream().map(mapper).collect(toList());
    return ((IPage<R>) this).setRecords(collect);
}

推荐阅读