001/*
002 * #%L
003 * Netarchivesuite - harvester
004 * %%
005 * Copyright (C) 2005 - 2018 The Royal Danish Library, 
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.harvester.datamodel;
024
025import java.util.Calendar;
026import java.util.Date;
027import java.util.GregorianCalendar;
028
029import dk.netarkivet.common.exceptions.ArgumentNotValid;
030
031/**
032 * Allows specification of a schedule with a frequency measured in minutes. This is an "anyTime" frequency, meaning that
033 * only the frequency of of the scheduling is specified, without any constraint on the actual walltime. (This is because
034 * the additional constraints don't make sense for minute frequencies. E.g. you can have a frequency "Every day at 6pm"
035 * but how would you complete "Every 17 minutes at ???"?)
036 */
037public class MinuteFrequency extends Frequency {
038
039    /**
040     * Constructor specifying the number of minutes between runs scheduled with this frequency.
041     *
042     * @param numMinutes
043     */
044    public MinuteFrequency(int numMinutes) {
045        super(numMinutes, true);
046    }
047
048    /**
049     * This method returns the time of the next event, which is just the value of lastEvent+(numMinutes)Minutes
050     *
051     * @param lastEvent A time from which the next event should be calculated.
052     * @return the time of the next event.
053     * @throws ArgumentNotValid if lastEvent is null.
054     */
055    @Override
056    public Date getNextEvent(Date lastEvent) {
057        ArgumentNotValid.checkNotNull(lastEvent, "lastEvent");
058        Calendar calendar = new GregorianCalendar();
059        calendar.setTime(getFirstEvent(lastEvent));
060        calendar.add(Calendar.MINUTE, getNumUnits());
061        return calendar.getTime();
062    }
063
064    /**
065     * As this is an "anyTime" frequency, this method just returns its argument (so long as it is not null).
066     *
067     * @param startTime The earliest time the event can happen.
068     * @return the startTime for the first event of this frequency.
069     * @throws ArgumentNotValid if startTime is null.
070     */
071    @Override
072    public Date getFirstEvent(Date startTime) {
073        ArgumentNotValid.checkNotNull(startTime, "startTime");
074        return startTime;
075    }
076
077    @Override
078    public Integer getOnMinute() {
079        return null;
080    }
081
082    @Override
083    public Integer getOnHour() {
084        return null;
085    }
086
087    @Override
088    public Integer getOnDayOfWeek() {
089        return null;
090    }
091
092    @Override
093    public Integer getOnDayOfMonth() {
094        return null;
095    }
096
097    @Override
098    public int ordinal() {
099        return TimeUnit.MINUTE.ordinal();
100    }
101
102    @Override
103    public boolean equals(Object o) {
104        if (this == o) {
105            return true;
106        }
107        if (o == null || getClass() != o.getClass()) {
108            return false;
109        }
110        if (!super.equals(o)) {
111            return false;
112        }
113
114        MinuteFrequency that = (MinuteFrequency) o;
115
116        if (getNumUnits() != that.getNumUnits()) {
117            return false;
118        }
119
120        return true;
121    }
122
123    @Override
124    public int hashCode() {
125        int result = super.hashCode();
126        result = 31 * result + getNumUnits();
127        return result;
128    }
129
130}