PageRenderTime 19ms CodeModel.GetById 15ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/googlecode/charts4j/UrlUtil.java

http://charts4j.googlecode.com/
Java | 77 lines | 29 code | 6 blank | 42 comment | 5 complexity | 799a35e0e0a572b18d67ba7c7bff2aa4 MD5 | raw file
 1/**
 2 *
 3 * The MIT License
 4 *
 5 * Copyright (c) 2011 the original author or authors.
 6 *
 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
 8 * of this software and associated documentation files (the "Software"), to deal
 9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26package com.googlecode.charts4j;
27
28import java.util.Arrays;
29import java.util.Collections;
30import java.util.List;
31
32import com.googlecode.charts4j.collect.ImmutableList;
33import com.googlecode.charts4j.collect.Lists;
34
35/**
36 * Small URL utility class.
37 *
38 * @author Julien Chastang (julien.c.chastang at gmail dot com)
39 *
40 */
41public final class UrlUtil {
42
43    /**
44     * Cannot be instantiated.
45     */
46    private UrlUtil() {
47        throw new AssertionError();
48    }
49
50    /**
51     * A method that reformats the URL so that the parameters appear in
52     * alphabetical order. This is useful for comparing URL strings in unit
53     * tests, for instance.
54     *
55     * @param s
56     *            String to be normalized
57     * @return Normalized string
58     */
59    public static String normalize(final String s) {
60        if (s == null) {
61            return "";
62        }
63        final ImmutableList<String> stringList = Lists.of(s.split("\\?"));
64        if (stringList.size() != 2) {
65            return s;
66        }
67        final String args = stringList.get(1);
68        final List<String> params = Arrays.asList(args.split("&"));
69        Collections.sort(params);
70        final StringBuilder sb = new StringBuilder(stringList.get(0) + "?");
71        int cnt = 0;
72        for (String p : params) {
73            sb.append(cnt++ > 0 ? "&" : "").append(p);
74        }
75        return sb.toString();
76    }
77}