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