1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ahat;
18 
19 import java.net.URI;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.TreeMap;
23 
24 /**
25  * A class for getting and modifying query parameters.
26  */
27 class Query {
28   private URI mUri;
29 
30   // Map from parameter name to value. If the same parameter appears multiple
31   // times, only the last value is used.
32   private Map<String, String> mParams;
33 
Query(URI uri)34   public Query(URI uri) {
35     mUri = uri;
36     mParams = new HashMap<String, String>();
37 
38     String query = uri.getQuery();
39     if (query != null) {
40       for (String param : query.split("&")) {
41         int i = param.indexOf('=');
42         if (i < 0) {
43           mParams.put(param, "");
44         } else {
45           mParams.put(param.substring(0, i), param.substring(i + 1));
46         }
47       }
48     }
49   }
50 
51   /**
52    * Return the value of a query parameter with the given name.
53    * If there is no query parameter with that name, returns the default value.
54    * If there are multiple query parameters with that name, the value of the
55    * last query parameter is returned.
56    * If the parameter is defined with an empty value, "" is returned.
57    */
get(String name, String defaultValue)58   public String get(String name, String defaultValue) {
59     String value = mParams.get(name);
60     return (value == null) ? defaultValue : value;
61   }
62 
63   /**
64    * Return the long value of a query parameter with the given name.
65    */
getLong(String name, long defaultValue)66   public long getLong(String name, long defaultValue) {
67     String value = get(name, null);
68     return value == null ? defaultValue : Long.decode(value);
69   }
70 
71   /**
72    * Return the int value of a query parameter with the given name.
73    */
getInt(String name, int defaultValue)74   public int getInt(String name, int defaultValue) {
75     String value = get(name, null);
76     return value == null ? defaultValue : Integer.decode(value);
77   }
78 
79   /**
80    * Return a uri suitable for an href target that links to the current
81    * page, except with the named query parameter set to the new value.
82    * <p>
83    * <code>value</code> may be null to remove the named query parameter.
84    * <p>
85    * The generated parameters will be sorted alphabetically so it is easier to
86    * test.
87    */
with(String name, String value)88   public URI with(String name, String value) {
89     StringBuilder newQuery = new StringBuilder();
90     newQuery.append(mUri.getRawPath());
91     newQuery.append('?');
92 
93     Map<String, String> params = new TreeMap<String, String>(mParams);
94     params.put(name, value);
95     String and = "";
96     for (Map.Entry<String, String> entry : params.entrySet()) {
97       if (entry.getValue() != null) {
98         newQuery.append(and);
99         newQuery.append(entry.getKey());
100         newQuery.append('=');
101         newQuery.append(entry.getValue());
102         and = "&";
103       }
104     }
105     return DocString.uri(newQuery.toString());
106   }
107 
108   /**
109    * Return a uri suitable for an href target that links to the current
110    * page, except with the named query parameter set to the new long value.
111    */
with(String name, long value)112   public URI with(String name, long value) {
113     return with(name, String.valueOf(value));
114   }
115 }
116