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.util.Args;
33  import org.apache.http.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 {@code null}
51       * @param parameters header element parameters. May be {@code null}.
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          this.name = Args.notNull(name, "Name");
60          this.value = value;
61          if (parameters != null) {
62              this.parameters = parameters;
63          } else {
64              this.parameters = new NameValuePair[] {};
65          }
66      }
67  
68      /**
69       * Constructor with name and value.
70       *
71       * @param name header element name
72       * @param value header element value. May be {@code null}
73       */
74      public BasicHeaderElement(final String name, final String value) {
75         this(name, value, null);
76      }
77  
78      @Override
79      public String getName() {
80          return this.name;
81      }
82  
83      @Override
84      public String getValue() {
85          return this.value;
86      }
87  
88      @Override
89      public NameValuePair[] getParameters() {
90          return this.parameters.clone();
91      }
92  
93      @Override
94      public int getParameterCount() {
95          return this.parameters.length;
96      }
97  
98      @Override
99      public NameValuePair getParameter(final int index) {
100         // ArrayIndexOutOfBoundsException is appropriate
101         return this.parameters[index];
102     }
103 
104     @Override
105     public NameValuePair getParameterByName(final String name) {
106         Args.notNull(name, "Name");
107         NameValuePair found = null;
108         for (final NameValuePair current : this.parameters) {
109             if (current.getName().equalsIgnoreCase(name)) {
110                 found = current;
111                 break;
112             }
113         }
114         return found;
115     }
116 
117     @Override
118     public boolean equals(final Object object) {
119         if (this == object) {
120             return true;
121         }
122         if (object instanceof HeaderElement) {
123             final BasicHeaderElement./org/apache/http/message/BasicHeaderElement.html#BasicHeaderElement">BasicHeaderElement that = (BasicHeaderElement) object;
124             return this.name.equals(that.name)
125                 && LangUtils.equals(this.value, that.value)
126                 && LangUtils.equals(this.parameters, that.parameters);
127         }
128         return false;
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 (final NameValuePair parameter : this.parameters) {
137             hash = LangUtils.hashCode(hash, parameter);
138         }
139         return hash;
140     }
141 
142     @Override
143     public String toString() {
144         final 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 (final NameValuePair parameter : this.parameters) {
151             buffer.append("; ");
152             buffer.append(parameter);
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