001    /*
002     * Copyright 2008-2015 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2008-2015 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    @NotMutable()
038    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
039    public final class ObjectPair<F,S>
040           implements Serializable
041    {
042      /**
043       * The serial version UID for this serializable class.
044       */
045      private static final long serialVersionUID = -8610279945233778440L;
046    
047    
048    
049      // The first object in this pair.
050      private final F first;
051    
052      // The second object in this pair.
053      private final S second;
054    
055    
056    
057      /**
058       * Creates a new object pair with the provided elements.
059       *
060       * @param  first   The first object in this pair.
061       * @param  second  The second object in this pair.
062       */
063      public ObjectPair(final F first, final S second)
064      {
065        this.first  = first;
066        this.second = second;
067      }
068    
069    
070    
071      /**
072       * Retrieves the first object in  this pair.
073       *
074       * @return  The first object in this pair.
075       */
076      public F getFirst()
077      {
078        return first;
079      }
080    
081    
082    
083      /**
084       * Retrieves the second object in this pair.
085       *
086       * @return  The second object in this pair.
087       */
088      public S getSecond()
089      {
090        return second;
091      }
092    
093    
094    
095      /**
096       * Retrieves a hash code for this object pair.
097       *
098       * @return  A hash code for this object pair.
099       */
100      @Override()
101      public int hashCode()
102      {
103        int h = 0;
104    
105        if (first != null)
106        {
107          h += first.hashCode();
108        }
109    
110        if (second != null)
111        {
112          h += second.hashCode();
113        }
114    
115        return h;
116      }
117    
118    
119    
120      /**
121       * Indicates whether the provided object is equal to this object pair.
122       *
123       * @param  o  The object for which to make the determination.
124       *
125       * @return  {@code true} if the provided object is equal to this object pair,
126       *          or {@code false} if not.
127       */
128      @Override()
129      public boolean equals(final Object o)
130      {
131        if (o == null)
132        {
133          return false;
134        }
135    
136        if (o == this)
137        {
138          return true;
139        }
140    
141        if (o instanceof ObjectPair)
142        {
143          final ObjectPair<?,?> p = (ObjectPair<?,?>) o;
144          if (first == null)
145          {
146            if (p.first != null)
147            {
148              return false;
149            }
150          }
151          else
152          {
153            if (! first.equals(p.first))
154            {
155              return false;
156            }
157          }
158    
159          if (second == null)
160          {
161            if (p.second != null)
162            {
163              return false;
164            }
165          }
166          else
167          {
168            if (! second.equals(p.second))
169            {
170              return false;
171            }
172          }
173    
174          return true;
175        }
176    
177        return false;
178      }
179    
180    
181    
182      /**
183       * Retrieves a string representation of this object pair.
184       *
185       * @return  A string representation of this object pair.
186       */
187      @Override()
188      public String toString()
189      {
190        final StringBuilder buffer = new StringBuilder();
191        toString(buffer);
192        return buffer.toString();
193      }
194    
195    
196    
197      /**
198       * Appends a string representation of this object pair to the provided buffer.
199       *
200       * @param  buffer  The buffer to which the information should be appended.
201       */
202      public void toString(final StringBuilder buffer)
203      {
204        buffer.append("ObjectPair(first=");
205        buffer.append(String.valueOf(first));
206        buffer.append(", second=");
207        buffer.append(String.valueOf(second));
208        buffer.append(')');
209      }
210    }