Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.sync.repositories; |
michael@0 | 6 | |
michael@0 | 7 | import java.io.IOException; |
michael@0 | 8 | |
michael@0 | 9 | import org.json.simple.parser.ParseException; |
michael@0 | 10 | import org.mozilla.gecko.background.common.log.Logger; |
michael@0 | 11 | import org.mozilla.gecko.sync.ExtendedJSONObject; |
michael@0 | 12 | import org.mozilla.gecko.sync.NonObjectJSONException; |
michael@0 | 13 | |
michael@0 | 14 | public class RepositorySessionBundle { |
michael@0 | 15 | public static final String LOG_TAG = RepositorySessionBundle.class.getSimpleName(); |
michael@0 | 16 | |
michael@0 | 17 | protected static final String JSON_KEY_TIMESTAMP = "timestamp"; |
michael@0 | 18 | |
michael@0 | 19 | protected final ExtendedJSONObject object; |
michael@0 | 20 | |
michael@0 | 21 | public RepositorySessionBundle(String jsonString) throws IOException, ParseException, NonObjectJSONException { |
michael@0 | 22 | object = ExtendedJSONObject.parseJSONObject(jsonString); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | public RepositorySessionBundle(long lastSyncTimestamp) { |
michael@0 | 26 | object = new ExtendedJSONObject(); |
michael@0 | 27 | this.setTimestamp(lastSyncTimestamp); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | public long getTimestamp() { |
michael@0 | 31 | if (object.containsKey(JSON_KEY_TIMESTAMP)) { |
michael@0 | 32 | return object.getLong(JSON_KEY_TIMESTAMP); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | return -1; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | public void setTimestamp(long timestamp) { |
michael@0 | 39 | Logger.debug(LOG_TAG, "Setting timestamp to " + timestamp + "."); |
michael@0 | 40 | object.put(JSON_KEY_TIMESTAMP, Long.valueOf(timestamp)); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | public void bumpTimestamp(long timestamp) { |
michael@0 | 44 | long existing = this.getTimestamp(); |
michael@0 | 45 | if (timestamp > existing) { |
michael@0 | 46 | this.setTimestamp(timestamp); |
michael@0 | 47 | } else { |
michael@0 | 48 | Logger.debug(LOG_TAG, "Timestamp " + timestamp + " not greater than " + existing + "; not bumping."); |
michael@0 | 49 | } |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | public String toJSONString() { |
michael@0 | 53 | return object.toJSONString(); |
michael@0 | 54 | } |
michael@0 | 55 | } |