source: josm/trunk/src/org/openstreetmap/josm/tools/Pair.java@ 3486

Last change on this file since 3486 was 2910, checked in by jttt, 14 years ago

Use different hash function for Pair (simple + instead of xor). Pair is often used for nodes, that have almost the same id, which makes xor really bad choice. Makes validator Overlapping ways test much faster.

  • Property svn:eol-style set to native
File size: 934 bytes
Line 
1package org.openstreetmap.josm.tools;
2import java.util.ArrayList;
3
4/**
5 * A pair.
6 */
7public final class Pair<A,B> {
8 public A a;
9 public B b;
10
11 public Pair(A a, B b) {
12 this.a = a;
13 this.b = b;
14 }
15
16 @Override public int hashCode() {
17 return a.hashCode() + b.hashCode();
18 }
19
20 @Override public boolean equals(Object other) {
21 if (other instanceof Pair<?, ?>) {
22 Pair<?, ?> o = (Pair<?, ?>)other;
23 return a.equals(o.a) && b.equals(o.b);
24 } else
25 return false;
26 }
27
28 public static <T> ArrayList<T> toArrayList(Pair<T, T> p) {
29 ArrayList<T> l = new ArrayList<T>(2);
30 l.add(p.a);
31 l.add(p.b);
32 return l;
33 }
34
35 public static <T> Pair<T,T> sort(Pair<T,T> p) {
36 if (p.b.hashCode() < p.a.hashCode()) {
37 T tmp = p.a;
38 p.a = p.b;
39 p.b = tmp;
40 }
41 return p;
42 }
43}
Note: See TracBrowser for help on using the repository browser.