All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codehaus.httpcache4j.uri.QueryParams Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package org.codehaus.httpcache4j.uri;

import com.google.common.base.Splitter;

import java.text.Collator;
import java.util.*;

/**
 * @author Erlend Hamnaberg
 */
public final class QueryParams implements Iterable {
    private final Map> parameters = new LinkedHashMap>();

    public QueryParams() {
        this(Collections.>emptyMap());
    }

    public QueryParams(Map> parameters) {
        this.parameters.putAll(parameters);
    }

    public QueryParams(Iterable parameters) {
        this(toMap(parameters));
    }

    public QueryParams empty() {
        return new QueryParams();
    }

    public boolean isEmpty() {
        return parameters.isEmpty();
    }

    public boolean contains(String name) {
        return parameters.containsKey(name);
    }

    public boolean contains(String name, String value) {
        return contains(new QueryParam(name, value));
    }

    public boolean contains(QueryParam parameter) {
        List values = parameters.get(parameter.getName());
        return values != null && (values.isEmpty() && parameter.isEmpty() || values.contains(parameter.getValue()));
    }

    public QueryParams add(String name, String... value) {
        List p = new ArrayList();
        if (value.length == 0) {
            p.add(new QueryParam(name, null));
        }
        for (String v: value) {
            p.add(new QueryParam(name, v));
        }
        return add(p);
    }

    public QueryParams add(QueryParam param) {
        return add(Arrays.asList(param));
    }

    public QueryParams add(Iterable params) {
        Map> map = toMap(params);
        return add(map);
    }

    public QueryParams add(Map> params) {
        if (params.isEmpty()) {
            return this;
        }
        LinkedHashMap> copy = copy();
        copy.putAll(params);
        return new QueryParams(copy);
    }

    public QueryParams set(Map> params) {
        return new QueryParams(params);
    }

    public QueryParams set(Iterable params) {
        Map> map = toMap(params);
        return new QueryParams(map);
    }

    public QueryParams set(String name, String value) {
        LinkedHashMap> copy = copy();
        copy.remove(name);
        if (value != null) {
            ArrayList list = new ArrayList();
            list.add(value);
            copy.put(name, list);
        }
        return new QueryParams(copy);
    }

    public QueryParams set(String name, List value) {
        LinkedHashMap> copy = copy();
        copy.remove(name);
        if (!value.isEmpty()) {
            ArrayList list = new ArrayList();
            list.addAll(value);
            copy.put(name, list);
        }
        return new QueryParams(copy);
    }

    public List get(String name) {
        List list = parameters.get(name);
        if (list != null) {
            return Collections.unmodifiableList(list);
        }
        return Collections.emptyList();
    }

    public String getFirst(String name) {
        List values = get(name);
        if (!values.isEmpty()) return values.get(0);
        return null;
    }

    public QueryParams remove(String name) {
        if (!parameters.containsKey(name)) {
            return this;
        }
        LinkedHashMap> copy = copy();
        copy.remove(name);
        return new QueryParams(copy);
    }

    public List asList() {
        List list = new ArrayList();
        for (Map.Entry> entry : parameters.entrySet()) {
            if (entry.getValue().isEmpty()) {
                list.add(new QueryParam(entry.getKey(), ""));
            }
            for (String value : entry.getValue()) {
                list.add(new QueryParam(entry.getKey(), value));
            }
        }
        return Collections.unmodifiableList(list);
    }

    public Map> asMap() {
        return Collections.unmodifiableMap(parameters);
    }

    public String toQuery(boolean sort) {
        StringBuilder builder = new StringBuilder();
        List params = new ArrayList(asList());
        if (sort) {
            Collections.sort(params, new Comparator() {
                @Override
                public int compare(QueryParam o1, QueryParam o2) {
                    //TODO: Possible bug here.
                    return Collator.getInstance(Locale.getDefault()).compare(o1.getName(), o2.getName());
                }
            });
        }
        for (QueryParam parameter : params) {
            if (builder.length() > 0) {
                builder.append("&");
            }
            builder.append(URIEncoder.encodeUTF8(parameter.getName()));
            if(!parameter.isEmpty()) {
                builder.append("=").append(URIEncoder.encodeUTF8(parameter.getValue()));
            }
        }
        if (builder.length() == 0) {
            return null;
        }
        return builder.toString();
    }

    private LinkedHashMap> copy() {
        return new LinkedHashMap>(this.parameters);
    }

    @Override
    public Iterator iterator() {
        return asList().iterator();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        QueryParams that = (QueryParams) o;

        if (!parameters.equals(that.parameters)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return parameters.hashCode();
    }


    public static QueryParams parse(String query) {
        Map> map = new LinkedHashMap>();
        if (query != null) {
            Iterable parts = Splitter.on("&").omitEmptyStrings().trimResults().split(query);
            for (String part : parts) {
                String[] equalParts = part.split("=");
                String name = null;
                String value = null;
                if (equalParts.length == 1) {
                    name = equalParts[0];
                }
                else if (equalParts.length == 2) {
                    name = equalParts[0];
                    value = equalParts[1];
                }
                if (name != null) {
                    addToQueryMap(map, URIDecoder.decodeUTF8(name), URIDecoder.decodeUTF8(value));
                }
            }
        }

        return new QueryParams(map);
    }

    private static Map> toMap(Iterable parameters) {
        Map> map = new LinkedHashMap>();
        for (QueryParam parameter : parameters) {
            addToQueryMap(map, parameter.getName(), parameter.getValue());
        }
        return map;
    }

    private static void addToQueryMap(Map> map, String name, String value) {
        List list = map.get(name);
        if (list == null) {
            list = new ArrayList();
        }
        if (value != null) {
            list.add(value);
        }
        map.put(name, list);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy