com.none.tom.simplerssreader.feed.SavedEntries.java Source code

Java tutorial

Introduction

Here is the source code for com.none.tom.simplerssreader.feed.SavedEntries.java

Source

// Copyright (c) 2017-2018, Tom Geiselmann
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY,WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.none.tom.simplerssreader.feed;

import com.none.tom.simplerssreader.db.DbEntry;
import com.none.tom.simplerssreader.utils.SearchResults;
import com.none.tom.simplerssreader.utils.SearchUtils;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;

public class SavedEntries {
    private static List<DbEntry> sDbEntries;

    public static String asString() {
        final StringBuilder savedEntries = new StringBuilder();

        for (final DbEntry entry : sDbEntries) {
            savedEntries.append(String.format("%s\n%s\n\n", entry.getEntryTitle(), entry.getEntryUrl()));
        }

        return savedEntries.toString();
    }

    public static void addSavedEntries(final List<DbEntry> savedEntries) {
        if (sDbEntries == null) {
            sDbEntries = new ArrayList<>();
        } else if (!sDbEntries.isEmpty()) {
            sDbEntries.clear();
        }

        sDbEntries.addAll(savedEntries);
    }

    public static String getSavedEntryUrlAt(final int index) {
        return sDbEntries.get(index).getEntryUrl();
    }

    public static String getSavedEntryTitleAt(final int index) {
        return sDbEntries.get(index).getEntryTitle();
    }

    public static DbEntry getSavedEntryAt(final int index) {
        return sDbEntries.get(index);
    }

    public static void removeSavedEntryAt(final int index) {
        sDbEntries.remove(index);
    }

    public static SearchResults getSearchResultsFor(final String query, final int position) {
        final List<Integer> positions = new ArrayList<>();
        final List<LinkedHashMap<Integer, Integer>> indices = new ArrayList<>();

        int i = 0;

        for (final DbEntry entry : sDbEntries) {
            final String title = entry.getEntryTitle();

            if (StringUtils.containsIgnoreCase(title, query)) {
                indices.add(SearchUtils.getIndicesForQuery(title, query));
                positions.add(i);
            }
            i++;
        }

        return new SearchResults(position, positions, indices);
    }

    public static void reverse() {
        Collections.reverse(sDbEntries);
    }

    public static int size() {
        return sDbEntries.size();
    }
}