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