001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.message;
029
030import java.io.Serializable;
031
032import org.apache.http.NameValuePair;
033import org.apache.http.annotation.ThreadingBehavior;
034import org.apache.http.annotation.Contract;
035import org.apache.http.util.Args;
036import org.apache.http.util.LangUtils;
037
038/**
039 * Basic implementation of {@link NameValuePair}.
040 *
041 * @since 4.0
042 */
043@Contract(threading = ThreadingBehavior.IMMUTABLE)
044public class BasicNameValuePair implements NameValuePair, Cloneable, Serializable {
045
046    private static final long serialVersionUID = -6437800749411518984L;
047
048    private final String name;
049    private final String value;
050
051    /**
052     * Default Constructor taking a name and a value. The value may be null.
053     *
054     * @param name The name.
055     * @param value The value.
056     */
057    public BasicNameValuePair(final String name, final String value) {
058        super();
059        this.name = Args.notNull(name, "Name");
060        this.value = value;
061    }
062
063    @Override
064    public String getName() {
065        return this.name;
066    }
067
068    @Override
069    public String getValue() {
070        return this.value;
071    }
072
073    @Override
074    public String toString() {
075        // don't call complex default formatting for a simple toString
076
077        if (this.value == null) {
078            return name;
079        }
080        final int len = this.name.length() + 1 + this.value.length();
081        final StringBuilder buffer = new StringBuilder(len);
082        buffer.append(this.name);
083        buffer.append("=");
084        buffer.append(this.value);
085        return buffer.toString();
086    }
087
088    @Override
089    public boolean equals(final Object object) {
090        if (this == object) {
091            return true;
092        }
093        if (object instanceof NameValuePair) {
094            final BasicNameValuePair that = (BasicNameValuePair) object;
095            return this.name.equals(that.name)
096                  && LangUtils.equals(this.value, that.value);
097        }
098        return false;
099    }
100
101    @Override
102    public int hashCode() {
103        int hash = LangUtils.HASH_SEED;
104        hash = LangUtils.hashCode(hash, this.name);
105        hash = LangUtils.hashCode(hash, this.value);
106        return hash;
107    }
108
109    @Override
110    public Object clone() throws CloneNotSupportedException {
111        return super.clone();
112    }
113
114}