1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package com.foxinmy.weixin4j.http.apache.mime;
29
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.LinkedList;
35 import java.util.List;
36 import java.util.Locale;
37 import java.util.Map;
38
39
40
41
42 public class Header implements Iterable<MinimalField> {
43
44 private final List<MinimalField> fields;
45 private final Map<String, List<MinimalField>> fieldMap;
46
47 public Header() {
48 super();
49 this.fields = new LinkedList<MinimalField>();
50 this.fieldMap = new HashMap<String, List<MinimalField>>();
51 }
52
53 public void addField(final MinimalField field) {
54 if (field == null) {
55 return;
56 }
57 final String key = field.getName().toLowerCase(Locale.ROOT);
58 List<MinimalField> values = this.fieldMap.get(key);
59 if (values == null) {
60 values = new LinkedList<MinimalField>();
61 this.fieldMap.put(key, values);
62 }
63 values.add(field);
64 this.fields.add(field);
65 }
66
67 public List<MinimalField> getFields() {
68 return new ArrayList<MinimalField>(this.fields);
69 }
70
71 public MinimalField getField(final String name) {
72 if (name == null) {
73 return null;
74 }
75 final String key = name.toLowerCase(Locale.ROOT);
76 final List<MinimalField> list = this.fieldMap.get(key);
77 if (list != null && !list.isEmpty()) {
78 return list.get(0);
79 }
80 return null;
81 }
82
83 public List<MinimalField> getFields(final String name) {
84 if (name == null) {
85 return null;
86 }
87 final String key = name.toLowerCase(Locale.ROOT);
88 final List<MinimalField> list = this.fieldMap.get(key);
89 if (list == null || list.isEmpty()) {
90 return Collections.emptyList();
91 } else {
92 return new ArrayList<MinimalField>(list);
93 }
94 }
95
96 public int removeFields(final String name) {
97 if (name == null) {
98 return 0;
99 }
100 final String key = name.toLowerCase(Locale.ROOT);
101 final List<MinimalField> removed = fieldMap.remove(key);
102 if (removed == null || removed.isEmpty()) {
103 return 0;
104 }
105 this.fields.removeAll(removed);
106 return removed.size();
107 }
108
109 public void setField(final MinimalField field) {
110 if (field == null) {
111 return;
112 }
113 final String key = field.getName().toLowerCase(Locale.ROOT);
114 final List<MinimalField> list = fieldMap.get(key);
115 if (list == null || list.isEmpty()) {
116 addField(field);
117 return;
118 }
119 list.clear();
120 list.add(field);
121 int firstOccurrence = -1;
122 int index = 0;
123 for (final Iterator<MinimalField> it = this.fields.iterator(); it.hasNext(); index++) {
124 final MinimalField f = it.next();
125 if (f.getName().equalsIgnoreCase(field.getName())) {
126 it.remove();
127 if (firstOccurrence == -1) {
128 firstOccurrence = index;
129 }
130 }
131 }
132 this.fields.add(firstOccurrence, field);
133 }
134
135 @Override
136 public Iterator<MinimalField> iterator() {
137 return Collections.unmodifiableList(fields).iterator();
138 }
139
140 @Override
141 public String toString() {
142 return this.fields.toString();
143 }
144 }