Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/objectstyle/ashwood/util/Pair.java


1   /* ====================================================================
2    *
3    * Copyright(c) 2003, Andriy Shapochka
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met:
9    *
10   * 1. Redistributions of source code must retain the above
11   *    copyright notice, this list of conditions and the following
12   *    disclaimer.
13   *
14   * 2. Redistributions in binary form must reproduce the above
15   *    copyright notice, this list of conditions and the following
16   *    disclaimer in the documentation and/or other materials
17   *    provided with the distribution.
18   *
19   * 3. Neither the name of the ASHWOOD nor the
20   *    names of its contributors may be used to endorse or
21   *    promote products derived from this software without
22   *    specific prior written permission.
23   *
24   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34   *
35   * ====================================================================
36   *
37   * This software consists of voluntary contributions made by
38   * individuals on behalf of the ASHWOOD Project and was originally
39   * created by Andriy Shapochka.
40   *
41   */
42  
43  package org.objectstyle.ashwood.util;
44  
45  import java.util.*;
46  import org.apache.commons.collections.*;
47  
48  public class Pair implements java.io.Serializable {
49    public static final Comparator FIRST_COMPARATOR = new FirstComparator();
50    public static final Comparator SECOND_COMPARATOR = new SecondComparator();
51  
52    public Object first;
53    public Object second;
54  
55    public Pair(Object x, Object y) {
56      first = x;
57      second = y;
58    }
59  
60    public Pair() {
61      first = null;
62      second = null;
63    }
64  
65    public Pair(Pair pair) {
66      first = pair.first;
67      second = pair.second;
68    }
69  
70    public int hashCode() {
71      int h = first == null ? 0 : first.hashCode();
72      if ( second != null )
73        h ^= second.hashCode();
74      return h;
75    }
76  
77    public String toString() {
78      return ("Pair( " + first + ", " + second + " )");
79    }
80  
81    public boolean equals(Object object) {
82      return (object instanceof Pair && equals((Pair)object));
83    }
84  
85    public boolean equals(Pair pair) {
86      if ( pair == null ) return false;
87      return (( first == null ? pair.first == null : first.equals( pair.first ) )
88              && ( second == null ? pair.second == null : second.equals( pair.second ) ));
89    }
90  
91    public Object clone() {
92      return new Pair( this );
93    }
94  
95    public static class FirstComparator implements Comparator {
96      public int compare(Object o1, Object o2) {
97        return ComparatorUtils.NATURAL_COMPARATOR.compare(((Pair)o1).first, ((Pair)o2).first);
98      }
99    }
100 
101   public static class SecondComparator implements Comparator {
102     public int compare(Object o1, Object o2) {
103       return ComparatorUtils.NATURAL_COMPARATOR.compare(((Pair)o1).second, ((Pair)o2).second);
104     }
105   }
106 }