org.rm3l.ddwrt.prefs.sort.SortingStrategy.java Source code

Java tutorial

Introduction

Here is the source code for org.rm3l.ddwrt.prefs.sort.SortingStrategy.java

Source

/*
 * DD-WRT Companion is a mobile app that lets you connect to,
 * monitor and manage your DD-WRT routers on the go.
 *
 * Copyright (C) 2014  Armel Soro
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contact Info: Armel Soro <apps+ddwrt@rm3l.org>
 */

package org.rm3l.ddwrt.prefs.sort;

import com.google.common.collect.Maps;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.rm3l.ddwrt.fragments.DDWRTBaseFragment;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * Abstract Sorting strategy: Indicates the strategy to use for sorting tabs in the UI
 */
public abstract class SortingStrategy {

    public static final String DEFAULT = DDWRTSortingStrategy.class.getSimpleName();

    @NotNull
    public abstract String getDisplayName();

    @NotNull
    public abstract String getShortDescription();

    @NotNull
    public final DDWRTBaseFragment[] sort(@NotNull final DDWRTBaseFragment[] tabs) {
        if (doCompare()) {

            final TreeMap<String, DDWRTBaseFragment> tabsMap = Maps.newTreeMap(this.getComparator());
            for (int i = 0; i < tabs.length; i++) {
                final DDWRTBaseFragment tab = tabs[i];
                tabsMap.put(tab.getTabTitle().toString(), tab);
            }

            @NotNull
            final DDWRTBaseFragment[] output = new DDWRTBaseFragment[tabsMap.size()];
            int j = 0;
            for (@NotNull
            Map.Entry<String, DDWRTBaseFragment> DDWRTBaseFragmentEntry : tabsMap.entrySet()) {
                output[j++] = DDWRTBaseFragmentEntry.getValue();
            }
            return output;
        }

        return tabs;

    }

    @Nullable
    protected abstract Comparator<String> getComparator();

    protected abstract boolean doCompare();
}