/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.util.maps;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import org.testng.annotations.Test;

import java.util.Map;
import java.util.Set;

import static java.util.stream.IntStream.range;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotSame;
import static org.testng.Assert.assertTrue;

public class TestIdentityLinkedHashMap
{
    @Test
    public void testUsesIdentityAsEquivalenceForKeys()
    {
        String key = "foo";
        String otherKey = new String(key);
        assertEquals(key, otherKey);
        assertNotSame(key, otherKey);

        IdentityLinkedHashMap<String, Integer> map = new IdentityLinkedHashMap<>();
        assertTrue(map.isEmpty());

        int value = 1;
        int otherValue = 2;

        map.put(key, value);
        assertEquals(map, ImmutableMap.of(key, value));
        assertTrue(map.containsKey(key));
        assertFalse(map.containsKey(otherKey));
        assertTrue(map.containsValue(value));

        map.put(otherKey, otherValue);
        assertEquals(map.get(key), Integer.valueOf(value));
        assertEquals(map.get(otherKey), Integer.valueOf(otherValue));

        assertEquals(map.size(), otherValue);
        map.remove(key);
        assertEquals(map.size(), value);
        map.remove(otherKey);
        assertTrue(map.isEmpty());

        Set<String> keys = ImmutableSet.of("a", "aa");
        Map<String, Integer> expectedMap = ImmutableMap.of("a", value, "aa", otherValue);

        map.putAll(Maps.asMap(keys, String::length));
        assertEquals(expectedMap, map);

        map.clear();
        assertTrue(map.isEmpty());
    }

    @Test
    public void testStableIterationOrder()
    {
        Set<String> keys = ImmutableSet.of("All", "your", "base", "are", "belong", "to", "us");
        Map<String, Integer> expectedMap = Maps.asMap(keys, String::length);

        range(0, 10).forEach(attempt -> {
            IdentityLinkedHashMap<String, Integer> map = new IdentityLinkedHashMap<>();

            keys.forEach(i -> map.put(i, i.length()));

            assertEquals(map.keySet(), keys);
            assertEquals(map.values(), expectedMap.values());
            assertEquals(map.entrySet(), expectedMap.entrySet());
        });
    }
}

@@ -595,16 +595,64 @@
 .maps;%0A%0A
+import com.google.common.collect.ImmutableList;%0A
 import c
@@ -834,35 +834,157 @@
 til.
-Map;%0Aimport java.util.Set;%0A
+List;%0Aimport java.util.Map;%0Aimport java.util.Map.Entry;%0Aimport java.util.Set;%0A%0Aimport static com.google.common.collect.ImmutableList.toImmutableList;
 %0Aimp
@@ -2575,34 +2575,35 @@
 )%0A    %7B%0A        
-Se
+Lis
 t%3CString%3E keys =
@@ -2608,26 +2608,27 @@
  = Immutable
-Se
+Lis
 t.of(%22All%22, 
@@ -2677,36 +2677,29 @@
 %22);%0A        
-Map%3CString, 
+List%3C
 Integer%3E exp
@@ -2707,45 +2707,76 @@
 cted
-Map = Maps.asMap(keys, String::length
+Values = keys.stream().map(String::length).collect(toImmutableList()
 );%0A%0A
@@ -2983,24 +2983,45 @@
 ssertEquals(
+ImmutableList.copyOf(
 map.keySet()
@@ -3020,16 +3020,17 @@
 keySet()
+)
 , keys);
@@ -3051,24 +3051,207 @@
 ssertEquals(
+ImmutableList.copyOf(map.keySet().iterator()), keys);%0A            assertEquals(map.keySet().stream().collect(toImmutableList()), keys);%0A%0A            assertEquals(ImmutableList.copyOf(
 map.values()
@@ -3250,16 +3250,17 @@
 values()
+)
 , expect
@@ -3265,23 +3265,199 @@
 cted
-Map.values()
+Values);%0A            assertEquals(ImmutableList.copyOf(map.values().iterator()), expectedValues);%0A            assertEquals(map.values().stream().collect(toImmutableList()), expectedValues
 );%0A
+%0A
     
@@ -3481,46 +3481,343 @@
 als(
-map.entrySet(), expectedMap.entrySet()
+ImmutableList.copyOf(map.entrySet()).stream().map(Entry::getKey).collect(toImmutableList()), keys);%0A            assertEquals(ImmutableList.copyOf(map.entrySet()::iterator).stream().map(Entry::getKey).collect(toImmutableList()), keys);%0A            assertEquals(map.entrySet().stream().map(Entry::getKey).collect(toImmutableList()), keys
 );%0A 

Make testStableIterationOrder() test iteration order