<commit_before>package org.commcare.adapters;

import android.content.Context;
import android.widget.Filter;

import java.util.ArrayList;

/**
 * An implementation of ComboboxAdapter that permits a wider range of strings to be entered
 * into the combobox's edittext field. SEE implementation of  isValidUserEntry() for precise
 * definition of accepted strings.
 *
 * @author <NAME>
 */
public class PermissiveComboboxAdapter extends ComboboxAdapter {

    private final String[] allChoices;
    private String[] currentChoices;

    public PermissiveComboboxAdapter(final Context context, final int textViewResourceId,
                                     final String[] objects, float textSize) {
        super(context, textViewResourceId, objects, textSize);
        allChoices = currentChoices = objects;
    }

    @Override
    public int getCount() {
        return currentChoices.length;
    }

    @Override
    public String getItem(int position) {
        return currentChoices[position];
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                ArrayList<String> matched = new ArrayList<>();
                for (String choice : allChoices) {
                    if (choiceShouldBeShown(choice.toLowerCase(), constraint)) {
                        matched.add(choice);
                    }
                }

                FilterResults results = new FilterResults();
                results.values = matched.toArray(new String[]{});
                results.count = matched.size();
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                currentChoices = (String[])results.values;
                if (results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        };
    }

    @Override
    public boolean isValidUserEntry(String enteredText) {
        for (String choice : allChoices) {
            if (choiceShouldBeShown(choice.toLowerCase(), enteredText)) {
                return true;
            }
        }
        return false;
    }

    private boolean choiceShouldBeShown(String choiceLowerCase, CharSequence textEntered) {
        if ("".equals(textEntered) || textEntered == null) {
            return true;
        }
        String[] enteredTextIndividualWords = textEntered.toString().split(" ");
        for (String word : enteredTextIndividualWords) {
            if (!choiceLowerCase.contains(word.toLowerCase())) {
                return false;
            }
        }
        return true;
    }

}<commit_msg>add method comment<commit_after>package org.commcare.adapters;

import android.content.Context;
import android.widget.Filter;

import java.util.ArrayList;

/**
 * An implementation of ComboboxAdapter that permits a wider range of strings to be entered
 * into the combobox's edittext field. SEE implementation of  isValidUserEntry() for precise
 * definition of accepted strings.
 *
 * @author <NAME>
 */
public class PermissiveComboboxAdapter extends ComboboxAdapter {

    private final String[] allChoices;
    private String[] currentChoices;

    public PermissiveComboboxAdapter(final Context context, final int textViewResourceId,
                                     final String[] objects, float textSize) {
        super(context, textViewResourceId, objects, textSize);
        allChoices = currentChoices = objects;
    }

    @Override
    public int getCount() {
        return currentChoices.length;
    }

    @Override
    public String getItem(int position) {
        return currentChoices[position];
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                ArrayList<String> matched = new ArrayList<>();
                for (String choice : allChoices) {
                    if (choiceShouldBeShown(choice.toLowerCase(), constraint)) {
                        matched.add(choice);
                    }
                }

                FilterResults results = new FilterResults();
                results.values = matched.toArray(new String[]{});
                results.count = matched.size();
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                currentChoices = (String[])results.values;
                if (results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        };
    }

    @Override
    public boolean isValidUserEntry(String enteredText) {
        for (String choice : allChoices) {
            if (choiceShouldBeShown(choice.toLowerCase(), enteredText)) {
                return true;
            }
        }
        return false;
    }

    /**
     *
     * @param choiceLowerCase - the answer choice to be considered, in all lower case
     * @param textEntered - the text entered by the user
     * @return true if choiceLowerCase contains any word within textEntered (the "words" of
     * textEntered are obtained by splitting textEntered on " ")
     */
    private boolean choiceShouldBeShown(String choiceLowerCase, CharSequence textEntered) {
        if ("".equals(textEntered) || textEntered == null) {
            return true;
        }
        String[] enteredTextIndividualWords = textEntered.toString().split(" ");
        for (String word : enteredTextIndividualWords) {
            if (!choiceLowerCase.contains(word.toLowerCase())) {
                return false;
            }
        }
        return true;
    }

}
{'fraction_non_alphanumeric': 0.08224674022066199, 'fraction_numerical': 0.00033433634236041456, 'mean_word_length': 1.9959939909864797, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5710625', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1392, 'n_words': 389}