summaryrefslogtreecommitdiffstats
path: root/mobile/android/services/src/main/java/org/mozilla/gecko/sync/repositories/RepositorySessionBundle.java
blob: 7908ec7971b39a4aa06becdecce29dfc3e04bcab (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
/* 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.sync.repositories;

import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.sync.NonObjectJSONException;

import java.io.IOException;

public class RepositorySessionBundle {
  public static final String LOG_TAG = RepositorySessionBundle.class.getSimpleName();

  protected static final String JSON_KEY_TIMESTAMP = "timestamp";

  protected final ExtendedJSONObject object;

  public RepositorySessionBundle(String jsonString) throws IOException, NonObjectJSONException {

    object = new ExtendedJSONObject(jsonString);
  }

  public RepositorySessionBundle(long lastSyncTimestamp) {
    object = new ExtendedJSONObject();
    this.setTimestamp(lastSyncTimestamp);
  }

  public long getTimestamp() {
    if (object.containsKey(JSON_KEY_TIMESTAMP)) {
      return object.getLong(JSON_KEY_TIMESTAMP);
    }

    return -1;
  }

  public void setTimestamp(long timestamp) {
    Logger.debug(LOG_TAG, "Setting timestamp to " + timestamp + ".");
    object.put(JSON_KEY_TIMESTAMP, timestamp);
  }

  public void bumpTimestamp(long timestamp) {
    long existing = this.getTimestamp();
    if (timestamp > existing) {
      this.setTimestamp(timestamp);
    } else {
      Logger.debug(LOG_TAG, "Timestamp " + timestamp + " not greater than " + existing + "; not bumping.");
    }
  }

  public String toJSONString() {
    return object.toJSONString();
  }
}