001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.jppf.utils;
019
020import java.io.Serializable;
021
022/**
023 * Utility class holding a pair of references to two objects.
024 * @param <U> the type of the first element in the pair.
025 * @param <V> the type of the second element in the pair.
026 * @author Laurent Cohen
027 */
028public class Pair<U, V> implements Serializable {
029  /**
030   * Explicit serialVersionUID.
031   */
032  private static final long serialVersionUID = 1L;
033  /**
034   * The first object of this pair.
035   */
036  protected U first = null;
037  /**
038   * The second object of this pair.
039   */
040  protected V second = null;
041
042  /**
043   * Default constructor provided as a convenience for subclasses.
044   */
045  protected Pair() {
046  }
047
048  /**
049   * Initialize this pair with two values.
050   * @param first the first value of the new pair.
051   * @param second the second value of the new pair.
052   */
053  public Pair(final U first, final V second) {
054    this.first = first;
055    this.second = second;
056  }
057
058  /**
059   * Get the first value of this pair.
060   * @return an object of type U.
061   */
062  public U first() {
063    return first;
064  }
065
066  /**
067   * Get the second value of this pair.
068   * @return an object of type V.
069   */
070  public V second() {
071    return second;
072  }
073
074  /**
075   * Get the left-side value of this pair.
076   * @return an object of type U.
077   */
078  public U left() {
079    return first;
080  }
081
082  /**
083   * Get the right-side value of this pair.
084   * @return an object of type V.
085   */
086  public V right() {
087    return second;
088  }
089
090  @Override
091  public int hashCode() {
092    final int prime = 31;
093    int result = 1;
094    result = prime * result + ((first == null) ? 0 : first.hashCode());
095    result = prime * result + ((second == null) ? 0 : second.hashCode());
096    return result;
097  }
098
099  @Override
100  public boolean equals(final Object obj) {
101    if (this == obj) return true;
102    if (obj == null) return false;
103    if (getClass() != obj.getClass()) return false;
104    @SuppressWarnings("unchecked")
105    final Pair<U, V> other = (Pair<U, V>) obj;
106    if (first == null) {
107      if (other.first != null) return false;
108    }
109    else if (!first.equals(other.first)) return false;
110    if (second == null) return  other.second == null;
111    return second.equals(other.second);
112  }
113
114  @Override
115  public String toString() {
116    return String.format("%s[first=%s, second=%s]", getClass().getSimpleName(), first, second);
117  }
118}