1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/message/BasicHeaderElement.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,160 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.message; 1.32 + 1.33 +import ch.boye.httpclientandroidlib.HeaderElement; 1.34 +import ch.boye.httpclientandroidlib.NameValuePair; 1.35 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer; 1.36 +import ch.boye.httpclientandroidlib.util.LangUtils; 1.37 + 1.38 +/** 1.39 + * Basic implementation of {@link HeaderElement} 1.40 + * 1.41 + * @since 4.0 1.42 + */ 1.43 +public class BasicHeaderElement implements HeaderElement, Cloneable { 1.44 + 1.45 + private final String name; 1.46 + private final String value; 1.47 + private final NameValuePair[] parameters; 1.48 + 1.49 + /** 1.50 + * Constructor with name, value and parameters. 1.51 + * 1.52 + * @param name header element name 1.53 + * @param value header element value. May be <tt>null</tt> 1.54 + * @param parameters header element parameters. May be <tt>null</tt>. 1.55 + * Parameters are copied by reference, not by value 1.56 + */ 1.57 + public BasicHeaderElement( 1.58 + final String name, 1.59 + final String value, 1.60 + final NameValuePair[] parameters) { 1.61 + super(); 1.62 + if (name == null) { 1.63 + throw new IllegalArgumentException("Name may not be null"); 1.64 + } 1.65 + this.name = name; 1.66 + this.value = value; 1.67 + if (parameters != null) { 1.68 + this.parameters = parameters; 1.69 + } else { 1.70 + this.parameters = new NameValuePair[] {}; 1.71 + } 1.72 + } 1.73 + 1.74 + /** 1.75 + * Constructor with name and value. 1.76 + * 1.77 + * @param name header element name 1.78 + * @param value header element value. May be <tt>null</tt> 1.79 + */ 1.80 + public BasicHeaderElement(final String name, final String value) { 1.81 + this(name, value, null); 1.82 + } 1.83 + 1.84 + public String getName() { 1.85 + return this.name; 1.86 + } 1.87 + 1.88 + public String getValue() { 1.89 + return this.value; 1.90 + } 1.91 + 1.92 + public NameValuePair[] getParameters() { 1.93 + return (NameValuePair[])this.parameters.clone(); 1.94 + } 1.95 + 1.96 + public int getParameterCount() { 1.97 + return this.parameters.length; 1.98 + } 1.99 + 1.100 + public NameValuePair getParameter(int index) { 1.101 + // ArrayIndexOutOfBoundsException is appropriate 1.102 + return this.parameters[index]; 1.103 + } 1.104 + 1.105 + public NameValuePair getParameterByName(final String name) { 1.106 + if (name == null) { 1.107 + throw new IllegalArgumentException("Name may not be null"); 1.108 + } 1.109 + NameValuePair found = null; 1.110 + for (int i = 0; i < this.parameters.length; i++) { 1.111 + NameValuePair current = this.parameters[ i ]; 1.112 + if (current.getName().equalsIgnoreCase(name)) { 1.113 + found = current; 1.114 + break; 1.115 + } 1.116 + } 1.117 + return found; 1.118 + } 1.119 + 1.120 + public boolean equals(final Object object) { 1.121 + if (this == object) return true; 1.122 + if (object instanceof HeaderElement) { 1.123 + BasicHeaderElement that = (BasicHeaderElement) object; 1.124 + return this.name.equals(that.name) 1.125 + && LangUtils.equals(this.value, that.value) 1.126 + && LangUtils.equals(this.parameters, that.parameters); 1.127 + } else { 1.128 + return false; 1.129 + } 1.130 + } 1.131 + 1.132 + public int hashCode() { 1.133 + int hash = LangUtils.HASH_SEED; 1.134 + hash = LangUtils.hashCode(hash, this.name); 1.135 + hash = LangUtils.hashCode(hash, this.value); 1.136 + for (int i = 0; i < this.parameters.length; i++) { 1.137 + hash = LangUtils.hashCode(hash, this.parameters[i]); 1.138 + } 1.139 + return hash; 1.140 + } 1.141 + 1.142 + public String toString() { 1.143 + CharArrayBuffer buffer = new CharArrayBuffer(64); 1.144 + buffer.append(this.name); 1.145 + if (this.value != null) { 1.146 + buffer.append("="); 1.147 + buffer.append(this.value); 1.148 + } 1.149 + for (int i = 0; i < this.parameters.length; i++) { 1.150 + buffer.append("; "); 1.151 + buffer.append(this.parameters[i]); 1.152 + } 1.153 + return buffer.toString(); 1.154 + } 1.155 + 1.156 + public Object clone() throws CloneNotSupportedException { 1.157 + // parameters array is considered immutable 1.158 + // no need to make a copy of it 1.159 + return super.clone(); 1.160 + } 1.161 + 1.162 +} 1.163 +