Advertisement
Guest User

QueryObjectConverter for Retrofit2 (for Java7)

a guest
Nov 23rd, 2017
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.76 KB | None | 0 0
  1. import android.support.annotation.NonNull;
  2.  
  3. import com.google.gson.Gson;
  4. import com.google.gson.JsonElement;
  5.  
  6. import java.util.Collection;
  7. import java.util.LinkedHashSet;
  8. import java.util.LinkedList;
  9. import java.util.Map;
  10. import java.util.Objects;
  11. import java.util.Set;
  12.  
  13.  
  14. public class QueryObjectConverter {
  15.  
  16.     private final Gson gson;
  17.  
  18.     public QueryObjectConverter(Gson gson) {
  19.         this.gson = gson;
  20.     }
  21.  
  22.     /**
  23.      * Converts an object to serialized URI parameters.
  24.      *
  25.      * @param value The value to be converted (can be an object or collection).
  26.      * @return Serialized URI parameters for use with {@literal @}{@link retrofit2.http.QueryMap}(encoded=true).
  27.      */
  28.     public Map<String, String> convert(Object value) {
  29.         return convertTree(gson.toJsonTree(value));
  30.     }
  31.  
  32.     /**
  33.      * Converts the given JSON tree to serialized URI parameters. This is equivalent to helpers.js/encodeParams.
  34.      *
  35.      * @param tree The JSON tree (can be an object or array).
  36.      * @return Serialized URI parameters for use with {@literal @}{@link retrofit2.http.QueryMap}(encoded=false).
  37.      */
  38.     public Map<String, String> convertTree(JsonElement tree) {
  39.         ParamsMap params = new ParamsMap();
  40.         if (tree.isJsonArray()) {
  41.             int i = 0;
  42.             for (JsonElement element : tree.getAsJsonArray()) {
  43.                 buildObjectParams(Integer.toString(i), element, params);
  44.                 i++;
  45.             }
  46.         } else if (tree.isJsonObject()) {
  47.             for (Map.Entry<String, JsonElement> entry : tree.getAsJsonObject().entrySet()) {
  48.                 buildObjectParams(entry.getKey(), entry.getValue(), params);
  49.             }
  50.         } else if (!tree.isJsonNull()) {
  51.             throw new IllegalArgumentException("Cannot convert " + tree.toString());
  52.         }
  53.         return params;
  54.     }
  55.  
  56.     /**
  57.      * Recursive helper method for {@link #convertTree(JsonElement)}. This is equivalent to helpers.js/buildObjectParams.
  58.      *
  59.      * @param prefix The prefix for the parameter names.
  60.      * @param tree   The remaining JSON tree.
  61.      * @param params The params object to write to.
  62.      */
  63.     private void buildObjectParams(String prefix, JsonElement tree, ParamsMap params) {
  64.         if (tree.isJsonArray()) {
  65.             int i = 0;
  66.             for (JsonElement element : tree.getAsJsonArray()) {
  67.                 buildObjectParams(prefix + "[" + i + "]", element, params);
  68.                 i++;
  69.             }
  70.         } else if (tree.isJsonObject()) {
  71.             for (Map.Entry<String, JsonElement> entry : tree.getAsJsonObject().entrySet()) {
  72.                 buildObjectParams(prefix + "[" + entry.getKey() + "]", entry.getValue(), params);
  73.             }
  74.         } else if (tree.isJsonPrimitive()) {
  75.             params.put(prefix, tree.getAsJsonPrimitive().getAsString());
  76.         }
  77.     }
  78.  
  79.     /**
  80.      * A map class that allows multiple entries per key.
  81.      */
  82.     private static class ParamsMap implements Map<String, String> {
  83.  
  84.         private final Set<Entry<String, String>> entries = new LinkedHashSet<>();
  85.  
  86.         @Override
  87.         public int size() {
  88.             return entries.size();
  89.         }
  90.  
  91.         @Override
  92.         public boolean isEmpty() {
  93.             return entries.isEmpty();
  94.         }
  95.  
  96.         @Override
  97.         public boolean containsKey(Object key) {
  98.             for (Map.Entry<String, String> entry : entries) {
  99.                 if (entry.getKey().equals(key)) {
  100.                     return true;
  101.                 }
  102.             }
  103.             return false;
  104.         }
  105.  
  106.         @Override
  107.         public boolean containsValue(Object value) {
  108.             for (Map.Entry<String, String> entry : entries) {
  109.                 if (entry.getValue().equals(value)) {
  110.                     return true;
  111.                 }
  112.             }
  113.             return false;
  114.         }
  115.  
  116.         /**
  117.          * @param key The key to look for.
  118.          * @return The value of the FIRST matching entry or null if none matches.
  119.          */
  120.         @Override
  121.         public String get(Object key) {
  122.             for (Map.Entry<String, String> entry : entries) {
  123.                 if (entry.getKey().equals(key)) {
  124.                     return entry.getValue();
  125.                 }
  126.             }
  127.             return null;
  128.         }
  129.  
  130.         @Override
  131.         public String put(String key, String value) {
  132.             entries.add(new ParamEntry(key, value));
  133.             return null;
  134.         }
  135.  
  136.         @Override
  137.         public String remove(Object key) {
  138.  
  139.             return null;
  140.         }
  141.  
  142.         @Override
  143.         public void putAll(@NonNull Map<? extends String, ? extends String> m) {
  144.             for (Entry<? extends String, ? extends String> entry : m.entrySet()) {
  145.                 put(entry.getKey(), entry.getValue());
  146.             }
  147.         }
  148.  
  149.         @Override
  150.         public void clear() {
  151.             entries.clear();
  152.         }
  153.  
  154.         @NonNull
  155.         @Override
  156.         public Set<String> keySet() {
  157.             Set<String> kSet = new LinkedHashSet<>();
  158.             for (Map.Entry<String, String> entry : entries) {
  159.                 kSet.add(entry.getKey());
  160.             }
  161.             return kSet;
  162.         }
  163.  
  164.         @NonNull
  165.         @Override
  166.         public Collection<String> values() {
  167.             LinkedList<String> vList = new LinkedList<>();
  168.             for (Map.Entry<String, String> entry : entries) {
  169.                 vList.add(entry.getValue());
  170.             }
  171.             return vList;
  172.         }
  173.  
  174.         @NonNull
  175.         @Override
  176.         public Set<Entry<String, String>> entrySet() {
  177.             return entries;
  178.         }
  179.     }
  180.  
  181.     private static class ParamEntry implements Map.Entry<String, String> {
  182.         private final String key;
  183.         private final String value;
  184.  
  185.         public ParamEntry(String key, String value) {
  186.             this.key = Objects.requireNonNull(key);
  187.             this.value = Objects.requireNonNull(value);
  188.         }
  189.  
  190.         @Override
  191.         public String getKey() {
  192.             return key;
  193.         }
  194.  
  195.         @Override
  196.         public String getValue() {
  197.             return value;
  198.         }
  199.  
  200.         @Override
  201.         public String setValue(String value) {
  202.             throw new UnsupportedOperationException();
  203.         }
  204.  
  205.         @Override
  206.         public boolean equals(Object o) {
  207.             if (this == o) return true;
  208.             if (o == null || getClass() != o.getClass()) return false;
  209.             ParamEntry that = (ParamEntry) o;
  210.             return key.equals(that.key) && value.equals(that.value);
  211.  
  212.         }
  213.  
  214.         @Override
  215.         public int hashCode() {
  216.             int result = key.hashCode();
  217.             result = 31 * result + value.hashCode();
  218.             return result;
  219.         }
  220.     }
  221.  
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement