package net.cobem.grizzly.storage;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DatabaseResultMonitor implements Runnable {
    public List<DatabaseResult> ResultsOpen;

    public DatabaseResultMonitor() {
        ResultsOpen = new ArrayList<DatabaseResult>();
    }

    public boolean AddTo(DatabaseResult Result) {
        ResultsOpen.add(Result);
        return true;
    }

    @Override
    public void run() {
        while (ResultsOpen.size() > 0) //If we don't have any results, we pause it.
        {
            synchronized (ResultsOpen) {
                Iterator<DatabaseResult> Iterator = ResultsOpen.iterator();

                while (Iterator.hasNext()) {
                    DatabaseResult Result = Iterator.next();

                    if (Result.RanQuery && Result.Opened) {
                        Result.close();
                        ResultsOpen.remove(Result);
                    }
                }
            }

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
            }
        }
    }

}
