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 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         } else {
128             return false;
129         }
130     }
131 
132     @Override
133     public int hashCode() {
134         int hash = LangUtils.HASH_SEED;
135         hash = LangUtils.hashCode(hash, this.name);
136         hash = LangUtils.hashCode(hash, this.value);
137         for (final NameValuePair parameter : this.parameters) {
138             hash = LangUtils.hashCode(hash, parameter);
139         }
140         return hash;
141     }
142 
143     @Override
144     public String toString() {
145         final StringBuilder buffer = new StringBuilder();
146         buffer.append(this.name);
147         if (this.value != null) {
148             buffer.append("=");
149             buffer.append(this.value);
150         }
151         for (final NameValuePair parameter : this.parameters) {
152             buffer.append("; ");
153             buffer.append(parameter);
154         }
155         return buffer.toString();
156     }
157 
158     @Override
159     public Object clone() throws CloneNotSupportedException {
160         // parameters array is considered immutable
161         // no need to make a copy of it
162         return super.clone();
163     }
164 
165 }
166