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

org.apache.wink.common.internal.MultivaluedMapImpl Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *  
 *******************************************************************************/
package org.apache.wink.common.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.ws.rs.core.MultivaluedMap;

public class MultivaluedMapImpl // extends LinkedHashMap>
    implements MultivaluedMap, Cloneable {

    private static final long     serialVersionUID = -1942980976209902832L;

    private final Map> map;

    public MultivaluedMapImpl() {
        map = new LinkedHashMap>();
    }

    /**
     * Used to create a CaseInsensitiveMultivaluedMap
     * 
     * @param keyComparator
     */
    MultivaluedMapImpl(Comparator keyComparator) {
        map = new TreeMap>(keyComparator);
    }

    public MultivaluedMapImpl(Map map) {
        this();
        for (K key : map.keySet()) {
            add(key, map.get(key));
        }
    }

    public void add(K key, V value) {
        List list = getOrCreate(key);
        list.add(value);
    }

    public V getFirst(K key) {
        List list = get(key);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    public void putSingle(K key, V value) {
        List list = getOrCreate(key);
        list.clear();
        list.add(value);
    }

    private List getOrCreate(K key) {
        List list = this.get(key);
        if (list == null) {
            list = createValueList(key);
            this.put(key, list);
        }
        return list;
    }

    private List createValueList(K key) {
        return new ArrayList();
    }

    public MultivaluedMapImpl clone() {
        return clone(this);
    }

    public static  MultivaluedMapImpl clone(MultivaluedMap src) {
        MultivaluedMapImpl clone = new MultivaluedMapImpl();
        copy(src, clone);
        return clone;
    }

    public static  void copy(MultivaluedMap src, MultivaluedMap dest) {
        for (K key : src.keySet()) {
            List value = src.get(key);
            List newValue = new ArrayList();
            newValue.addAll(value);
            dest.put(key, newValue);
        }
    }

    public static  void addAll(MultivaluedMap src, MultivaluedMap dest) {
        for (K key : src.keySet()) {
            List srcList = src.get(key);
            List destList = dest.get(key);
            if (destList == null) {
                destList = new ArrayList(srcList.size());
                dest.put(key, destList);
            }
            destList.addAll(srcList);
        }
    }

    @Override
    public String toString() {
        return "[" + toString(this, ",") + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    }

    public static String toString(MultivaluedMap map, String delimiter) {
        StringBuilder result = new StringBuilder();
        MultivaluedMap params = map;
        String delim = ""; //$NON-NLS-1$
        for (Object name : params.keySet()) {
            for (Object value : params.get(name)) {
                result.append(delim);
                if(name == null) {
                    result.append("null"); //$NON-NLS-1$
                } else {
                    result.append(name.toString());
                }
                if (value != null) {
                    result.append('=');
                    result.append(value.toString());
                }
                delim = delimiter;
            }
        }
        return result.toString();
    }

    public static MultivaluedMap toMultivaluedMapString(Map values) {
        MultivaluedMap mValues = new MultivaluedMapImpl();
        for (String key : values.keySet()) {
            Object value = values.get(key);
            if (value == null) {
                mValues.add(key, null);
            } else if (value instanceof Object[]) {
                for (Object obj : (Object[])value) {
                    if (obj == null) {
                        mValues.add(key, null);
                    } else {
                        mValues.add(key, obj.toString());
                    }
                }
            } else if (value instanceof List) {
                for (Object obj : (List)value) {
                    if (obj == null) {
                        mValues.add(key, null);
                    } else {
                        mValues.add(key, obj.toString());
                    }
                }
            } else {
                mValues.add(key, value.toString());
            }
        }
        return mValues;
    }

    public void clear() {
        map.clear();
    }

    public boolean containsKey(Object key) {
        return map.containsKey(key);
    }

    public boolean containsValue(Object value) {
        return map.containsValue(value);
    }

    public Set>> entrySet() {
        return map.entrySet();
    }

    public boolean equals(Object o) {
        return map.equals(o);
    }

    public List get(Object key) {
        return map.get(key);
    }

    public int hashCode() {
        return map.hashCode();
    }

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

    public Set keySet() {
        return map.keySet();
    }

    public List put(K key, List value) {
        return map.put(key, value);
    }

    public void putAll(Map> t) {
        map.putAll(t);
    }

    public List remove(Object key) {
        return map.remove(key);
    }

    public int size() {
        return map.size();
    }

    public Collection> values() {
        return map.values();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy