1 /*
2 * Copyright (c) 2002-2026 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 /**
21 * A name/value pair.
22 *
23 * @author Daniel Gredler
24 * @author Nicolas Belisle
25 * @author Ronald Brill
26 * @author Michael Lueck
27 */
28 public class NameValuePair implements Serializable {
29
30 /** The name. */
31 private final String name_;
32
33 /** The value. */
34 private final String value_;
35
36 /**
37 * Creates a new instance.
38 * @param name the name
39 * @param value the value
40 */
41 public NameValuePair(final String name, final String value) {
42 name_ = name;
43 value_ = value;
44 }
45
46 /**
47 * Returns the name.
48 * @return the name
49 */
50 public String getName() {
51 return name_;
52 }
53
54 /**
55 * Returns the value.
56 * @return the value
57 */
58 public String getValue() {
59 return value_;
60 }
61
62 /**
63 * {@inheritDoc}
64 */
65 @Override
66 public boolean equals(final Object object) {
67 if (!(object instanceof NameValuePair other)) {
68 return false;
69 }
70 return Objects.equals(name_, other.name_) && Objects.equals(value_, other.value_);
71 }
72
73 /**
74 * {@inheritDoc}
75 */
76 @Override
77 public int hashCode() {
78 return Objects.hash(name_, value_);
79 }
80
81 /**
82 * {@inheritDoc}
83 */
84 @Override
85 public String toString() {
86 return name_ + "=" + value_;
87 }
88
89 /**
90 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
91 *
92 * convert null values to empty string
93 * @return a normalized copy of the {@link NameValuePair}
94 */
95 public NameValuePair normalized() {
96 return new NameValuePair(this.name_, this.value_ == null ? "" : this.value_);
97 }
98 }