Home | History | Annotate | Download | only in ahat
      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 
     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    */
     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    */
     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    */
     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    *
     83    * The generated parameters will be sorted alphabetically so it is easier to
     84    * test.
     85    */
     86   public URI with(String name, String value) {
     87     StringBuilder newQuery = new StringBuilder();
     88     newQuery.append(mUri.getRawPath());
     89     newQuery.append('?');
     90 
     91     Map<String, String> params = new TreeMap<String, String>(mParams);
     92     params.put(name, value);
     93     String and = "";
     94     for (Map.Entry<String, String> entry : params.entrySet()) {
     95       newQuery.append(and);
     96       newQuery.append(entry.getKey());
     97       newQuery.append('=');
     98       newQuery.append(entry.getValue());
     99       and = "&";
    100     }
    101     return DocString.uri(newQuery.toString());
    102   }
    103 
    104   /**
    105    * Return a uri suitable for an href target that links to the current
    106    * page, except with the named query parameter set to the new long value.
    107    */
    108   public URI with(String name, long value) {
    109     return with(name, String.valueOf(value));
    110   }
    111 }
    112