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