View Javadoc

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 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   * Basic implementation of {@link HeaderElement}
37   *
38   * @since 4.0
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       * Constructor with name, value and parameters.
49       *
50       * @param name header element name
51       * @param value header element value. May be <tt>null</tt>
52       * @param parameters header element parameters. May be <tt>null</tt>.
53       *   Parameters are copied by reference, not by value
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       * Constructor with name and value.
74       *
75       * @param name header element name
76       * @param value header element value. May be <tt>null</tt>
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          // ArrayIndexOutOfBoundsException is appropriate
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         // parameters array is considered immutable
160         // no need to make a copy of it
161         return super.clone();
162     }
163 
164 }
165