package com.firebase.geofire;

import junit.framework.Assert;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class TestListener {
    private final List<String> events = new ArrayList<>();
    private final ReentrantLock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();

    protected void addEvent(String event) {
        lock.lock();
        try {
            this.events.add(event);
            condition.signal();
        } finally {
            lock.unlock();
        }
    }

    public void expectEvents(Set<String> events) throws InterruptedException {
        boolean stillWaiting = true;
        lock.lock();
        try {
            while (!new HashSet(this.events).equals(events)) {
                if (!stillWaiting) {
                    Assert.assertEquals(events, new HashSet(this.events));
                    Assert.fail("Timeout occured");
                    return;
                }
                stillWaiting = condition.await(10, TimeUnit.SECONDS);
            }
        } finally {
            lock.unlock();
        }
    }

    public void expectEvents(List<String> events) throws InterruptedException {
        boolean stillWaiting = true;
        lock.lock();
        try {
            while (!this.events.equals(events)) {
                if (!stillWaiting) {
                    Assert.assertEquals(events, this.events);
                    Assert.fail("Timeout occured");
                    return;
                }
                stillWaiting = condition.await(10, TimeUnit.SECONDS);
            }
        } finally {
            lock.unlock();
        }
    }
}

@@ -28,40 +28,8 @@
 e;%0A%0A
-import junit.framework.Assert;%0A%0A
 impo
@@ -69,16 +69,51 @@
 va.util.
+Collection;%0Aimport java.util.Linked
 HashSet;
@@ -140,30 +140,8 @@
 st;%0A
-import java.util.Set;%0A
 impo
@@ -267,16 +267,47 @@
 antLock;
+%0Aimport junit.framework.Assert;
 %0A%0Apublic
@@ -566,32 +566,33 @@
    lock.lock();%0A
+%0A
         try %7B%0A  
@@ -601,21 +601,16 @@
         
-this.
 events.a
@@ -745,19 +745,26 @@
 tEvents(
-Set
+Collection
 %3CString%3E
@@ -852,32 +852,33 @@
    lock.lock();%0A
+%0A
         try %7B%0A  
@@ -895,31 +895,39 @@
 while (!new 
+Linked
 HashSet
+%3C%3E
 (this.events
@@ -1039,575 +1039,36 @@
 new 
-HashSet(this.events));%0A                    Assert.fail(%22Timeout occured%22);%0A                    return;%0A                %7D%0A                stillWaiting = condition.await(10, TimeUnit.SECONDS);%0A            %7D%0A        %7D finally %7B%0A            lock.unlock();%0A        %7D%0A    %7D%0A%0A    public void expectEvents(List%3CString%3E events) throws InterruptedException %7B%0A        boolean stillWaiting = true;%0A        lock.lock();%0A        try %7B%0A            while (!this.events.equals(events)) %7B%0A                if (!stillWaiting) %7B%0A                    Assert.assertEquals(events, this.events
+LinkedHashSet%3C%3E(this.events)
 );%0A 

Refactor redundant expectEvents method. (#115)