mobile/android/thirdparty/ch/boye/httpclientandroidlib/message/BasicHeaderElement.java

branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
equal deleted inserted replaced
-1:000000000000 0:38ed76406dfb
1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
27
28 package ch.boye.httpclientandroidlib.message;
29
30 import ch.boye.httpclientandroidlib.HeaderElement;
31 import ch.boye.httpclientandroidlib.NameValuePair;
32 import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
33 import ch.boye.httpclientandroidlib.util.LangUtils;
34
35 /**
36 * Basic implementation of {@link HeaderElement}
37 *
38 * @since 4.0
39 */
40 public class BasicHeaderElement implements HeaderElement, Cloneable {
41
42 private final String name;
43 private final String value;
44 private final NameValuePair[] parameters;
45
46 /**
47 * Constructor with name, value and parameters.
48 *
49 * @param name header element name
50 * @param value header element value. May be <tt>null</tt>
51 * @param parameters header element parameters. May be <tt>null</tt>.
52 * Parameters are copied by reference, not by value
53 */
54 public BasicHeaderElement(
55 final String name,
56 final String value,
57 final NameValuePair[] parameters) {
58 super();
59 if (name == null) {
60 throw new IllegalArgumentException("Name may not be null");
61 }
62 this.name = name;
63 this.value = value;
64 if (parameters != null) {
65 this.parameters = parameters;
66 } else {
67 this.parameters = new NameValuePair[] {};
68 }
69 }
70
71 /**
72 * Constructor with name and value.
73 *
74 * @param name header element name
75 * @param value header element value. May be <tt>null</tt>
76 */
77 public BasicHeaderElement(final String name, final String value) {
78 this(name, value, null);
79 }
80
81 public String getName() {
82 return this.name;
83 }
84
85 public String getValue() {
86 return this.value;
87 }
88
89 public NameValuePair[] getParameters() {
90 return (NameValuePair[])this.parameters.clone();
91 }
92
93 public int getParameterCount() {
94 return this.parameters.length;
95 }
96
97 public NameValuePair getParameter(int index) {
98 // ArrayIndexOutOfBoundsException is appropriate
99 return this.parameters[index];
100 }
101
102 public NameValuePair getParameterByName(final String name) {
103 if (name == null) {
104 throw new IllegalArgumentException("Name may not be null");
105 }
106 NameValuePair found = null;
107 for (int i = 0; i < this.parameters.length; i++) {
108 NameValuePair current = this.parameters[ i ];
109 if (current.getName().equalsIgnoreCase(name)) {
110 found = current;
111 break;
112 }
113 }
114 return found;
115 }
116
117 public boolean equals(final Object object) {
118 if (this == object) return true;
119 if (object instanceof HeaderElement) {
120 BasicHeaderElement that = (BasicHeaderElement) object;
121 return this.name.equals(that.name)
122 && LangUtils.equals(this.value, that.value)
123 && LangUtils.equals(this.parameters, that.parameters);
124 } else {
125 return false;
126 }
127 }
128
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 (int i = 0; i < this.parameters.length; i++) {
134 hash = LangUtils.hashCode(hash, this.parameters[i]);
135 }
136 return hash;
137 }
138
139 public String toString() {
140 CharArrayBuffer buffer = new CharArrayBuffer(64);
141 buffer.append(this.name);
142 if (this.value != null) {
143 buffer.append("=");
144 buffer.append(this.value);
145 }
146 for (int i = 0; i < this.parameters.length; i++) {
147 buffer.append("; ");
148 buffer.append(this.parameters[i]);
149 }
150 return buffer.toString();
151 }
152
153 public Object clone() throws CloneNotSupportedException {
154 // parameters array is considered immutable
155 // no need to make a copy of it
156 return super.clone();
157 }
158
159 }
160

mercurial