001/*
002 * Copyright 2009 Red Hat, Inc.
003 * Red Hat licenses this file to you under the Apache License, version
004 * 2.0 (the "License"); you may not use this file except in compliance
005 * with the License.  You may obtain a copy of the License at
006 *    http://www.apache.org/licenses/LICENSE-2.0
007 * Unless required by applicable law or agreed to in writing, software
008 * distributed under the License is distributed on an "AS IS" BASIS,
009 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
010 * implied.  See the License for the specific language governing
011 * permissions and limitations under the License.
012 */
013
014package org.hornetq.api.core;
015
016import java.io.Serializable;
017
018/**
019 * 
020 * A Pair is a holder for 2 objects.
021 * 
022 * @author <a href="mailto:tim.fox@jboss.com">Tim Fox</a>
023 *
024 */
025public class Pair<A, B> implements Serializable
026{
027   private static final long serialVersionUID = -2496357457812368127L;
028
029   public Pair(final A a, final B b)
030   {
031      this.a = a;
032
033      this.b = b;
034   }
035
036   private A a;
037
038   private B b;
039
040   private int hash = -1;
041
042   @Override
043   public int hashCode()
044   {
045      if (hash == -1)
046      {
047         if (a == null && b == null)
048         {
049            return super.hashCode();
050         }
051         else
052         {
053            hash = (a == null ? 0 : a.hashCode()) + 37 * (b == null ? 0 : b.hashCode());
054         }
055      }
056
057      return hash;
058   }
059
060   @Override
061   public boolean equals(final Object other)
062   {
063      if (other == this)
064      {
065         return true;
066      }
067
068      if (other instanceof Pair == false)
069      {
070         return false;
071      }
072
073      Pair<A, B> pother = (Pair<A, B>)other;
074
075      return (pother.a == null ? a == null : pother.a.equals(a)) && (pother.b == null ? b == null : pother.b.equals(b));
076
077   }
078
079   @Override
080   public String toString()
081   {
082      return "Pair[a=" + a + ", b=" + b + "]";
083   }
084
085   public void setA(A a)
086   {
087      hash = -1;
088      this.a = a;
089   }
090
091   public A getA()
092   {
093      return a;
094   }
095
096   public void setB(B b)
097   {
098      hash = -1;
099      this.b = b;
100   }
101
102   public B getB()
103   {
104      return b;
105   }
106}