001/*
002 * Copyright 2023-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2023-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) 2023-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 trio of objects.  It may be used whenever three
046 * objects are required but only one is allowed (e.g., returning three values
047 * from a method).
048 *
049 * @param  <F>  The type of the first object.
050 * @param  <S>  The type of the second object.
051 * @param  <T>  The type of the third object.
052 */
053@NotMutable()
054@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
055public final class ObjectTrio<F,S,T>
056       implements Serializable
057{
058  /**
059   * The serial version UID for this serializable class.
060   */
061  private static final long serialVersionUID = 1205337843902801247L;
062
063
064
065  // The first object in this trio.
066  @Nullable private final F first;
067
068  // The second object in this trio.
069  @Nullable private final S second;
070
071  // The third object in this trio.
072  @Nullable private final T third;
073
074
075
076  /**
077   * Creates a new object trio with the provided elements.
078   *
079   * @param  first   The first object in this trio.
080   * @param  second  The second object in this trio.
081   * @param  third   The third object in this trio.
082   */
083  public ObjectTrio(@Nullable final F first,
084                    @Nullable final S second,
085                    @Nullable final T third)
086  {
087    this.first = first;
088    this.second = second;
089    this.third = third;
090  }
091
092
093
094  /**
095   * Retrieves the first object in  this trio.
096   *
097   * @return  The first object in this trio.
098   */
099  @Nullable()
100  public F getFirst()
101  {
102    return first;
103  }
104
105
106
107  /**
108   * Retrieves the second object in this trio.
109   *
110   * @return  The second object in this trio.
111   */
112  @Nullable()
113  public S getSecond()
114  {
115    return second;
116  }
117
118
119
120  /**
121   * Retrieves the third object in this trio.
122   *
123   * @return  The third object in this trio.
124   */
125  @Nullable()
126  public T getThird()
127  {
128    return third;
129  }
130
131
132
133  /**
134   * Retrieves a hash code for this object trio.
135   *
136   * @return  A hash code for this object trio.
137   */
138  @Override()
139  public int hashCode()
140  {
141    int h = 0;
142
143    if (first != null)
144    {
145      h += first.hashCode();
146    }
147
148    if (second != null)
149    {
150      h += second.hashCode();
151    }
152
153    if (third != null)
154    {
155      h += third.hashCode();
156    }
157
158    return h;
159  }
160
161
162
163  /**
164   * Indicates whether the provided object is equal to this object trio.
165   *
166   * @param  o  The object for which to make the determination.
167   *
168   * @return  {@code true} if the provided object is equal to this object trio,
169   *          or {@code false} if not.
170   */
171  @Override()
172  public boolean equals(@Nullable final Object o)
173  {
174    if (o == null)
175    {
176      return false;
177    }
178
179    if (o == this)
180    {
181      return true;
182    }
183
184    if (o instanceof ObjectTrio)
185    {
186      final ObjectTrio<?,?,?> t = (ObjectTrio<?,?,?>) o;
187      if (first == null)
188      {
189        if (t.first != null)
190        {
191          return false;
192        }
193      }
194      else
195      {
196        if (! first.equals(t.first))
197        {
198          return false;
199        }
200      }
201
202      if (second == null)
203      {
204        if (t.second != null)
205        {
206          return false;
207        }
208      }
209      else
210      {
211        if (! second.equals(t.second))
212        {
213          return false;
214        }
215      }
216
217      if (third == null)
218      {
219        if (t.third != null)
220        {
221          return false;
222        }
223      }
224      else
225      {
226        if (! third.equals(t.third))
227        {
228          return false;
229        }
230      }
231
232      return true;
233    }
234
235    return false;
236  }
237
238
239
240  /**
241   * Retrieves a string representation of this object trio.
242   *
243   * @return  A string representation of this object trio.
244   */
245  @Override()
246  @NotNull()
247  public String toString()
248  {
249    final StringBuilder buffer = new StringBuilder();
250    toString(buffer);
251    return buffer.toString();
252  }
253
254
255
256  /**
257   * Appends a string representation of this object trio to the provided buffer.
258   *
259   * @param  buffer  The buffer to which the information should be appended.
260   */
261  public void toString(@NotNull final StringBuilder buffer)
262  {
263    buffer.append("ObjectTrio(first=");
264    buffer.append(String.valueOf(first));
265    buffer.append(", second=");
266    buffer.append(String.valueOf(second));
267    buffer.append(", third=");
268    buffer.append(String.valueOf(third));
269    buffer.append(')');
270  }
271}