mobile/android/base/background/healthreport/HealthReportUtils.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/background/healthreport/HealthReportUtils.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,136 @@
     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.background.healthreport;
     1.9 +
    1.10 +import java.util.HashSet;
    1.11 +import java.util.Iterator;
    1.12 +import java.util.Set;
    1.13 +import java.util.SortedSet;
    1.14 +import java.util.TreeSet;
    1.15 +import java.util.UUID;
    1.16 +
    1.17 +import org.json.JSONArray;
    1.18 +import org.json.JSONException;
    1.19 +import org.json.JSONObject;
    1.20 +import org.mozilla.apache.commons.codec.digest.DigestUtils;
    1.21 +
    1.22 +import android.content.ContentUris;
    1.23 +import android.net.Uri;
    1.24 +
    1.25 +public class HealthReportUtils {
    1.26 +  public static final String LOG_TAG = HealthReportUtils.class.getSimpleName();
    1.27 +
    1.28 +  public static String getEnvironmentHash(final String input) {
    1.29 +    return DigestUtils.shaHex(input);
    1.30 +  }
    1.31 +
    1.32 +  /**
    1.33 +   * Take an environment URI (one that identifies an environment) and produce an
    1.34 +   * event URI.
    1.35 +   *
    1.36 +   * That this is needed is tragic.
    1.37 +   *
    1.38 +   * @param environmentURI
    1.39 +   *          the {@link Uri} returned by an environment operation.
    1.40 +   * @return a {@link Uri} to which insertions can be dispatched.
    1.41 +   */
    1.42 +  public static Uri getEventURI(Uri environmentURI) {
    1.43 +    return environmentURI.buildUpon().path("/events/" + ContentUris.parseId(environmentURI) + "/").build();
    1.44 +  }
    1.45 +
    1.46 +  /**
    1.47 +   * Copy the keys from the provided {@link JSONObject} into the provided {@link Set}.
    1.48 +   */
    1.49 +  private static <T extends Set<String>> T intoKeySet(T keys, JSONObject o) {
    1.50 +    if (o == null || o == JSONObject.NULL) {
    1.51 +      return keys;
    1.52 +    }
    1.53 +
    1.54 +    @SuppressWarnings("unchecked")
    1.55 +    Iterator<String> it = o.keys();
    1.56 +    while (it.hasNext()) {
    1.57 +      keys.add(it.next());
    1.58 +    }
    1.59 +    return keys;
    1.60 +  }
    1.61 +
    1.62 +  /**
    1.63 +   * Produce a {@link SortedSet} containing the string keys of the provided
    1.64 +   * object.
    1.65 +   *
    1.66 +   * @param o a {@link JSONObject} with string keys.
    1.67 +   * @return a sorted set.
    1.68 +   */
    1.69 +  public static SortedSet<String> sortedKeySet(JSONObject o) {
    1.70 +    return intoKeySet(new TreeSet<String>(), o);
    1.71 +  }
    1.72 +
    1.73 +  /**
    1.74 +   * Produce a {@link Set} containing the string keys of the provided object.
    1.75 +   * @param o a {@link JSONObject} with string keys.
    1.76 +   * @return an unsorted set.
    1.77 +   */
    1.78 +  public static Set<String> keySet(JSONObject o) {
    1.79 +    return intoKeySet(new HashSet<String>(), o);
    1.80 +  }
    1.81 +
    1.82 +  /**
    1.83 +   * Just like {@link JSONObject#accumulate(String, Object)}, but doesn't do the wrong thing for single values.
    1.84 +   * @throws JSONException 
    1.85 +   */
    1.86 +  public static void append(JSONObject o, String key, Object value) throws JSONException {
    1.87 +    if (!o.has(key)) {
    1.88 +      JSONArray arr = new JSONArray();
    1.89 +      arr.put(value);
    1.90 +      o.put(key, arr);
    1.91 +      return;
    1.92 +    }
    1.93 +    Object dest = o.get(key);
    1.94 +    if (dest instanceof JSONArray) {
    1.95 +      ((JSONArray) dest).put(value);
    1.96 +      return;
    1.97 +    }
    1.98 +    JSONArray arr = new JSONArray();
    1.99 +    arr.put(dest);
   1.100 +    arr.put(value);
   1.101 +    o.put(key, arr);
   1.102 +  }
   1.103 +
   1.104 +  /**
   1.105 +   * Accumulate counts for how often each provided value occurs.
   1.106 +   *
   1.107 +   * <code>
   1.108 +   *   HealthReportUtils.count(o, "foo", "bar");
   1.109 +   * </code>
   1.110 +   *
   1.111 +   * will change
   1.112 +   *
   1.113 +   * <pre>
   1.114 +   *   {"foo", {"bar": 1}}
   1.115 +   * </pre>
   1.116 +   *
   1.117 +   * into
   1.118 +   *
   1.119 +   * <pre>
   1.120 +   *   {"foo", {"bar": 2}}
   1.121 +   * </pre>
   1.122 +   *
   1.123 +   */
   1.124 +  public static void count(JSONObject o, String key,
   1.125 +                           String value) throws JSONException {
   1.126 +    if (!o.has(key)) {
   1.127 +      JSONObject counts = new JSONObject();
   1.128 +      counts.put(value, 1);
   1.129 +      o.put(key, counts);
   1.130 +      return;
   1.131 +    }
   1.132 +    JSONObject dest = o.getJSONObject(key);
   1.133 +    dest.put(value, dest.optInt(value, 0) + 1);
   1.134 +  }
   1.135 +
   1.136 +  public static String generateDocumentId() {
   1.137 +    return UUID.randomUUID().toString();
   1.138 +  }
   1.139 +}

mercurial