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 org.apache.http.message;
29
30 import org.apache.http.HeaderElement;
31 import org.apache.http.NameValuePair;
32 import org.apache.http.annotation.NotThreadSafe;
33 import org.apache.http.util.Args;
34 import org.apache.http.util.LangUtils;
35
36
37
38
39
40
41 @NotThreadSafe
42 public class BasicHeaderElement implements HeaderElement, Cloneable {
43
44 private final String name;
45 private final String value;
46 private final NameValuePair[] parameters;
47
48
49
50
51
52
53
54
55
56 public BasicHeaderElement(
57 final String name,
58 final String value,
59 final NameValuePair[] parameters) {
60 super();
61 this.name = Args.notNull(name, "Name");
62 this.value = value;
63 if (parameters != null) {
64 this.parameters = parameters;
65 } else {
66 this.parameters = new NameValuePair[] {};
67 }
68 }
69
70
71
72
73
74
75
76 public BasicHeaderElement(final String name, final String value) {
77 this(name, value, null);
78 }
79
80 public String getName() {
81 return this.name;
82 }
83
84 public String getValue() {
85 return this.value;
86 }
87
88 public NameValuePair[] getParameters() {
89 return this.parameters.clone();
90 }
91
92 public int getParameterCount() {
93 return this.parameters.length;
94 }
95
96 public NameValuePair getParameter(final int index) {
97
98 return this.parameters[index];
99 }
100
101 public NameValuePair getParameterByName(final String name) {
102 Args.notNull(name, "Name");
103 NameValuePair found = null;
104 for (final NameValuePair current : this.parameters) {
105 if (current.getName().equalsIgnoreCase(name)) {
106 found = current;
107 break;
108 }
109 }
110 return found;
111 }
112
113 @Override
114 public boolean equals(final Object object) {
115 if (this == object) {
116 return true;
117 }
118 if (object instanceof HeaderElement) {
119 final BasicHeaderElement that = (BasicHeaderElement) object;
120 return this.name.equals(that.name)
121 && LangUtils.equals(this.value, that.value)
122 && LangUtils.equals(this.parameters, that.parameters);
123 } else {
124 return false;
125 }
126 }
127
128 @Override
129 public int hashCode() {
130 int hash = LangUtils.HASH_SEED;
131 hash = LangUtils.hashCode(hash, this.name);
132 hash = LangUtils.hashCode(hash, this.value);
133 for (final NameValuePair parameter : this.parameters) {
134 hash = LangUtils.hashCode(hash, parameter);
135 }
136 return hash;
137 }
138
139 @Override
140 public String toString() {
141 final StringBuilder buffer = new StringBuilder();
142 buffer.append(this.name);
143 if (this.value != null) {
144 buffer.append("=");
145 buffer.append(this.value);
146 }
147 for (final NameValuePair parameter : this.parameters) {
148 buffer.append("; ");
149 buffer.append(parameter);
150 }
151 return buffer.toString();
152 }
153
154 @Override
155 public Object clone() throws CloneNotSupportedException {
156
157
158 return super.clone();
159 }
160
161 }
162