001/*
002 * #%L
003 * Netarchivesuite - harvester
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University 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 dk.netarkivet.common.exceptions.ArgumentNotValid;
026
027/**
028 * Enumeration of the possible time units used for frequencies in schedules.
029 */
030public enum TimeUnit {
031
032    /**
033     * 'No time unit' is only included for historic reasons, since 0 did not denote a timeunit.
034     */
035    NOTIMEUNIT,
036    /**
037     * Hourly time unit will result in a frequency where only "on minute" will be set, i.e. "on hour", "on day of week"
038     * and "on day of month" are unused.
039     */
040    HOURLY,
041    /**
042     * Daily time unit will result in a frequency where only "on minute" and "on hour" will be set, i.e.
043     * "on day of week" and "on day of month" are unused.
044     */
045    DAILY,
046    /**
047     * Weekly time unit will result in a frequency where only "on minute", "on hour" and "on day of week" will be set,
048     * i.e. "on day of month" is unused.
049     */
050    WEEKLY,
051    /**
052     * Monthly time unit will result in a frequency where only "on minute", "on hour" and "on day of month" will be set,
053     * i.e. "on day of week" is unused.
054     */
055    MONTHLY,
056    /**
057     * For minute time units, it doesn't make sense to use any additional constraints: "on minute", "on hour",
058     * "on day of month" or "on day of week"
059     */
060    MINUTE;
061
062    /**
063     * Helper method that gives a proper object from e.g. a DB-stored value.
064     *
065     * @param tu a certain integer for a timeunit
066     * @return the TimeUnit related to a certain integer
067     * @throws ArgumentNotValid If argument tu is invalid (i.e. does not correspond to a TimeUnit)
068     */
069    public static TimeUnit fromOrdinal(int tu) {
070        switch (tu) {
071        case 1:
072            return HOURLY;
073        case 2:
074            return DAILY;
075        case 3:
076            return WEEKLY;
077        case 4:
078            return MONTHLY;
079        case 5:
080            return MINUTE;
081        default:
082            throw new ArgumentNotValid("Invalid time unit " + tu);
083        }
084    }
085
086}