]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/content/OrderedPair.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / content / OrderedPair.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.content;
13
14 /**
15  * @author Tuukka Lehtonen
16  *
17  * @param <T> a comparable type
18  */
19 public class OrderedPair<T extends Comparable<T>> {
20
21     private final T first;
22     private final T second;
23
24     public OrderedPair(T first, T second) {
25         assert first != null;
26         assert second != null;
27
28         int comp = first.compareTo(second);
29         if (comp < 0) {
30             this.first = first;
31             this.second = second;
32         } else {
33             this.first = second;
34             this.second = first;
35         }
36     }
37
38     public T first() {
39         return first;
40     }
41
42     public T second() {
43         return second;
44     }
45
46     @Override
47     public int hashCode() {
48         return (first.hashCode() * 31) + second.hashCode();
49     }
50
51     @Override
52     public boolean equals(Object obj) {
53         if (this == obj)
54             return true;
55         if (!(obj instanceof OrderedPair<?>))
56             return false;
57         OrderedPair<?> other = (OrderedPair<?>) obj;
58         return first.equals(other.first) && second.equals(other.second);
59     }
60
61     public static <T extends Comparable<T>> OrderedPair<T> make(T ra, T rb) {
62         return new OrderedPair<T>(ra, rb);
63     }
64
65 }