content: /*
   * Copyright (C) 2014 Alexrs95
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package me.alexrs.prefs.lib;
  
  import android.content.Context;
  import android.content.SharedPreferences;
  import android.preference.PreferenceManager;
  
  import java.util.Map;
  import java.util.Set;
  
  /**
   * @author Alejandro Rodriguez (https://github.com/Alexrs95/Prefs)
   *
   *         Wrapper over the Android Preferences which provides a fluid syntax
   */
  public class Prefs {
  
      static Prefs singleton = null;
  
      static String prefName = null;
  
      static SharedPreferences preferences;
  
      static SharedPreferences.Editor editor;
  
      Prefs(Context context) {
          preferences = PreferenceManager.getDefaultSharedPreferences(context);
          editor = preferences.edit();
          prefName = null;
      }
  
      Prefs(Context context, String name, int mode) {
          preferences = context.getSharedPreferences(name, mode);
          editor = preferences.edit();
          prefName = name;
      }
  
      public static Prefs with(Context context) {
          if (singleton == null) {
              singleton = new Builder(context, null, -1).build();
          }
          return singleton;
      }
  
      public static Prefs with(Context context, String name, int mode) {
          if(singleton == null || (prefName != null && !prefName.equals(name))) {
              singleton = new Builder(context, name, mode).build();
          }
          return singleton;
      }
  
      public void save(String key, boolean value) {
          editor.putBoolean(key, value).apply();
      }
  
      public void save(String key, String value) {
          editor.putString(key, value).apply();
      }
  
      public void save(String key, int value) {
          editor.putInt(key, value).apply();
      }
  
      public void save(String key, float value) {
          editor.putFloat(key, value).apply();
      }
  
      public void save(String key, long value) {
          editor.putLong(key, value).apply();
      }
  
      public void save(String key, Set<String> value) {
          editor.putStringSet(key, value).apply();
      }
  
      public boolean getBoolean(String key, boolean defValue) {
          return preferences.getBoolean(key, defValue);
      }
  
      public String getString(String key, String defValue) {
          return preferences.getString(key, defValue);
      }
  
      public int getInt(String key, int defValue) {
          return preferences.getInt(key, defValue);
      }
  
      public float getFloat(String key, float defValue) {
          return preferences.getFloat(key, defValue);
      }
  
      public long getLong(String key, long defValue) {
          return preferences.getLong(key, defValue);
      }
  
      public Set<String> getStringSet(String key, Set<String> defValue) {
          return preferences.getStringSet(key, defValue);
      }
  
      public Map<String, ?> getAll() {
          return preferences.getAll();
      }
  
      public void remove(String key) {
          editor.remove(key).apply();
      }
  
      public void removeAll(){
          editor.clear().apply();
      }
  
      public void contains(String key){
          preferences.contains(key);
      }
  
      private static class Builder {
  
          private final Context context;
          private final int mode;
          private final String name;
  
          public Builder(Context context, String name, int mode) {
              if (context == null) {
                  throw new IllegalArgumentException("Context must not be null.");
              }
              this.context = context.getApplicationContext();
              this.name = name;
              this.mode = mode;
          }
  
          /**
           * Method that creates an instance of Prefs
           *
           * @return an instance of Prefs
           */
          public Prefs build() {
              if (mode == -1 || name == null) {
                  return new Prefs(context);
              }
              return new Prefs(context, name, mode);
          }
      }
  }
  