001/*
002 * Copyright (C) 2009 Swedish Institute of Computer Science (SICS) Copyright (C)
003 * 2009 Royal Institute of Technology (KTH)
004 *
005 * GVoD is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
018 */
019package se.sics.kompics.simulator.core.impl;
020
021import java.util.Collections;
022import java.util.LinkedList;
023import java.util.PriorityQueue;
024import se.sics.kompics.simulator.stochastic.events.StochasticSimulatorEvent;
025
026/**
027 * The <code>FutureEventList</code> class.
028 *
029 * @author Cosmin Arad {@literal <[email protected]>}
030 * @version $Id$
031 */
032public class FutureEventList {
033
034    private PriorityQueue<StochasticSimulatorEvent> futureEventList = new PriorityQueue<>();
035
036    public FutureEventList() {
037    }
038
039    void scheduleFutureEvent(long now, StochasticSimulatorEvent event) {
040        if (event.getTime() < now) {
041            throw new RuntimeException("Cannot schedule an event in the past");
042        }
043        futureEventList.add(event);
044        event.setOnList(true);
045    }
046
047    boolean cancelFutureEvent(long now, StochasticSimulatorEvent event) {
048        if (event == null) {
049            throw new RuntimeException("Cannot cancel a null event");
050        }
051
052        boolean removed = futureEventList.remove(event);
053        if (removed) {
054            event.setOnList(false);
055        }
056        return removed;
057    }
058
059    boolean hasMoreEventsAtTime(long now) {
060        StochasticSimulatorEvent event = futureEventList.peek();
061        if (event != null) {
062            return (event.getTime() == now);
063        }
064        return false;
065    }
066
067    long getFirstEventTime() {
068        return futureEventList.isEmpty() ? -1 : futureEventList.peek().getTime();
069    }
070
071    StochasticSimulatorEvent getAndRemoveFirstEvent(long now) {
072        StochasticSimulatorEvent event = futureEventList.poll();
073
074        if (event != null) {
075            event.setOnList(false);
076        }
077
078        return event;
079    }
080
081    void dumpFEL() {
082        System.err.print(". FEL(" + futureEventList.size() + "): ");
083        LinkedList<Long> times = new LinkedList<>();
084
085        for (StochasticSimulatorEvent simulatorEvent : futureEventList) {
086            times.add(simulatorEvent.getTime());
087        }
088        Collections.sort(times);
089
090        for (Long long1 : times) {
091            System.err.print(long1 + ", ");
092        }
093        System.err.println();
094        System.err.flush();
095    }
096}