/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2013 Regents of the University of Minnesota and contributors
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.grouplens.lenskit.data.dao;

import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.grouplens.lenskit.cursors.Cursor;
import org.grouplens.lenskit.cursors.Cursors;
import org.grouplens.lenskit.data.event.Event;
import org.grouplens.lenskit.util.TypeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

import static org.grouplens.lenskit.collections.CollectionUtils.fast;

/**
 * Data source backed by a collection of events.
 *
 * @author <a href="http://www.grouplens.org">GroupLens Research</a>
 * @compat Public
 */
public class EventCollectionDAO implements EventDAO {
    private static final Logger logger = LoggerFactory.getLogger(EventCollectionDAO.class);
    private Collection<? extends Event> events;
    private Set<Class<? extends Event>> types;

    /**
     * Construct a new data source from a collection of events.
     *
     * @param evts The events to use.
     * @deprecated use {@link #create(Collection)} instead
     */
    @Deprecated
    public EventCollectionDAO(Collection<? extends Event> evts) {
        logger.debug("Creating event collection DAO for {} events", evts.size());
        events = evts;

        // Scan for the types in the data source.
        types = TypeUtils.findTypes(fast(evts), Event.class);
    }

    /**
     * Create a new data source from a collection of events.
     *
     * @param evts          The events collection to be used.
     * @return              A EventCollectionDao generated from events collection.
     */
    public static EventDAO create(Collection<? extends Event> evts){
        logger.debug("Creating event collection DAO for {} events", evts.size());
        EventCollectionDAO ecDAO = new EventCollectionDAO(evts);
        return ecDAO;
    }

    /**
     * Create a new data source from a cursor of events.
     *
     * @param eventCursor   The event cursor to be used.
     * @return              A EventCollectionDao generated from events read from the cursor.
     */
    public static EventDAO fromCursor(Cursor<Event> eventCursor){
        List<Event> eventList = new ArrayList<Event>();
        while(eventCursor.hasNext()){
            eventList.add(eventCursor.next());
        }
        eventCursor.close();
        EventCollectionDAO ecDAO = new EventCollectionDAO(eventList);
        logger.debug("Creating event collection DAO for {} events", eventList.size());
        return ecDAO;
    }


    /**
     * Create a new data source from EventDAO.
     *
     * @param eventDAO      The EventDAO to be used.
     * @return              A EventCollectionDao generated from events read from the cursor.
     */
    public static EventDAO loadAndWrap(EventDAO eventDAO){
        return  fromCursor(eventDAO.streamEvents());
    }

    @Override
    public Cursor<Event> streamEvents() {
        return Cursors.wrap(events);
    }

    @Override
    public <E extends Event> Cursor<E> streamEvents(Class<E> type) {
        return streamEvents(type, SortOrder.ANY);
    }

    @Override
    public <E extends Event> Cursor<E> streamEvents(Class<E> type, SortOrder order) {
        boolean needFilter = Iterables.any(types, Predicates.not(TypeUtils.subtypePredicate(type)));

        Comparator<Event> comp = order.getEventComparator();

        if (!needFilter) {
            if (comp == null) {
                return (Cursor<E>) Cursors.wrap(events);
            } else {
                List evts = Lists.newArrayList(events);
                Collections.sort(evts, comp);
                return Cursors.wrap(evts);
            }
        } else {
            if (comp == null) {
                return Cursors.filter(streamEvents(), type);
            } else {
                List<E> filtered = Lists.newArrayList(Iterables.filter(events, type));
                Collections.sort(filtered, comp);
                return Cursors.wrap(filtered);
            }
        }
    }

}
@@ -2680,90 +2680,8 @@
 s)%7B%0A
-        logger.debug(%22Creating event collection DAO for %7B%7D events%22, evts.size());%0A
     
@@ -2733,32 +2733,32 @@
 ctionDAO(evts);%0A
+
         return e
@@ -2890,32 +2890,43 @@
 rsor to be used.
+ @WillClose
 %0A     * @return 
@@ -3089,333 +3089,86 @@
     
-List%3CEvent%3E eventList = new ArrayList%3CEvent%3E();%0A        while(eventCursor.hasNext())%7B%0A            eventList.add(eventCursor.next());%0A        %7D%0A        eventCursor.close();%0A        EventCollectionDAO ecDAO = new EventCollectionDAO(eventList);%0A        logger.debug(%22Creating event collection DAO for %7B%7D events%22, eventList.size(
+EventCollectionDAO ecDAO = new EventCollectionDAO(Cursors.makeList(eventCursor
 ));%0A

use Cursors.makeList function instead of making our own list. Added @WillClose annotation