1 /*
2 * Copyright (c) 2002-2025 Gargoyle Software Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.htmlunit.util;
16
17 import java.io.Serializable;
18 import java.util.Objects;
19
20 import org.apache.commons.lang3.builder.HashCodeBuilder;
21
22 /**
23 * A name/value pair.
24 *
25 * @author Daniel Gredler
26 * @author Nicolas Belisle
27 * @author Ronald Brill
28 * @author Michael Lueck
29 */
30 public class NameValuePair implements Serializable {
31
32 /** The name. */
33 private final String name_;
34
35 /** The value. */
36 private final String value_;
37
38 /**
39 * Creates a new instance.
40 * @param name the name
41 * @param value the value
42 */
43 public NameValuePair(final String name, final String value) {
44 name_ = name;
45 value_ = value;
46 }
47
48 /**
49 * Returns the name.
50 * @return the name
51 */
52 public String getName() {
53 return name_;
54 }
55
56 /**
57 * Returns the value.
58 * @return the value
59 */
60 public String getValue() {
61 return value_;
62 }
63
64 /**
65 * {@inheritDoc}
66 */
67 @Override
68 public boolean equals(final Object object) {
69 if (!(object instanceof NameValuePair)) {
70 return false;
71 }
72 final NameValuePair other = (NameValuePair) object;
73 return Objects.equals(name_, other.name_) && Objects.equals(value_, other.value_);
74 }
75
76 /**
77 * {@inheritDoc}
78 */
79 @Override
80 public int hashCode() {
81 return new HashCodeBuilder().
82 append(name_).
83 append(value_).
84 toHashCode();
85 }
86
87 /**
88 * {@inheritDoc}
89 */
90 @Override
91 public String toString() {
92 return name_ + "=" + value_;
93 }
94
95 /**
96 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
97 *
98 * convert null values to empty string
99 * @return a normalized copy of the {@link NameValuePair}
100 */
101 public NameValuePair normalized() {
102 return new NameValuePair(this.name_, this.value_ == null ? "" : this.value_);
103 }
104 }