1 package org.djutils.immutablecollections;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotEquals;
5 import static org.junit.Assert.assertTrue;
6
7 import java.util.Comparator;
8 import java.util.Map.Entry;
9
10 import org.djutils.immutablecollections.ImmutableMap.ImmutableEntry;
11 import org.junit.Test;
12
13
14
15
16
17
18
19
20
21
22
23 public class TestImmutableEntry
24 {
25
26
27
28
29 @Test
30 public void testImmutableEntry()
31 {
32 MyEntry<String, String> entry = new MyEntry<>("Key", "Value");
33 ImmutableEntry<String, String> ie = new ImmutableEntry<>(entry);
34 assertEquals("key must be retrievable", entry.getKey(), ie.getKey());
35 assertEquals("value must be retrievable", entry.getValue(), ie.getValue());
36 assertEquals("ie is equal to itself", ie, ie);
37 Comparator<ImmutableEntry<String, String>> keyComparator = ImmutableEntry.comparingByKey();
38 Comparator<ImmutableEntry<String, String>> valueComparator = ImmutableEntry.comparingByValue();
39 Comparator<String> reverseComparator = new Comparator<String>()
40 {
41 @Override
42 public int compare(final String o1, final String o2)
43 {
44 return o2.compareTo(o1);
45 }
46 };
47 Comparator<ImmutableEntry<String, String>> ownKeyComparator = ImmutableEntry.comparingByKey(reverseComparator);
48 Comparator<ImmutableEntry<String, String>> ownValueComparator = ImmutableEntry.comparingByValue(reverseComparator);
49 assertEquals("keyComparator returns 0 when comparing ie to itself", 0, keyComparator.compare(ie, ie));
50 assertEquals("valueComparator returns 0 when comparing ie to itself", 0, valueComparator.compare(ie, ie));
51 assertEquals("ownKeyComparator returns 0 when comparing ie to itself", 0, ownKeyComparator.compare(ie, ie));
52 assertEquals("ownValueComparator returns 0 when comparing ie to itself", 0, ownValueComparator.compare(ie, ie));
53 ImmutableEntry<String, String> ie2 = new ImmutableEntry<>(entry);
54 assertEquals("ie has same hashCode as ie2 (which wraps the same MyEntry)", ie.hashCode(), ie2.hashCode());
55 assertEquals("ie is equal to another ie embedding the same entry", ie, ie2);
56 assertNotEquals("ie is not equal to null", ie, null);
57 assertNotEquals("ie is not equal to some unrelated object", ie, "Hello");
58 ie2 = new ImmutableEntry<>(null);
59 assertNotEquals("ie is not equal to ie embedding null", ie, ie2);
60 assertNotEquals("ie embedding null is not equal to ie embedding non-null", ie2, ie);
61 MyEntry<String, String> entry2 = new MyEntry<>("Key", "DifferentValue");
62 ie2 = new ImmutableEntry<>(entry2);
63 assertNotEquals("ie is not equal to other ie embedding same key but different value", ie, ie2);
64 assertEquals("comparator returns 0 when comparing ie to other that has same key but different value", 0,
65 keyComparator.compare(ie, ie2));
66 entry2 = new MyEntry<>("Key2", "Value2");
67 ie2 = new ImmutableEntry<>(entry2);
68 System.out.println(ie + " " + ie2 + " " + keyComparator.compare(ie, ie2));
69 assertTrue("keyComparator returns < 0 when comparing objects that are in natural order",
70 keyComparator.compare(ie, ie2) < 0);
71 assertTrue("keyComparator returns > 0 when comparing objects that are in reverse natural order",
72 keyComparator.compare(ie2, ie) > 0);
73 assertTrue("ownKeyComparator returns > 0 when comparing objects that are in natural order",
74 ownKeyComparator.compare(ie, ie2) > 0);
75 assertTrue("ownKeyComparator returns < 0 when comparing objects that are in reverse natural order",
76 ownKeyComparator.compare(ie2, ie) < 0);
77 assertTrue("valueComparator returns < 0 when comparing objects that are in natural order",
78 valueComparator.compare(ie, ie2) < 0);
79 assertTrue("valueComparator returns > 0 when comparing objects that are in reverse natural order",
80 valueComparator.compare(ie2, ie) > 0);
81 assertTrue("ownValueComparator returns > 0 when comparing objects that are in natural order",
82 ownValueComparator.compare(ie, ie2) > 0);
83 assertTrue("ownValueComparator returns > 0 when comparing objects that are in reverse natural order",
84 ownValueComparator.compare(ie2, ie) < 0);
85 ie = new ImmutableEntry<>(null);
86 ie2 = new ImmutableEntry<>(null);
87 assertEquals("ie embedding null is equal to another that also embeds null", ie, ie2);
88 }
89
90
91
92
93
94
95 public static class MyEntry<K, V> implements Entry<K, V>
96 {
97
98 private final K key;
99
100
101 private V value;
102
103
104
105
106
107
108 MyEntry(final K key, final V value)
109 {
110 this.key = key;
111 this.value = value;
112 }
113
114
115 @Override
116 public final K getKey()
117 {
118 return this.key;
119 }
120
121
122 @Override
123 public final V getValue()
124 {
125 return this.value;
126 }
127
128
129 @Override
130 public final V setValue(final V newValue)
131 {
132 this.value = newValue;
133 return this.value;
134 }
135
136
137 @Override
138 public String toString()
139 {
140 return "MyEntry [key=" + this.key + ", value=" + this.value + "]";
141 }
142
143 }
144
145 }