1 |
lpetzo |
381 |
package tools; |
2 |
|
|
|
3 |
|
|
import java.util.HashMap; |
4 |
|
|
import java.util.HashSet; |
5 |
|
|
|
6 |
|
|
import javax.swing.JComponent; |
7 |
|
|
import javax.swing.JMenuItem; |
8 |
|
|
|
9 |
|
|
/** |
10 |
|
|
* Simple data structure for the storage of two object of arbitrary types in a |
11 |
|
|
* pair. This class is inspired by the STL utility class <code>pair</code>. |
12 |
|
|
* @author Lars Petzold |
13 |
|
|
* |
14 |
|
|
* @param <F> type of the first object |
15 |
|
|
* @param <S> type of the second object |
16 |
|
|
*/ |
17 |
|
|
public class Pair<F,S> { |
18 |
|
|
|
19 |
|
|
F first; |
20 |
|
|
S second; |
21 |
|
|
|
22 |
|
|
/** |
23 |
|
|
* Create a new pair. |
24 |
|
|
* @param first the first object |
25 |
|
|
* @param second the secon object |
26 |
|
|
*/ |
27 |
|
|
public Pair(F first, S second) { |
28 |
|
|
this.first = first; |
29 |
|
|
this.second = second; |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
/** |
33 |
|
|
* Gets the F type object. |
34 |
|
|
* @return the first object |
35 |
|
|
*/ |
36 |
|
|
public F getFirst() { |
37 |
|
|
return first; |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
/** |
41 |
|
|
* Gets the S type object. |
42 |
|
|
* @return the second object |
43 |
|
|
*/ |
44 |
|
|
public S getSecond() { |
45 |
|
|
return second; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
/** |
49 |
|
|
* Sets the F type object. |
50 |
|
|
* @param first the first object |
51 |
|
|
*/ |
52 |
|
|
public void setFirst(F first) { |
53 |
|
|
this.first = first; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
/** |
57 |
|
|
* Sets the S type object. |
58 |
|
|
* @param second the second object |
59 |
|
|
*/ |
60 |
|
|
public void setSecond(S second) { |
61 |
|
|
this.second = second; |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
/** |
65 |
|
|
* Gets the string representation of the pair which is the comma-separated |
66 |
|
|
* string representation of the types F and S encapsulated in squared |
67 |
|
|
* brackets. |
68 |
|
|
* @return the Pair's string representation |
69 |
|
|
*/ |
70 |
|
|
public String toString() { |
71 |
|
|
return "[" + first.toString() + "," + second.toString() + "]"; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
public static void main(String args[]) { |
75 |
|
|
HashSet<Pair<Integer,Integer>> h1 = new HashSet<Pair<Integer,Integer>>(); |
76 |
|
|
h1.add(new Pair<Integer, Integer>(1,2)); |
77 |
|
|
h1.add(new Pair<Integer, Integer>(4,2)); |
78 |
|
|
|
79 |
|
|
JMenuItem m1 = new JMenuItem(); |
80 |
|
|
JMenuItem m2 = new JMenuItem(); |
81 |
|
|
JMenuItem m3 = new JMenuItem(); |
82 |
|
|
HashSet<Pair<JComponent, Boolean>> h2 = new HashSet<Pair<JComponent, Boolean>>(); |
83 |
|
|
h2.add(new Pair<JComponent, Boolean>(m1,true)); |
84 |
|
|
h2.add(new Pair<JComponent, Boolean>(m2,true)); |
85 |
|
|
h2.add(new Pair<JComponent, Boolean>(m3,false)); |
86 |
|
|
|
87 |
|
|
HashMap<JComponent,Boolean> h3 = new HashMap<JComponent, Boolean>(); |
88 |
|
|
h3.put(m1,true); |
89 |
|
|
h3.put(m2,true); |
90 |
|
|
h3.put(m3,false); |
91 |
|
|
|
92 |
|
|
} |
93 |
|
|
} |