All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.solovyev.android.prefs.LocalePreference Maven / Gradle / Ivy

There is a newer version: 1.1.18
Show newest version
package org.solovyev.android.prefs;

import android.content.SharedPreferences;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.text.Mapper;

import java.util.Locale;
import java.util.StringTokenizer;

/**
 * User: serso
 * Date: 4/15/12
 * Time: 7:09 PM
 */
public class LocalePreference implements Preference {

    @NotNull
    private final StringPreference stringPreference;

    public LocalePreference(@NotNull String id, @Nullable Locale defaultValue, @NotNull Mapper localeMapper) {
        this.stringPreference = new StringPreference(id, defaultValue, localeMapper);
    }

    public LocalePreference(@NotNull String id, @Nullable Locale defaultValue) {
        this.stringPreference = new StringPreference(id, defaultValue, DefaultLocaleMapper.getInstance());
    }

    @Override
    @NotNull
    public String getKey() {
        return stringPreference.getKey();
    }

    @Override
    public Locale getDefaultValue() {
        return stringPreference.getDefaultValue();
    }

    @Override
    public Locale getPreference(@NotNull SharedPreferences preferences) {
        return stringPreference.getPreference(preferences);
    }

    @Override
    public Locale getPreferenceNoError(@NotNull SharedPreferences preferences) {
        return stringPreference.getPreferenceNoError(preferences);
    }

    @Override
    public void putDefault(@NotNull SharedPreferences preferences) {
        stringPreference.putDefault(preferences);
    }

    @Override
    public void putPreference(@NotNull SharedPreferences preferences, @Nullable Locale value) {
        stringPreference.putPreference(preferences, value);
    }

    @Override
    public boolean isSet(@NotNull SharedPreferences preferences) {
        return stringPreference.isSet(preferences);
    }

    private static final class DefaultLocaleMapper implements Mapper {

        @NotNull
        private static final String delimiter = ";";

        @NotNull
        private static Mapper instance = new DefaultLocaleMapper();

        private DefaultLocaleMapper() {
        }

        @NotNull
        public static Mapper getInstance() {
            return instance;
        }

        @Override
        public String formatValue(@Nullable Locale locale) throws IllegalArgumentException {
            assert locale != null;
            return locale.getLanguage() + delimiter + locale.getCountry() + delimiter + locale.getVariant();
        }

        @Override
        public Locale parseValue(@Nullable String s) throws IllegalArgumentException {
            final StringTokenizer st = new StringTokenizer(s, delimiter, false);

            final String language = st.nextToken();

            final String country;
            if (st.hasMoreTokens()) {
                country = st.nextToken();
            } else {
                country = "";
            }

            final String variant;
            if (st.hasMoreTokens()) {
                variant = st.nextToken();
            } else {
                variant = "";
            }

            return new Locale(language, country, variant);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy