package cgeo.geocaching.filter;

import cgeo.geocaching.R;
import cgeo.geocaching.Geocache;
import cgeo.geocaching.cgData;
import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.enumerations.LoadFlags.LoadFlag;

import org.apache.commons.lang3.StringUtils;

import android.content.res.Resources;

import java.util.EnumSet;

class AttributeFilter extends AbstractFilter {

    private final String attribute;

    public AttributeFilter(final String name, final String attribute) {
        super(name);
        this.attribute = attribute;
    }

    private static String getName(final String attribute, final Resources res, final String packageName) {
        // dynamically search for a translation of the attribute
        final int id = res.getIdentifier(attribute, "string", packageName);
        if (id > 0) {
            final String translated = res.getString(id);
            if (StringUtils.isNotBlank(translated)) {
                return translated;
            }
        }
        return attribute;
    }

    @Override
    public boolean accepts(final Geocache cache) {
        Geocache fullCache = cgData.loadCache(cache.getGeocode(), EnumSet.of(LoadFlag.LOAD_ATTRIBUTES));
        if (fullCache == null) {
            fullCache = cache;
        }
        return fullCache.getAttributes().contains(attribute);
    }

    public static class Factory implements IFilterFactory {

        @Override
        public IFilter[] getFilters() {
            final String packageName = cgeoapplication.getInstance().getBaseContext().getPackageName();
            final Resources res = cgeoapplication.getInstance().getResources();

            final String[] ids = res.getStringArray(R.array.attribute_ids);
            final IFilter[] filters = new IFilter[ids.length];
            for (int i = 0; i < ids.length; i++) {
                filters[i] = new AttributeFilter(getName("attribute_" + ids[i], res, packageName), ids[i]);
            }
            return filters;
        }

    }
}

@@ -49,17 +49,24 @@
 caching.
-R
+Geocache
 ;%0Aimport
@@ -78,32 +78,25 @@
 .geocaching.
-Geocache
+R
 ;%0Aimport cge
@@ -217,54 +217,8 @@
 g;%0A%0A
-import org.apache.commons.lang3.StringUtils;%0A%0A
 impo
@@ -760,206 +760,43 @@
     
-if (id %3E 0) %7B%0A            final String translated = res.getString(id);%0A            if (StringUtils.isNotBlank(translated)) %7B%0A                return translated;%0A            %7D%0A        %7D%0A        return
+return id %3E 0 ? res.getString(id) :
  att

simplify attribute resolution code