michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.sync.repositories; michael@0: michael@0: import java.io.IOException; michael@0: michael@0: import org.json.simple.parser.ParseException; michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.sync.ExtendedJSONObject; michael@0: import org.mozilla.gecko.sync.NonObjectJSONException; michael@0: michael@0: public class RepositorySessionBundle { michael@0: public static final String LOG_TAG = RepositorySessionBundle.class.getSimpleName(); michael@0: michael@0: protected static final String JSON_KEY_TIMESTAMP = "timestamp"; michael@0: michael@0: protected final ExtendedJSONObject object; michael@0: michael@0: public RepositorySessionBundle(String jsonString) throws IOException, ParseException, NonObjectJSONException { michael@0: object = ExtendedJSONObject.parseJSONObject(jsonString); michael@0: } michael@0: michael@0: public RepositorySessionBundle(long lastSyncTimestamp) { michael@0: object = new ExtendedJSONObject(); michael@0: this.setTimestamp(lastSyncTimestamp); michael@0: } michael@0: michael@0: public long getTimestamp() { michael@0: if (object.containsKey(JSON_KEY_TIMESTAMP)) { michael@0: return object.getLong(JSON_KEY_TIMESTAMP); michael@0: } michael@0: michael@0: return -1; michael@0: } michael@0: michael@0: public void setTimestamp(long timestamp) { michael@0: Logger.debug(LOG_TAG, "Setting timestamp to " + timestamp + "."); michael@0: object.put(JSON_KEY_TIMESTAMP, Long.valueOf(timestamp)); michael@0: } michael@0: michael@0: public void bumpTimestamp(long timestamp) { michael@0: long existing = this.getTimestamp(); michael@0: if (timestamp > existing) { michael@0: this.setTimestamp(timestamp); michael@0: } else { michael@0: Logger.debug(LOG_TAG, "Timestamp " + timestamp + " not greater than " + existing + "; not bumping."); michael@0: } michael@0: } michael@0: michael@0: public String toJSONString() { michael@0: return object.toJSONString(); michael@0: } michael@0: }