View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
19   */
20  
21  package org.onemind.commons.java.datastructure;
22  
23  /***
24   * Represent a map key that consists of two value
25   * @author TiongHiang Lee (thlee@onemindsoft.org)
26   */
27  public class DuoMapKey
28  {
29  
30      private final int _hashCode;
31  
32      /*** key1 **/
33      private Object key1;
34  
35      /*** key2 **/
36      private Object key2;
37  
38      /***
39       * Constructor
40       * @param key1
41       * @param key2
42       */
43      public DuoMapKey(Object key1, Object key2)
44      {
45          _hashCode = ((key1 == null) ? 0 : key1.hashCode()) + ((key2 == null) ? 0 : key2.hashCode() >> 4);
46      }
47  
48      /*** 
49       * {@inheritDoc}
50       */
51      public int hashCode()
52      {
53          return _hashCode;
54      }
55  
56      /*** 
57       * {@inheritDoc}
58       */
59      public boolean equals(Object o)
60      {
61          if (o instanceof DuoMapKey)
62          {
63              DuoMapKey other = (DuoMapKey) o;
64              return _keyEquals(key1, other.key1) && _keyEquals(key2, other.key2);
65          } else
66          {
67              return false;
68          }
69      }
70  
71      /***
72       * Return whether key and other is equals
73       * @param key the key
74       * @param other the other key
75       * @return true if both null or equals
76       */
77      private boolean _keyEquals(Object key, Object other)
78      {
79          if (key == null)
80          {
81              return other == null;
82          } else
83          {
84              return key.equals(other);
85          }
86      }
87  }