package com.fsck.k9.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import timber.log.Timber;
import android.widget.Filter;

/**
 * Filter to search for occurrences of the search-expression in any place of the folder name.
 */
public class FolderListFilter<T> extends Filter {
    private FilterableAdapter<T> mFolders;
    private List<T> mOriginalValues = null;

    public FolderListFilter(final FilterableAdapter<T> folders) {
        this.mFolders = folders;
    }

    @Override
    protected FilterResults performFiltering(CharSequence searchTerm) {
        FilterResults results = new FilterResults();

        if (mOriginalValues == null) {
            int count = mFolders.getCount();
            mOriginalValues = new ArrayList<>(count);
            for (int i = 0; i < count; i++) {
                mOriginalValues.add(mFolders.getItem(i));
            }
        }

        Locale locale = Locale.getDefault();
        if ((searchTerm == null) || (searchTerm.length() == 0)) {
            List<T> list = new ArrayList<>(mOriginalValues);
            results.values = list;
            results.count = list.size();
        } else {
            final String searchTermString = searchTerm.toString().toLowerCase(locale);
            final String[] words = searchTermString.split(" ");
            final int wordCount = words.length;

            final List<T> values = mOriginalValues;

            final List<T> newValues = new ArrayList<>();

            for (final T value : values) {
                final String valueText = value.toString().toLowerCase(locale);

                for (int k = 0; k < wordCount; k++) {
                    if (valueText.contains(words[k])) {
                        newValues.add(value);
                        break;
                    }
                }
            }

            results.values = newValues;
            results.count = newValues.size();
        }

        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        final List<T> folders = (List<T>) results.values;
        mFolders.clear();
        if (folders != null) {
            for (T folder : folders) {
                if (folder != null) {
                    mFolders.add(folder);
                }
            }
        } else {
            Timber.w("FolderListFilter.publishResults - null search-result ");
        }

        mFolders.notifyDataSetChanged();
    }

    public interface FilterableAdapter<T> {
        void notifyDataSetChanged();
        void clear();
        void add(T object);
        int getCount();
        T getItem(int i);
    }
}

@@ -343,24 +343,23 @@
 pter%3CT%3E 
-mFold
+adapt
 er
-s
 ;%0A    pr
@@ -372,31 +372,36 @@
 List%3CT%3E 
-mOriginalValues
+unfilteredFolderList
  = null;
@@ -481,24 +481,23 @@
    this.
-mFold
+adapt
 er
-s
  = folde
@@ -659,31 +659,36 @@
     if (
-mOriginalValues
+unfilteredFolderList
  == null
@@ -715,24 +715,23 @@
 count = 
-mFold
+adapt
 er
-s
 .getCoun
@@ -747,31 +747,36 @@
         
-mOriginalValues
+unfilteredFolderList
  = new A
@@ -860,36 +860,40 @@
     
-mOriginalValues.add(mFold
+unfilteredFolderList.add(adapt
 er
-s
 .get
@@ -1081,31 +1081,36 @@
 yList%3C%3E(
-mOriginalValues
+unfilteredFolderList
 );%0A     
@@ -1436,23 +1436,28 @@
 s = 
-mOriginalValues
+unfilteredFolderList
 ;%0A%0A 
@@ -2205,24 +2205,23 @@
         
-mFold
+adapt
 er
-s
 .clear()
@@ -2350,24 +2350,23 @@
         
-mFold
+adapt
 er
-s
 .add(fol
@@ -2522,16 +2522,15 @@
     
-mFold
+adapt
 er
-s
 .not

Rename fields in FolderListFilter