mobile/android/base/sync/repositories/RepositorySessionBundle.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/sync/repositories/RepositorySessionBundle.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,55 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.sync.repositories;
     1.9 +
    1.10 +import java.io.IOException;
    1.11 +
    1.12 +import org.json.simple.parser.ParseException;
    1.13 +import org.mozilla.gecko.background.common.log.Logger;
    1.14 +import org.mozilla.gecko.sync.ExtendedJSONObject;
    1.15 +import org.mozilla.gecko.sync.NonObjectJSONException;
    1.16 +
    1.17 +public class RepositorySessionBundle {
    1.18 +  public static final String LOG_TAG = RepositorySessionBundle.class.getSimpleName();
    1.19 +
    1.20 +  protected static final String JSON_KEY_TIMESTAMP = "timestamp";
    1.21 +
    1.22 +  protected final ExtendedJSONObject object;
    1.23 +
    1.24 +  public RepositorySessionBundle(String jsonString) throws IOException, ParseException, NonObjectJSONException {
    1.25 +    object = ExtendedJSONObject.parseJSONObject(jsonString);
    1.26 +  }
    1.27 +
    1.28 +  public RepositorySessionBundle(long lastSyncTimestamp) {
    1.29 +    object = new ExtendedJSONObject();
    1.30 +    this.setTimestamp(lastSyncTimestamp);
    1.31 +  }
    1.32 +
    1.33 +  public long getTimestamp() {
    1.34 +    if (object.containsKey(JSON_KEY_TIMESTAMP)) {
    1.35 +      return object.getLong(JSON_KEY_TIMESTAMP);
    1.36 +    }
    1.37 +
    1.38 +    return -1;
    1.39 +  }
    1.40 +
    1.41 +  public void setTimestamp(long timestamp) {
    1.42 +    Logger.debug(LOG_TAG, "Setting timestamp to " + timestamp + ".");
    1.43 +    object.put(JSON_KEY_TIMESTAMP, Long.valueOf(timestamp));
    1.44 +  }
    1.45 +
    1.46 +  public void bumpTimestamp(long timestamp) {
    1.47 +    long existing = this.getTimestamp();
    1.48 +    if (timestamp > existing) {
    1.49 +      this.setTimestamp(timestamp);
    1.50 +    } else {
    1.51 +      Logger.debug(LOG_TAG, "Timestamp " + timestamp + " not greater than " + existing + "; not bumping.");
    1.52 +    }
    1.53 +  }
    1.54 +
    1.55 +  public String toJSONString() {
    1.56 +    return object.toJSONString();
    1.57 +  }
    1.58 +}

mercurial