<commit_before>/*
 *  Copyright (c) 2015 RoboSwag (<NAME>, <NAME>)
 *
 *  This file is part of RoboSwag library.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package org.roboswag.components.listing;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.SparseArray;

import org.roboswag.core.log.Lc;
import org.roboswag.core.utils.ShouldNotHappenException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import rx.Observable;
import rx.functions.Function;
import rx.schedulers.Schedulers;

/**
 * Created by <NAME> on 07/12/2015.
 * TODO: fill description
 */
public class PagingListProvider<T> implements ItemsProvider<T> {

    private static final int PAGE_SIZE = 25;

    private final SparseArray<List<T>> loadedPages = new SparseArray<>();
    private boolean isInitialized;
    @Nullable
    private Integer maxLoadedPage;
    @NonNull
    private final PageRequestCreator<T> pageRequestCreator;
    private boolean isLastPageLoaded;
    @Nullable
    private Integer totalCount;

    private final Object lock = new Object();

    public PagingListProvider(@NonNull final PageRequestCreator<T> pageRequestCreator) {
        this.pageRequestCreator = pageRequestCreator;
    }

    @Nullable
    @Override
    public T getItem(final int position) {
        synchronized (lock) {
            final List<T> page = loadedPages.get(position / PAGE_SIZE);
            return page != null ? page.get(position % PAGE_SIZE) : null;
        }
    }

    @Override
    public int getSize() {
        synchronized (lock) {
            return maxLoadedPage != null
                    ? maxLoadedPage * PAGE_SIZE + loadedPages.get(maxLoadedPage).size() + (isLastPageLoaded ? 0 : 1)
                    : 0;
        }
    }

    public Observable<Integer> initialize() {
        synchronized (lock) {
            isInitialized = false;
        }
        return initialize(0);
    }

    @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
    public Observable<Integer> initialize(final int loadToPosition) {
        synchronized (lock) {
            if (isInitialized) {<commit_msg>static ignore for now - will be reworked later<commit_after>/*
 *  Copyright (c) 2015 RoboSwag (<NAME>, <NAME>)
 *
 *  This file is part of RoboSwag library.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package org.roboswag.components.listing;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.SparseArray;

import org.roboswag.core.log.Lc;
import org.roboswag.core.utils.ShouldNotHappenException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import rx.Observable;
import rx.functions.Function;
import rx.schedulers.Schedulers;

/**
 * Created by <NAME> on 07/12/2015.
 * TODO: fill description
 */
public class PagingListProvider<T> implements ItemsProvider<T> {

    private static final int PAGE_SIZE = 25;

    private final SparseArray<List<T>> loadedPages = new SparseArray<>();
    private boolean isInitialized;
    @Nullable
    private Integer maxLoadedPage;
    @NonNull
    private final PageRequestCreator<T> pageRequestCreator;
    private boolean isLastPageLoaded;
    @Nullable
    private Integer totalCount;

    private final Object lock = new Object();

    public PagingListProvider(@NonNull final PageRequestCreator<T> pageRequestCreator) {
        this.pageRequestCreator = pageRequestCreator;
    }

    @Nullable
    @Override
    public T getItem(final int position) {
        synchronized (lock) {
            final List<T> page = loadedPages.get(position / PAGE_SIZE);
            return page != null ? page.get(position % PAGE_SIZE) : null;
        }
    }

    @Override
    public int getSize() {
        synchronized (lock) {
            return maxLoadedPage != null
                    ? maxLoadedPage * PAGE_SIZE + loadedPages.get(maxLoadedPage).size() + (isLastPageLoaded ? 0 : 1)
                    : 0;
        }
    }

    public Observable<Integer> initialize() {
        synchronized (lock) {
            isInitialized = false;
        }
        return initialize(0);
    }

    @SuppressWarnings("checkstyle:MethodLengthCheck")
    public Observable<Integer> initialize(final int loadToPosition) {
        synchronized (lock) {
            if (isInitialized) {
{'fraction_non_alphanumeric': 0.08858447488584476, 'fraction_numerical': 0.008036529680365296, 'mean_word_length': 3.1266013564431048, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '419337', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1527, 'n_words': 467}