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.background.healthreport; michael@0: michael@0: import java.util.HashSet; michael@0: import java.util.Iterator; michael@0: import java.util.Set; michael@0: import java.util.SortedSet; michael@0: import java.util.TreeSet; michael@0: import java.util.UUID; michael@0: michael@0: import org.json.JSONArray; michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: import org.mozilla.apache.commons.codec.digest.DigestUtils; michael@0: michael@0: import android.content.ContentUris; michael@0: import android.net.Uri; michael@0: michael@0: public class HealthReportUtils { michael@0: public static final String LOG_TAG = HealthReportUtils.class.getSimpleName(); michael@0: michael@0: public static String getEnvironmentHash(final String input) { michael@0: return DigestUtils.shaHex(input); michael@0: } michael@0: michael@0: /** michael@0: * Take an environment URI (one that identifies an environment) and produce an michael@0: * event URI. michael@0: * michael@0: * That this is needed is tragic. michael@0: * michael@0: * @param environmentURI michael@0: * the {@link Uri} returned by an environment operation. michael@0: * @return a {@link Uri} to which insertions can be dispatched. michael@0: */ michael@0: public static Uri getEventURI(Uri environmentURI) { michael@0: return environmentURI.buildUpon().path("/events/" + ContentUris.parseId(environmentURI) + "/").build(); michael@0: } michael@0: michael@0: /** michael@0: * Copy the keys from the provided {@link JSONObject} into the provided {@link Set}. michael@0: */ michael@0: private static > T intoKeySet(T keys, JSONObject o) { michael@0: if (o == null || o == JSONObject.NULL) { michael@0: return keys; michael@0: } michael@0: michael@0: @SuppressWarnings("unchecked") michael@0: Iterator it = o.keys(); michael@0: while (it.hasNext()) { michael@0: keys.add(it.next()); michael@0: } michael@0: return keys; michael@0: } michael@0: michael@0: /** michael@0: * Produce a {@link SortedSet} containing the string keys of the provided michael@0: * object. michael@0: * michael@0: * @param o a {@link JSONObject} with string keys. michael@0: * @return a sorted set. michael@0: */ michael@0: public static SortedSet sortedKeySet(JSONObject o) { michael@0: return intoKeySet(new TreeSet(), o); michael@0: } michael@0: michael@0: /** michael@0: * Produce a {@link Set} containing the string keys of the provided object. michael@0: * @param o a {@link JSONObject} with string keys. michael@0: * @return an unsorted set. michael@0: */ michael@0: public static Set keySet(JSONObject o) { michael@0: return intoKeySet(new HashSet(), o); michael@0: } michael@0: michael@0: /** michael@0: * Just like {@link JSONObject#accumulate(String, Object)}, but doesn't do the wrong thing for single values. michael@0: * @throws JSONException michael@0: */ michael@0: public static void append(JSONObject o, String key, Object value) throws JSONException { michael@0: if (!o.has(key)) { michael@0: JSONArray arr = new JSONArray(); michael@0: arr.put(value); michael@0: o.put(key, arr); michael@0: return; michael@0: } michael@0: Object dest = o.get(key); michael@0: if (dest instanceof JSONArray) { michael@0: ((JSONArray) dest).put(value); michael@0: return; michael@0: } michael@0: JSONArray arr = new JSONArray(); michael@0: arr.put(dest); michael@0: arr.put(value); michael@0: o.put(key, arr); michael@0: } michael@0: michael@0: /** michael@0: * Accumulate counts for how often each provided value occurs. michael@0: * michael@0: * michael@0: * HealthReportUtils.count(o, "foo", "bar"); michael@0: * michael@0: * michael@0: * will change michael@0: * michael@0: *
michael@0:    *   {"foo", {"bar": 1}}
michael@0:    * 
michael@0: * michael@0: * into michael@0: * michael@0: *
michael@0:    *   {"foo", {"bar": 2}}
michael@0:    * 
michael@0: * michael@0: */ michael@0: public static void count(JSONObject o, String key, michael@0: String value) throws JSONException { michael@0: if (!o.has(key)) { michael@0: JSONObject counts = new JSONObject(); michael@0: counts.put(value, 1); michael@0: o.put(key, counts); michael@0: return; michael@0: } michael@0: JSONObject dest = o.getJSONObject(key); michael@0: dest.put(value, dest.optInt(value, 0) + 1); michael@0: } michael@0: michael@0: public static String generateDocumentId() { michael@0: return UUID.randomUUID().toString(); michael@0: } michael@0: }