summaryrefslogtreecommitdiffstats
path: root/mobile/android/services/src/main/java/org/mozilla/gecko/background/common/EditorBranch.java
blob: 1ead09afae03a099f9006c1a606159cb68e18187 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.background.common;

import java.util.Set;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class EditorBranch implements Editor {

  private final String prefix;
  private Editor editor;

  public EditorBranch(final SharedPreferences prefs, final String prefix) {
    if (!prefix.endsWith(".")) {
      throw new IllegalArgumentException("No trailing period in prefix.");
    }
    this.prefix = prefix;
    this.editor = prefs.edit();
  }

  @Override
  public void apply() {
    this.editor.apply();
  }

  @Override
  public Editor clear() {
    this.editor = this.editor.clear();
    return this;
  }

  @Override
  public boolean commit() {
    return this.editor.commit();
  }

  @Override
  public Editor putBoolean(String key, boolean value) {
    this.editor = this.editor.putBoolean(prefix + key, value);
    return this;
  }

  @Override
  public Editor putFloat(String key, float value) {
    this.editor = this.editor.putFloat(prefix + key, value);
    return this;
  }

  @Override
  public Editor putInt(String key, int value) {
    this.editor = this.editor.putInt(prefix + key, value);
    return this;
  }

  @Override
  public Editor putLong(String key, long value) {
    this.editor = this.editor.putLong(prefix + key, value);
    return this;
  }

  @Override
  public Editor putString(String key, String value) {
    this.editor = this.editor.putString(prefix + key, value);
    return this;
  }

  // Not marking as Override, because Android <= 10 doesn't have
  // putStringSet. Neither can we implement it.
  public Editor putStringSet(String key, Set<String> value) {
    throw new RuntimeException("putStringSet not available.");
  }

  @Override
  public Editor remove(String key) {
    this.editor = this.editor.remove(prefix + key);
    return this;
  }
}