org.solovyev.android.prefs
Class AbstractPreference<T>

java.lang.Object
  extended by org.solovyev.android.prefs.AbstractPreference<T>
Type Parameters:
T - type of preference
All Implemented Interfaces:
Preference<T>
Direct Known Subclasses:
BooleanPreference, CollectionSetPreference, CollectionToStringPreference, FloatPreference, IntegerPreference, LongPreference, NumberIntervalPreference, NumberToStringPreference, StringPreference

public abstract class AbstractPreference<T>
extends Object
implements Preference<T>

Base class for Preference implementation. Contains preference key and default value


Constructor Summary
protected AbstractPreference(String key, T defaultValue)
           
 
Method Summary
 T getDefaultValue()
           
 String getKey()
          Method returns key of preference used in android: the key with which current preference is saved in persistence
protected abstract  T getPersistedValue(android.content.SharedPreferences preferences)
           
 T getPreference(android.content.SharedPreferences preferences)
          NOTE: this method can throw runtime exceptions if errors occurred while extracting preferences values
 T getPreferenceNoError(android.content.SharedPreferences preferences)
          NOTE: this method SHOULD not throw any runtime exceptions BUT return default value if any error occurred
 boolean isSameKey(String key)
           
 boolean isSet(android.content.SharedPreferences preferences)
           
 void putDefault(android.content.SharedPreferences preferences)
          Method saves default value in preferences container.
protected abstract  void putPersistedValue(android.content.SharedPreferences.Editor editor, T value)
          Method saved preference to preferences container editor
 void putPreference(android.content.SharedPreferences preferences, T value)
          Method puts (saves) preference represented by value in preferences container
 boolean tryPutDefault(android.content.SharedPreferences preferences)
          Method applies default value to preference only if explicit value is not set
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractPreference

protected AbstractPreference(@Nonnull
                             String key,
                             @Nullable
                             T defaultValue)
Method Detail

getKey

@Nonnull
public String getKey()
Description copied from interface: Preference
Method returns key of preference used in android: the key with which current preference is saved in persistence

Specified by:
getKey in interface Preference<T>
Returns:
android preference key

getDefaultValue

public T getDefaultValue()
Specified by:
getDefaultValue in interface Preference<T>
Returns:
default preference value, may be null

getPreference

public final T getPreference(@Nonnull
                             android.content.SharedPreferences preferences)
Description copied from interface: Preference
NOTE: this method can throw runtime exceptions if errors occurred while extracting preferences values

Specified by:
getPreference in interface Preference<T>
Parameters:
preferences - application preferences
Returns:
value from preference, default value if no value in preference was found

getPreferenceNoError

public T getPreferenceNoError(@Nonnull
                              android.content.SharedPreferences preferences)
Description copied from interface: Preference
NOTE: this method SHOULD not throw any runtime exceptions BUT return default value if any error occurred

Specified by:
getPreferenceNoError in interface Preference<T>
Parameters:
preferences - application preferences
Returns:
value from preference, default value if no value in preference was found or error occurred

putDefault

public void putDefault(@Nonnull
                       android.content.SharedPreferences preferences)
Description copied from interface: Preference
Method saves default value in preferences container. Should behave exactly as p.putPreference(preferences, p.getDefaultValue())

Specified by:
putDefault in interface Preference<T>
Parameters:
preferences - preferences container

putPreference

public void putPreference(@Nonnull
                          android.content.SharedPreferences preferences,
                          @Nullable
                          T value)
Description copied from interface: Preference
Method puts (saves) preference represented by value in preferences container

Specified by:
putPreference in interface Preference<T>
Parameters:
preferences - preferences container
value - value to be saved

isSet

public boolean isSet(@Nonnull
                     android.content.SharedPreferences preferences)
Specified by:
isSet in interface Preference<T>
Parameters:
preferences - preferences container
Returns:
true if any value is saved in preferences container, false - otherwise

tryPutDefault

public final boolean tryPutDefault(@Nonnull
                                   android.content.SharedPreferences preferences)
Description copied from interface: Preference
Method applies default value to preference only if explicit value is not set

Specified by:
tryPutDefault in interface Preference<T>
Parameters:
preferences - preferences container
Returns:
true if default values have been applied, false otherwise

isSameKey

public final boolean isSameKey(@Nonnull
                               String key)
Specified by:
isSameKey in interface Preference<T>
Parameters:
key - preference key
Returns:
true if current preferences has the same key

getPersistedValue

@Nullable
protected abstract T getPersistedValue(@Nonnull
                                                android.content.SharedPreferences preferences)
Parameters:
preferences - preferences container
Returns:
preference value from preferences with key defined by getKey() method

putPersistedValue

protected abstract void putPersistedValue(@Nonnull
                                          android.content.SharedPreferences.Editor editor,
                                          @Nonnull
                                          T value)
Method saved preference to preferences container editor

Parameters:
editor - editor in which value must be saved
value - value to be saved


Copyright © 2014 se.solovyev. All Rights Reserved.