Android Open Source - TipOnDiscount Preferences File Persister






From Project

Back to project page TipOnDiscount.

License

The source code is released under:

GNU General Public License

If you think the Android project TipOnDiscount listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.itllp.tipOnDiscount.persistence.impl;
// w w  w  .ja  v a2s .  co  m
import java.math.BigDecimal;

import android.content.Context;
import android.content.SharedPreferences;

import com.itllp.tipOnDiscount.persistence.Persister;

/* The preferences are stored in a SharedPreferences file. 
 * All instances of an app share the same instance of this file 
 */
public class PreferencesFilePersister implements Persister {

  private String preferencesFileName = null;
  private SharedPreferences.Editor editor = null;
  
  public PreferencesFilePersister(String preferencesFileName) {
    this.preferencesFileName = preferencesFileName;
  }
  
  @Override
  public void beginSave(Context context) {
    SharedPreferences prefs = context.getSharedPreferences(
        this.preferencesFileName, 
        android.content.Context.MODE_PRIVATE);
    editor = prefs.edit();
    editor.clear();
  }

  @Override
  public void save(String key, BigDecimal value) throws Exception {
    if (null == editor) {
      throw new Exception("beginSave must be called first");
    }
    if (null == key) {
      throw new Exception("key may not be null");
    }
    if (null == value) {
      throw new Exception("value may not be null");
    }
        String stringValue = value.toPlainString();
        editor.putString(key, stringValue);
  }

  @Override
  public void save(String key, boolean value) throws Exception {
    if (null == editor) {
      throw new Exception("beginSave must be called first");
    }
    if (null == key) {
      throw new Exception("key may not be null");
    }
        editor.putBoolean(key, value);
  }

  @Override
  public void save(String key, int value) throws Exception {
    if (null == editor) {
      throw new Exception("beginSave not called");
    }
    if (null == key) {
      throw new Exception("key may not be null");
    }
        editor.putInt(key, value);
  }

  @Override
  public void endSave() throws Exception {
    if (null == editor) {
      throw new Exception("beginSave must be called first");
    }
    editor.commit();
    editor = null;
  }

  @Override
  public BigDecimal retrieveBigDecimal(Context context, String key) {
    SharedPreferences prefs =
        context.getSharedPreferences(
            this.preferencesFileName, 
            android.content.Context.MODE_PRIVATE);
    if (!prefs.contains(key)) {
      return null;
    }
    String noValue = "";
        String stringValue = prefs.getString(key, noValue);
        BigDecimal bigDecimalValue = null;
        if (null != stringValue) {
          try {
            bigDecimalValue = new BigDecimal(stringValue);
          } catch (NumberFormatException x) {
            // leave bigDecimalValue == null
          }
        }
    return bigDecimalValue;
  }

  
  @Override
  public Boolean retrieveBoolean(Context context, String key) {
    SharedPreferences prefs =
        context.getSharedPreferences(
            this.preferencesFileName, 
            android.content.Context.MODE_PRIVATE);
    if (!prefs.contains(key)) {
      return null;
    }
    Boolean booleanValue = null;
        try {
          boolean boolValue = prefs.getBoolean(key, false);
          booleanValue = Boolean.valueOf(boolValue);
        } catch (Exception x) {
          // leave booleanValue == null
        }
    return booleanValue;
  }

  
  @Override
  public Integer retrieveInteger(Context context, String key) {
    SharedPreferences prefs =
        context.getSharedPreferences(
            this.preferencesFileName, 
            android.content.Context.MODE_PRIVATE);
    if (!prefs.contains(key)) {
      return null;
    }
    Integer integerValue = null;
        try {
          int intValue = prefs.getInt(key, Integer.MIN_VALUE);
          integerValue = Integer.valueOf(intValue);
        } catch (Exception x) {
          // leave integerValue == null
        }
    return integerValue;
  }

}




Java Source Code List

android.content.Context.java
com.itllp.tipOnDiscount.SetDefaultsActivity.java
com.itllp.tipOnDiscount.TipOnDiscountApplication.java
com.itllp.tipOnDiscount.TipOnDiscountApplication.java
com.itllp.tipOnDiscount.TipOnDiscount.java
com.itllp.tipOnDiscount.defaults.DefaultsFactory.java
com.itllp.tipOnDiscount.defaults.DefaultsImpl.java
com.itllp.tipOnDiscount.defaults.Defaults.java
com.itllp.tipOnDiscount.defaults.persistence.DefaultsPersisterFactory.java
com.itllp.tipOnDiscount.defaults.persistence.DefaultsPersister.java
com.itllp.tipOnDiscount.defaults.persistence.impl.DefaultsPersisterImpl.java
com.itllp.tipOnDiscount.model.DataModelFactory.java
com.itllp.tipOnDiscount.model.DataModelInitializerFactory.java
com.itllp.tipOnDiscount.model.DataModelInitializer.java
com.itllp.tipOnDiscount.model.DataModelObservableTests.java
com.itllp.tipOnDiscount.model.DataModelObservable.java
com.itllp.tipOnDiscount.model.DataModelObserver.java
com.itllp.tipOnDiscount.model.DataModel.java
com.itllp.tipOnDiscount.model.impl.DataModelImplNotificationTests.java
com.itllp.tipOnDiscount.model.impl.DataModelImplTests.java
com.itllp.tipOnDiscount.model.impl.DataModelImpl.java
com.itllp.tipOnDiscount.model.impl.DataModelInitializerFromPersistedDefaultsTests.java
com.itllp.tipOnDiscount.model.impl.DataModelInitializerFromPersistedDefaults.java
com.itllp.tipOnDiscount.model.impl.SimpleDataModelInitializerTests.java
com.itllp.tipOnDiscount.model.impl.SimpleDataModelInitializer.java
com.itllp.tipOnDiscount.model.persistence.DataModelPersisterFactory.java
com.itllp.tipOnDiscount.model.persistence.DataModelPersister.java
com.itllp.tipOnDiscount.model.persistence.impl.DataModelPersisterImpl.java
com.itllp.tipOnDiscount.model.update.ActualTipAmountUpdate.java
com.itllp.tipOnDiscount.model.update.ActualTipRateUpdate.java
com.itllp.tipOnDiscount.model.update.BillSubtotalUpdate.java
com.itllp.tipOnDiscount.model.update.BillTotalUpdate.java
com.itllp.tipOnDiscount.model.update.BumpsUpdate.java
com.itllp.tipOnDiscount.model.update.DiscountUpdate.java
com.itllp.tipOnDiscount.model.update.PlannedTipAmountUpdate.java
com.itllp.tipOnDiscount.model.update.PlannedTipRateUpdate.java
com.itllp.tipOnDiscount.model.update.RoundUpToNearestUpdate.java
com.itllp.tipOnDiscount.model.update.ShareDueUpdate.java
com.itllp.tipOnDiscount.model.update.SplitBetweenUpdate.java
com.itllp.tipOnDiscount.model.update.TaxAmountUpdate.java
com.itllp.tipOnDiscount.model.update.TaxRateUpdate.java
com.itllp.tipOnDiscount.model.update.TippableAmountUpdate.java
com.itllp.tipOnDiscount.model.update.TotalDueUpdate.java
com.itllp.tipOnDiscount.model.update.UpdateSetTests.java
com.itllp.tipOnDiscount.model.update.UpdateSet.java
com.itllp.tipOnDiscount.model.update.Update.java
com.itllp.tipOnDiscount.persistence.Persister.java
com.itllp.tipOnDiscount.persistence.impl.PreferencesFilePersister.java
com.itllp.tipOnDiscount.util.BigDecimalLabelMap.java
com.itllp.tipOnDiscount.util.EqualsUtil.java