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 */ 023 024package dk.netarkivet.harvester.datamodel; 025 026import java.util.Date; 027 028import dk.netarkivet.common.exceptions.ArgumentNotValid; 029 030/** 031 * This class implements a schedule that should run a certain number of times. 032 */ 033@SuppressWarnings({"serial"}) 034public class RepeatingSchedule extends Schedule { 035 036 /** How many times this schedule should be repeated. */ 037 private final int repeats; 038 039 /** 040 * Create a new RepeatingSchedule that runs a given number of times. 041 * 042 * @param startDate The time at which the schedule starts running. This is not necessarily the time of the first 043 * event, but no events will happen before this. May be null, meaning start any time. 044 * @param repeats how many events should happen totally. 045 * @param frequency How frequently the event should happen. 046 * @param name The unique name of the schedule. 047 * @param comments Comments entered by the user 048 * @throws ArgumentNotValid if frequency, name or comments is null, or name is "" or repeats is 0 or negative 049 */ 050 public RepeatingSchedule(Date startDate, int repeats, Frequency frequency, String name, String comments) { 051 super(startDate, frequency, name, comments); 052 ArgumentNotValid.checkPositive(repeats, "repeats"); 053 054 this.repeats = repeats; 055 } 056 057 /** 058 * Autogenerated equals. 059 * 060 * @param o The object to compare with 061 * @return Whether objects are equal 062 */ 063 public boolean equals(Object o) { 064 if (this == o) { 065 return true; 066 } 067 if (!(o instanceof RepeatingSchedule)) { 068 return false; 069 } 070 if (!super.equals(o)) { 071 return false; 072 } 073 074 final RepeatingSchedule repeatingSchedule = (RepeatingSchedule) o; 075 076 if (repeats != repeatingSchedule.repeats) { 077 return false; 078 } 079 080 return true; 081 } 082 083 /** 084 * Autogenerated hashcode method. 085 * 086 * @return the hashcode 087 */ 088 public int hashCode() { 089 int result = super.hashCode(); 090 result = 29 * result + repeats; 091 return result; 092 } 093 094 /** 095 * Return the date at which the next event will happen. 096 * 097 * @param lastEvent The time at which the previous event happened. 098 * @param numPreviousEvents How many events have previously happened. 099 * @return The date of the next event to happen or null for no more events. 100 * @throws ArgumentNotValid if numPreviousEvents is negative 101 */ 102 public Date getNextEvent(Date lastEvent, int numPreviousEvents) { 103 ArgumentNotValid.checkNotNegative(numPreviousEvents, "numPreviousEvents"); 104 105 if (lastEvent == null) { 106 return null; 107 } 108 if (numPreviousEvents >= repeats) { 109 return null; 110 } 111 return frequency.getNextEvent(lastEvent); 112 } 113 114 /** 115 * Return how many times this schedule should be triggered. 116 * 117 * @return That number of times 118 */ 119 public int getRepeats() { 120 return repeats; 121 } 122 123 /** 124 * Human readable represenation of this object. 125 * 126 * @return Human readble representation 127 */ 128 public String toString() { 129 return name + ": " + repeats + " times " + frequency + "(" + comments + ")"; 130 } 131 132}