如何解决对原始类型为“ android.arch.paging.PositionalDataSource”的成员的对“ mapByPage(Function <list <t>,List <v >>)”的未经检查的调用

时间:2018-08-07 13:47:15

标签: java android generics

我正在调查在当前的Android应用程序中使用android.arch.paging.PagedList的情况。

我的数据源是我的数据库和UI模型对象之间的映射。

所有操作均按预期方式进行,但是我的DataSource.Factory create()方法具有以下警告:-

Unchecked call to 'mapByPage(Function<List<T>, List<V>>)' as a member of raw type 'android.arch.paging.PositionalDataSource'
Unchecked assignment: 'android.arch.paging.PositionalDataSource' to 'android.arch.paging.DataSource<java.lang.Long, example.ui.ModelUI'>'.
Reason 'new ModelUIDataSource' has raw type, so result of mapByPage is erased

有问题的课程是:-

public abstract class BaseDataSource<T> extends PositionalDataSource<ModelDO> {
    private static final String TAG = "BaseDataSource";

    @WorkerThread
    public abstract int countItems();

    @WorkerThread
    public abstract List<ModelDO> loadRange(final int startPosition, final int count);

    @Override
    public final void loadInitial(@NonNull final LoadInitialParams params, @NonNull final LoadInitialCallback<ModelDO> callback) {
        final int totalCount = countItems();

        if (totalCount == 0) {
            callback.onResult(Collections.emptyList(), 0, 0);
            return;
        }

        final int firstLoadPosition = computeInitialLoadPosition(params, totalCount);
        final int firstLoadSize = computeInitialLoadSize(params, firstLoadPosition, totalCount);

        final List<ModelDO> list = loadRange(firstLoadPosition, firstLoadSize);

        if (list != null && list.size() == firstLoadSize) {
            callback.onResult(list, firstLoadPosition, totalCount);
        } else {
            invalidate();
        }
    }

    @Override
    public final void loadRange(@NonNull final LoadRangeParams params, @NonNull final LoadRangeCallback<ModelDO> callback) {
        final List<ModelDO> list = loadRange(params.startPosition, params.loadSize);

        if (list == null) {
            invalidate();
        } else {
            callback.onResult(list);
        }
    }
}


public class ModelDataSource<T> extends BaseDataSource<ModelDO> {

    private static final String TAG = "ModelDataSource";

    private final Realm workerRealm;
    private final RealmResults<ModelDO> liveResults;

    private final RealmChangeListener<RealmResults<ModelDO>> realmChangeListener = results -> {
        if (results.isLoaded()) {
            invalidate();
        }
    };

    // WORKER THREAD
    ModelDataSource() {
        this.workerRealm = DatabaseController.getRealm();
        this.liveResults = DatabaseController.fetchShortListedResults(this.workerRealm);

        if (!liveResults.isLoaded()) {
            liveResults.load();
        }

        this.liveResults.addChangeListener(realmChangeListener);
    }

    @WorkerThread
    public int countItems() {
        if (workerRealm.isClosed() || !liveResults.isValid()) {
            Log.i(TAG, "RESULTS ARE NOT VALID, OR REALM IS CLOSED.");
            return 0;
        }
        return liveResults.size();
    }

    @Override
    public boolean isInvalid() {
        workerRealm.refresh();
        return super.isInvalid();
    }

    @WorkerThread
    @Override
    public List<ModelDO> loadRange(final int startPosition, final int count) {
        final int countItems = countItems();

        if (countItems == 0) {
            return Collections.emptyList();
        }

        final List<ModelDO> list = new ArrayList<>(count);

        for (int i = startPosition; i < startPosition + count && i < countItems; i++) {
            list.add(workerRealm.copyFromRealm(liveResults.get(i)));
        }

        return Collections.unmodifiableList(list);
    }
}


public class ModelDataSourceFactory<T> extends DataSource.Factory<Long, ModelUI> {
    private static final String TAG = "ModelDataSource";

    @Override
    public DataSource<Long, ModelUI> create() {
        return new ModelDataSource().mapByPage(new Function<List<ModelDO>, List<ModelUI>>() {
            @Override
            public List<ModelUI> apply(final List<ModelDO> input) {
                final List<ModelUI> output = new ArrayList<>();

                for (final ModelDO modelDO : input) {
                    final ModelUI modelUI = constructModelUI(modelDO);
                    output.add(modelUI);
                }
                return output;
            }
        });
    }
}

要删除这些警告,需要进行哪些重构?

我不是在问“什么是原始类型”。

我看不到上面的类中我在哪里使用“原始类型”,也看不到为什么看到这些警告。

0 个答案:

没有答案