View Javadoc
1   package org.djutils.immutablecollections;
2   
3   import java.util.Collection;
4   import java.util.LinkedHashMap;
5   import java.util.Map;
6   import java.util.Map.Entry;
7   import java.util.Set;
8   
9   import org.djutils.immutablecollections.ImmutableMap.ImmutableEntry;
10  import org.junit.Assert;
11  import org.junit.Test;
12  
13  /**
14   * TestImmutableLinkedHashMap.java.
15   * <p>
16   * Copyright (c) 2002-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
17   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
18   * distributed under a three-clause BSD-style license, which can be found at
19   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
20   * </p>
21   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank"> Alexander Verbraeck</a>
22   */
23  public class TestImmutableLinkedHashMap
24  {
25  
26      /**
27       * ...
28       */
29      @Test
30      public final void testImmutableLinkedHashMap()
31      {
32          Map<Integer, Integer> isMap = new LinkedHashMap<>();
33          for (int i = 1; i <= 10; i++)
34          {
35              isMap.put(i, 100 * i);
36          }
37          Map<Integer, Integer> map = new LinkedHashMap<Integer, Integer>(isMap);
38          testIntMap(map, new ImmutableLinkedHashMap<Integer, Integer>(map, Immutable.WRAP), Immutable.WRAP);
39          map = new LinkedHashMap<Integer, Integer>(isMap);
40          testIntMap(map, new ImmutableLinkedHashMap<Integer, Integer>(map, Immutable.COPY), Immutable.COPY);
41          map = new LinkedHashMap<Integer, Integer>(isMap);
42          testIntMap(map, new ImmutableLinkedHashMap<Integer, Integer>(map), Immutable.COPY);
43          map = new LinkedHashMap<Integer, Integer>(isMap);
44          ImmutableLinkedHashMap<Integer, Integer> ihs = new ImmutableLinkedHashMap<Integer, Integer>(map);
45          testIntMap(map, new ImmutableLinkedHashMap<Integer, Integer>(ihs), Immutable.COPY);
46          ImmutableLinkedHashMap<Integer, Integer> ilhm = new ImmutableLinkedHashMap<>(ihs, Immutable.COPY);
47          Assert.assertTrue("toString returns something descriptive", ilhm.toString().startsWith("ImmutableLinkedHashMap ["));
48      }
49  
50      /**
51       * ...
52       * @param map Map&lt;Integer, Integer&gt;; map
53       * @param imMap ImmutableMap&lt;Integer, Integer&gt;; immutable map
54       * @param copyOrWrap Immutable;
55       */
56      private void testIntMap(final Map<Integer, Integer> map, final ImmutableMap<Integer, Integer> imMap,
57              final Immutable copyOrWrap)
58      {
59          Assert.assertTrue(map.size() == 10);
60          Assert.assertTrue(imMap.size() == 10);
61          for (int i = 0; i < 10; i++)
62          {
63              Assert.assertTrue(imMap.containsKey(i + 1));
64          }
65          for (int i = 0; i < 10; i++)
66          {
67              Assert.assertTrue(imMap.containsValue(100 * (i + 1)));
68          }
69          Assert.assertFalse(imMap.isEmpty());
70          Assert.assertFalse(imMap.containsKey(15));
71          Assert.assertFalse(imMap.containsValue(1500));
72  
73          Assert.assertTrue(imMap.keySet().size() == 10);
74          Assert.assertTrue(imMap.values().size() == 10);
75  
76          Assert.assertTrue(sameContent(map.keySet(), imMap.keySet().toSet()));
77          Assert.assertTrue(sameContent(map.values(), imMap.values().toCollection()));
78          Assert.assertTrue(sameContent(map.keySet(), imMap.keySet().toSet())); // cached
79          Assert.assertTrue(sameContent(map.values(), imMap.values().toCollection()));
80  
81          Assert.assertTrue(checkEntrySets(map.entrySet(), imMap.entrySet().toSet()));
82          Assert.assertTrue(checkEntrySets(map.entrySet(), imMap.entrySet().toSet())); // cached
83  
84          if (copyOrWrap == Immutable.COPY)
85          {
86              Assert.assertTrue(imMap.isCopy());
87              Assert.assertTrue(imMap.toMap().equals(map));
88              Assert.assertFalse(imMap.toMap() == map);
89          }
90          else
91          {
92              Assert.assertTrue(imMap.isWrap());
93              Assert.assertTrue(imMap.toMap().equals(map));
94              Assert.assertFalse(imMap.toMap() == map); // this WRAP method returns a NEW list
95          }
96  
97          Map<Integer, Integer> to = imMap.toMap();
98          Assert.assertTrue(map.equals(to));
99  
100         // modify the underlying data structure
101         map.put(11, 1100);
102         if (copyOrWrap == Immutable.COPY)
103         {
104             Assert.assertTrue(imMap.size() == 10);
105         }
106         else
107         {
108             Assert.assertTrue(imMap.size() == 11);
109         }
110     }
111 
112     /**
113      * Determine if two collections contain the same objects.
114      * @param a Collection&lt;?&gt;; collection
115      * @param b Collection&lt;?&gt;; another collection
116      * @return boolean; true if the collections contain the same objects
117      */
118     private boolean sameContent(final Collection<?> a, final Collection<?> b)
119     {
120         return a.containsAll(b) && b.containsAll(a); // Oops: second half was b.containsAll(b)
121     }
122 
123     /**
124      * Determine if two entry sets contain the same entries.
125      * @param es Set&lt;Engry&gt;Integer, Integer&gt;&gt;; entry set
126      * @param ies Set&lt;ImmutableEngry&gt;Integer, Integer&gt;&gt;; immutable entry set
127      * @return boolean; true if the entry sets contain the same set of keys, each with the same values
128      */
129     private boolean checkEntrySets(final Set<Entry<Integer, Integer>> es, final Set<ImmutableEntry<Integer, Integer>> ies)
130     {
131         if (es.size() != ies.size())
132         {
133             return false;
134         }
135         for (Entry<Integer, Integer> entry : es)
136         {
137             boolean found = false;
138             for (ImmutableEntry<Integer, Integer> immEntry : ies)
139             {
140                 if (entry.getKey().equals(immEntry.getKey()) && entry.getValue().equals(immEntry.getValue()))
141                 {
142                     found = true;
143                     break;
144                 }
145             }
146             if (!found)
147             {
148                 return false;
149             }
150         }
151         return true;
152     }
153 
154 }