001/*
002 * Copyright 2008-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2008-2024 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2008-2024 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.util;
037
038
039
040import java.io.Serializable;
041
042
043
044/**
045 * This class provides a typed pair of objects.  It may be used whenever two
046 * objects are required but only one is allowed (e.g., returning two values from
047 * a method).
048 *
049 * @param  <F>  The type of the first object.
050 * @param  <S>  The type of the second object.
051 */
052@NotMutable()
053@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
054public final class ObjectPair<F,S>
055       implements Serializable
056{
057  /**
058   * The serial version UID for this serializable class.
059   */
060  private static final long serialVersionUID = -8610279945233778440L;
061
062
063
064  // The first object in this pair.
065  @Nullable private final F first;
066
067  // The second object in this pair.
068  @Nullable private final S second;
069
070
071
072  /**
073   * Creates a new object pair with the provided elements.
074   *
075   * @param  first   The first object in this pair.
076   * @param  second  The second object in this pair.
077   */
078  public ObjectPair(@Nullable final F first, @Nullable final S second)
079  {
080    this.first  = first;
081    this.second = second;
082  }
083
084
085
086  /**
087   * Retrieves the first object in  this pair.
088   *
089   * @return  The first object in this pair.
090   */
091  @Nullable()
092  public F getFirst()
093  {
094    return first;
095  }
096
097
098
099  /**
100   * Retrieves the second object in this pair.
101   *
102   * @return  The second object in this pair.
103   */
104  @Nullable()
105  public S getSecond()
106  {
107    return second;
108  }
109
110
111
112  /**
113   * Retrieves a hash code for this object pair.
114   *
115   * @return  A hash code for this object pair.
116   */
117  @Override()
118  public int hashCode()
119  {
120    int h = 0;
121
122    if (first != null)
123    {
124      h += first.hashCode();
125    }
126
127    if (second != null)
128    {
129      h += second.hashCode();
130    }
131
132    return h;
133  }
134
135
136
137  /**
138   * Indicates whether the provided object is equal to this object pair.
139   *
140   * @param  o  The object for which to make the determination.
141   *
142   * @return  {@code true} if the provided object is equal to this object pair,
143   *          or {@code false} if not.
144   */
145  @Override()
146  public boolean equals(@Nullable final Object o)
147  {
148    if (o == null)
149    {
150      return false;
151    }
152
153    if (o == this)
154    {
155      return true;
156    }
157
158    if (o instanceof ObjectPair)
159    {
160      final ObjectPair<?,?> p = (ObjectPair<?,?>) o;
161      if (first == null)
162      {
163        if (p.first != null)
164        {
165          return false;
166        }
167      }
168      else
169      {
170        if (! first.equals(p.first))
171        {
172          return false;
173        }
174      }
175
176      if (second == null)
177      {
178        if (p.second != null)
179        {
180          return false;
181        }
182      }
183      else
184      {
185        if (! second.equals(p.second))
186        {
187          return false;
188        }
189      }
190
191      return true;
192    }
193
194    return false;
195  }
196
197
198
199  /**
200   * Retrieves a string representation of this object pair.
201   *
202   * @return  A string representation of this object pair.
203   */
204  @Override()
205  @NotNull()
206  public String toString()
207  {
208    final StringBuilder buffer = new StringBuilder();
209    toString(buffer);
210    return buffer.toString();
211  }
212
213
214
215  /**
216   * Appends a string representation of this object pair to the provided buffer.
217   *
218   * @param  buffer  The buffer to which the information should be appended.
219   */
220  public void toString(@NotNull final StringBuilder buffer)
221  {
222    buffer.append("ObjectPair(first=");
223    buffer.append(String.valueOf(first));
224    buffer.append(", second=");
225    buffer.append(String.valueOf(second));
226    buffer.append(')');
227  }
228}