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

com.iwbfly.myhttp.http.MyhttpHeaderMap Maven / Gradle / Ivy

The newest version!
package com.iwbfly.myhttp.http;

import java.util.*;

/**
 * @author pangyajun
 * @since 2021/11/27 14:39
 **/
public class MyhttpHeaderMap {

    private final List headers;

    public MyhttpHeaderMap(List headers) {
        this.headers = headers;
    }

    public MyhttpHeaderMap() {
        this.headers = new LinkedList<>();
    }


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

    public String getValue(String name) {
        MyhttpHeader header = getHeader(name);
        if (header != null) {
            return header.getValue();
        }
        return null;
    }

    public List getValues(String name) {
        List results = new ArrayList<>(2);
        for (MyhttpHeader header : headers) {
            if (header.getName().equalsIgnoreCase(name)) {
                results.add(header.getValue());
            }
        }
        return Collections.unmodifiableList(results);
    }


    public MyhttpHeader getHeader(String name) {
        for (MyhttpHeader header : headers) {
            if (header.getName().equalsIgnoreCase(name)) {
                return header;
            }
        }
        return null;
    }

    public List getHeaders(String name) {
        List results = new ArrayList<>(2);
        for (MyhttpHeader header : headers) {
            if (header.getName().equalsIgnoreCase(name)) {
                results.add(header);
            }
        }
        return results;
    }


    public List names() {
        List results = new ArrayList<>(headers.size());
        for (MyhttpHeader header : headers) {
            results.add(header.getName());
        }
        return Collections.unmodifiableList(results);
    }


    public List getValues() {
        List results = new ArrayList<>(headers.size());
        for (MyhttpHeader header : headers) {
            results.add(header.getValue());
        }
        return Collections.unmodifiableList(results);
    }

    public void addHeader(MyhttpHeader header) {
        headers.add(header);
    }

    public void addHeader(String name, String value) {
        addHeader(new MyhttpHeader(name, value));
    }

    public void setHeader(String name, String value) {
        MyhttpHeader header = getHeader(name);
        if (header != null) {
            header.setValue(value);
        } else {
            addHeader(name, value);
        }
    }


    public Iterator headerIterator() {
        return headers.iterator();
    }

    public void remove(String name) {
        MyhttpHeader header = getHeader(name);
        if (header != null) {
            headers.remove(header);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy