package com.zhuinden.simplestack;

import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * This is internal implementation detail, but I want to test it anyway.
 */
public class CollectionHelperTest {
    class PlaceholderClass {
        private String a;
        private String b;

        public PlaceholderClass(String a, String b) {
            this.a = a;
            this.b = b;
        }

        @Override
        public boolean equals(Object o) {
            if(this == o) {
                return true;
            }
            if(o == null || getClass() != o.getClass()) {
                return false;
            }

            PlaceholderClass that = (PlaceholderClass) o;

            if(a != null ? !a.equals(that.a) : that.a != null) {
                return false;
            }
            return b != null ? b.equals(that.b) : that.b == null;
        }

        @Override
        public int hashCode() {
            int result = a != null ? a.hashCode() : 0;
            result = 31 * result + (b != null ? b.hashCode() : 0);
            return result;
        }

        public String getA() {
            return a;
        }

        public String getB() {
            return b;
        }
    }

    @Test
    public void retainAllWorksAsIntended() {
        Set<PlaceholderClass> items = new LinkedHashSet<>();
        items.add(new PlaceholderClass("aaaaaaaa1", "bbbbbbb1"));
        items.add(new PlaceholderClass("aaaaaaaa2", "bbbbbbb2"));
        items.add(new PlaceholderClass("aaaaaaaa3", "bbbbbbb3"));
        items.add(new PlaceholderClass("aaaaaaaa4", "bbbbbbb4"));
        items.add(new PlaceholderClass("aaaaaaaa5", "bbbbbbb5"));

        List<PlaceholderClass> list = new ArrayList<>();
        list.add(new PlaceholderClass("aaaaaaaa1", "bbbbbbb1"));
        list.add(new PlaceholderClass("aaaaaaaa5", "bbbbbbb5"));

        CollectionHelper.retainAll(items, list);

        assertThat(items).containsExactly(new PlaceholderClass("aaaaaaaa1", "bbbbbbb1"),
                                          new PlaceholderClass("aaaaaaaa5", "bbbbbbb5"));
    }
}

@@ -1340,24 +1340,234 @@
 b;%0A        %7D
+%0A%0A        @Override%0A        public String toString() %7B%0A            return %22PlaceholderClass%7B%22 +%0A                    %22a='%22 + a + '%5C'' +%0A                    %22, b='%22 + b + '%5C'' +%0A                    '%7D';%0A        %7D
 %0A    %7D%0A%0A    
@@ -2189,32 +2189,430 @@
  %22bbbbbbb5%22));%0A%0A
+        Set%3CPlaceholderClass%3E items2 = new LinkedHashSet%3C%3E();%0A        items2.add(new PlaceholderClass(%22aaaaaaaa1%22, %22bbbbbbb1%22));%0A        items2.add(new PlaceholderClass(%22aaaaaaaa2%22, %22bbbbbbb2%22));%0A        items2.add(new PlaceholderClass(%22aaaaaaaa3%22, %22bbbbbbb3%22));%0A        items2.add(new PlaceholderClass(%22aaaaaaaa4%22, %22bbbbbbb4%22));%0A        items2.add(new PlaceholderClass(%22aaaaaaaa5%22, %22bbbbbbb5%22));%0A%0A
         Collecti
@@ -2643,16 +2643,48 @@
 , list);
+%0A        items2.retainAll(list);
 %0A%0A      
@@ -2766,16 +2766,16 @@
 bbb1%22),%0A
-
         
@@ -2852,20 +2852,262 @@
 bbbbbb5%22));%0A
+        assertThat(items2).containsExactly(new PlaceholderClass(%22aaaaaaaa1%22, %22bbbbbbb1%22),%0A                                           new PlaceholderClass(%22aaaaaaaa5%22, %22bbbbbbb5%22));%0A        assertThat(items).containsExactlyElementsOf(items2);%0A
     %7D%0A%7D%0A

Add some sanity check for retainAll() just to be sure