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

cn.ipokerface.weixin.request.http.apache.Header Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package cn.ipokerface.weixin.request.http.apache;

import java.util.*;

/**
 * Created by       PokerFace
 * Create Date      2019-12-27.
 * Email:           [email protected]
 * Version          1.0.0
 * 

* Description: */ public class Header implements Iterable { private final List fields; private final Map> fieldMap; public Header() { super(); this.fields = new LinkedList(); this.fieldMap = new HashMap>(); } public void addField(final MinimalField field) { if (field == null) { return; } final String key = field.getName().toLowerCase(Locale.ROOT); List values = this.fieldMap.get(key); if (values == null) { values = new LinkedList(); this.fieldMap.put(key, values); } values.add(field); this.fields.add(field); } public List getFields() { return new ArrayList(this.fields); } public MinimalField getField(final String name) { if (name == null) { return null; } final String key = name.toLowerCase(Locale.ROOT); final List list = this.fieldMap.get(key); if (list != null && !list.isEmpty()) { return list.get(0); } return null; } public List getFields(final String name) { if (name == null) { return null; } final String key = name.toLowerCase(Locale.ROOT); final List list = this.fieldMap.get(key); if (list == null || list.isEmpty()) { return Collections.emptyList(); } else { return new ArrayList(list); } } public int removeFields(final String name) { if (name == null) { return 0; } final String key = name.toLowerCase(Locale.ROOT); final List removed = fieldMap.remove(key); if (removed == null || removed.isEmpty()) { return 0; } this.fields.removeAll(removed); return removed.size(); } public void setField(final MinimalField field) { if (field == null) { return; } final String key = field.getName().toLowerCase(Locale.ROOT); final List list = fieldMap.get(key); if (list == null || list.isEmpty()) { addField(field); return; } list.clear(); list.add(field); int firstOccurrence = -1; int index = 0; for (final Iterator it = this.fields.iterator(); it.hasNext(); index++) { final MinimalField f = it.next(); if (f.getName().equalsIgnoreCase(field.getName())) { it.remove(); if (firstOccurrence == -1) { firstOccurrence = index; } } } this.fields.add(firstOccurrence, field); } @Override public Iterator iterator() { return Collections.unmodifiableList(fields).iterator(); } @Override public String toString() { return this.fields.toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy