001/*
002 * Copyright 2008-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2008-2020 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-2020 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  private final F first;
066
067  // The second object in this pair.
068  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(final F first, 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  public F getFirst()
092  {
093    return first;
094  }
095
096
097
098  /**
099   * Retrieves the second object in this pair.
100   *
101   * @return  The second object in this pair.
102   */
103  public S getSecond()
104  {
105    return second;
106  }
107
108
109
110  /**
111   * Retrieves a hash code for this object pair.
112   *
113   * @return  A hash code for this object pair.
114   */
115  @Override()
116  public int hashCode()
117  {
118    int h = 0;
119
120    if (first != null)
121    {
122      h += first.hashCode();
123    }
124
125    if (second != null)
126    {
127      h += second.hashCode();
128    }
129
130    return h;
131  }
132
133
134
135  /**
136   * Indicates whether the provided object is equal to this object pair.
137   *
138   * @param  o  The object for which to make the determination.
139   *
140   * @return  {@code true} if the provided object is equal to this object pair,
141   *          or {@code false} if not.
142   */
143  @Override()
144  public boolean equals(final Object o)
145  {
146    if (o == null)
147    {
148      return false;
149    }
150
151    if (o == this)
152    {
153      return true;
154    }
155
156    if (o instanceof ObjectPair)
157    {
158      final ObjectPair<?,?> p = (ObjectPair<?,?>) o;
159      if (first == null)
160      {
161        if (p.first != null)
162        {
163          return false;
164        }
165      }
166      else
167      {
168        if (! first.equals(p.first))
169        {
170          return false;
171        }
172      }
173
174      if (second == null)
175      {
176        if (p.second != null)
177        {
178          return false;
179        }
180      }
181      else
182      {
183        if (! second.equals(p.second))
184        {
185          return false;
186        }
187      }
188
189      return true;
190    }
191
192    return false;
193  }
194
195
196
197  /**
198   * Retrieves a string representation of this object pair.
199   *
200   * @return  A string representation of this object pair.
201   */
202  @Override()
203  public String toString()
204  {
205    final StringBuilder buffer = new StringBuilder();
206    toString(buffer);
207    return buffer.toString();
208  }
209
210
211
212  /**
213   * Appends a string representation of this object pair to the provided buffer.
214   *
215   * @param  buffer  The buffer to which the information should be appended.
216   */
217  public void toString(final StringBuilder buffer)
218  {
219    buffer.append("ObjectPair(first=");
220    buffer.append(String.valueOf(first));
221    buffer.append(", second=");
222    buffer.append(String.valueOf(second));
223    buffer.append(')');
224  }
225}