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.Args;
34  import org.apache.http.util.LangUtils;
35  
36  /**
37   * Basic implementation of {@link HeaderElement}
38   *
39   * @since 4.0
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       * Constructor with name, value and parameters.
50       *
51       * @param name header element name
52       * @param value header element value. May be <tt>null</tt>
53       * @param parameters header element parameters. May be <tt>null</tt>.
54       *   Parameters are copied by reference, not by value
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       * Constructor with name and value.
72       *
73       * @param name header element name
74       * @param value header element value. May be <tt>null</tt>
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          // ArrayIndexOutOfBoundsException is appropriate
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         // parameters array is considered immutable
157         // no need to make a copy of it
158         return super.clone();
159     }
160 
161 }
162