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

org.apache.dubbo.remoting.http12.message.HttpHeadersMapAdapter Maven / Gradle / Ivy

/*
 * 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.dubbo.remoting.http12.message;

import org.apache.dubbo.remoting.http12.HttpHeaders;

import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public final class HttpHeadersMapAdapter implements Map> {

    private final HttpHeaders headers;

    public HttpHeadersMapAdapter(HttpHeaders headers) {
        this.headers = headers;
    }

    @Override
    public int size() {
        return headers.size();
    }

    @Override
    public boolean isEmpty() {
        return headers.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
        return key instanceof CharSequence && headers.containsKey((CharSequence) key);
    }

    @Override
    public boolean containsValue(Object value) {
        for (Entry entry : headers) {
            if (Objects.equals(value, entry.getValue())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public List get(Object key) {
        return key instanceof CharSequence ? headers.get((CharSequence) key) : Collections.emptyList();
    }

    @Override
    public List put(String key, List value) {
        List all = headers.get(key);
        headers.set(key, value);
        return all;
    }

    @Override
    public List remove(Object key) {
        return key instanceof CharSequence ? headers.remove((CharSequence) key) : Collections.emptyList();
    }

    @Override
    public void putAll(Map> m) {
        headers.set(m);
    }

    @Override
    public void clear() {
        headers.clear();
    }

    @Override
    public Set keySet() {
        return headers.names();
    }

    @Override
    public Collection> values() {
        Set names = headers.nameSet();
        return new AbstractCollection>() {
            @Override
            public Iterator> iterator() {
                Iterator it = names.iterator();
                return new Iterator>() {
                    @Override
                    public boolean hasNext() {
                        return it.hasNext();
                    }

                    @Override
                    public List next() {
                        CharSequence next = it.next();
                        return next == null ? Collections.emptyList() : headers.get(next);
                    }
                };
            }

            @Override
            public int size() {
                return names.size();
            }
        };
    }

    @Override
    public Set>> entrySet() {
        Set names = headers.nameSet();
        return new AbstractSet>>() {
            @Override
            public Iterator>> iterator() {
                Iterator it = names.iterator();
                return new Iterator>>() {
                    @Override
                    public boolean hasNext() {
                        return it.hasNext();
                    }

                    @Override
                    public Entry> next() {
                        CharSequence next = it.next();
                        return new Entry>() {
                            @Override
                            public String getKey() {
                                return next == null ? null : next.toString();
                            }

                            @Override
                            public List getValue() {
                                return next == null ? Collections.emptyList() : get(next);
                            }

                            @Override
                            public List setValue(List value) {
                                if (next == null) {
                                    return Collections.emptyList();
                                }
                                List values = get(next);
                                headers.set(next, value);
                                return values;
                            }
                        };
                    }
                };
            }

            @Override
            public int size() {
                return names.size();
            }
        };
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof HttpHeadersMapAdapter && headers.equals(((HttpHeadersMapAdapter) obj).headers);
    }

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

    @Override
    public String toString() {
        return "HttpHeadersMapAdapter{" + "headers=" + headers + '}';
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy