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      @Override
81      public String getName() {
82          return this.name;
83      }
84  
85      @Override
86      public String getValue() {
87          return this.value;
88      }
89  
90      @Override
91      public NameValuePair[] getParameters() {
92          return this.parameters.clone();
93      }
94  
95      @Override
96      public int getParameterCount() {
97          return this.parameters.length;
98      }
99  
100     @Override
101     public NameValuePair getParameter(final int index) {
102         // ArrayIndexOutOfBoundsException is appropriate
103         return this.parameters[index];
104     }
105 
106     @Override
107     public NameValuePair getParameterByName(final String name) {
108         Args.notNull(name, "Name");
109         NameValuePair found = null;
110         for (final NameValuePair current : this.parameters) {
111             if (current.getName().equalsIgnoreCase(name)) {
112                 found = current;
113                 break;
114             }
115         }
116         return found;
117     }
118 
119     @Override
120     public boolean equals(final Object object) {
121         if (this == object) {
122             return true;
123         }
124         if (object instanceof HeaderElement) {
125             final BasicHeaderElement that = (BasicHeaderElement) object;
126             return this.name.equals(that.name)
127                 && LangUtils.equals(this.value, that.value)
128                 && LangUtils.equals(this.parameters, that.parameters);
129         } else {
130             return false;
131         }
132     }
133 
134     @Override
135     public int hashCode() {
136         int hash = LangUtils.HASH_SEED;
137         hash = LangUtils.hashCode(hash, this.name);
138         hash = LangUtils.hashCode(hash, this.value);
139         for (final NameValuePair parameter : this.parameters) {
140             hash = LangUtils.hashCode(hash, parameter);
141         }
142         return hash;
143     }
144 
145     @Override
146     public String toString() {
147         final StringBuilder buffer = new StringBuilder();
148         buffer.append(this.name);
149         if (this.value != null) {
150             buffer.append("=");
151             buffer.append(this.value);
152         }
153         for (final NameValuePair parameter : this.parameters) {
154             buffer.append("; ");
155             buffer.append(parameter);
156         }
157         return buffer.toString();
158     }
159 
160     @Override
161     public Object clone() throws CloneNotSupportedException {
162         // parameters array is considered immutable
163         // no need to make a copy of it
164         return super.clone();
165     }
166 
167 }
168