/*
 * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
 *
 * This library 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 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.mandarax.lib.date;


import java.util.Calendar;

import org.mandarax.kernel.Function;
import org.mandarax.kernel.Predicate;

/**
 * Class serving predicates and functions needed for date manipulations.
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.1 <9 August 2004>
 * @since 1.4
 */
public class DateArithmetic {

    // constants
    public static final String[] MONTHS = {
        "January", "February", "March", "April", "May", "June", "July",
        "August", "September", "October", "November", "December"
    };
    public static final String[] DAYS = {
    	// indexing starts with 1 !!!
        null,"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday","Saturday"
    };

    // returns the day of the week (starting with sunday = 0)
    public static class Day extends UnaryDateFunction1 {

        public String getName() {
            return "day of month";
        }
        ;

        public int compute(Calendar c) {
            return c.get (Calendar.DAY_OF_MONTH);
        }
        ;
    }
    ;

    // returns the day of the week by its (english) name (sunday, monday)
    public static class DayByName extends UnaryDateFunction2 {

        public String getName() {
            return "day by name";
        }

        public String compute(Calendar c) {
            return DAYS[c.get (Calendar.DAY_OF_WEEK)];
        }
    }
    ;

    // returns the hours (0..23)
    public static class Hours extends UnaryDateFunction1 {

        public String getName() {
            return "hour";
        }

        public int compute(Calendar c) {
            return c.get (Calendar.HOUR);
        }
    }
    ;

    // returns the minutes (0..59)
    public static class Minutes extends UnaryDateFunction1 {

        public String getName() {
            return "minute";
        }

        public int compute(Calendar c) {
            return c.get (Calendar.MINUTE);
        }
    }

    // returns the month (0..11)
    public static class Month extends UnaryDateFunction1 {

        public String getName() {
            return "month";
        }

        public int compute(Calendar c) {
            return c.get (Calendar.MONTH);
        }
    }

    // returns the month by its (english) name (january, february, ..)
    public static class MonthByName extends UnaryDateFunction2 {

        public String getName() {
            return "month by name";
        }

        public String compute(Calendar c) {
            return MONTHS[c.get (Calendar.MONTH)];
        }
    }
    ;

    // returns the seconds (0..59)
    public static class Seconds extends UnaryDateFunction1 {

        public String getName() {
            return "second";
        }

        public int compute(Calendar c) {
            return c.get (Calendar.SECOND);
        }
    }
    ;

    // returns the year (0..11)
    public static class Year extends UnaryDateFunction1 {

        public String getName() {
            return "year";
        }

        public int compute(Calendar c) {
            return c.get (Calendar.YEAR);
        }
    }
    ;

    // compares dates       
    public static class After extends BinaryDatePredicate {

        public String getName() {
            return "after";
        }

        public boolean compute(Calendar c1, Calendar c2) {
            return c1.after (c2);
        }
    }
    ;

    // compares dates
    public static class Before extends BinaryDatePredicate {

        public String getName() {
            return "before";
        }

        public boolean compute(Calendar c1, Calendar c2) {
            return c1.before (c2);
        }
    }
    ;

    // compares dates
    public static class Equals extends BinaryDatePredicate {

        public String getName() {
            return "equals (dates)";
        }

        public boolean compute(Calendar c1, Calendar c2) {
            return c1.equals (c2);
        }
    }
    ;

    // unary functions
    public static final Function DAY         = new DateArithmetic.Day ();
    public static final Function DAY_BY_NAME =
        new DateArithmetic.DayByName ();
    public static final Function HOURS         = new DateArithmetic.Hours ();
    public static final Function MINUTES       =
        new DateArithmetic.Minutes ();
    public static final Function MONTH         = new DateArithmetic.Month ();
    public static final Function MONTH_BY_NAME =
        new DateArithmetic.MonthByName ();
    public static final Function SECONDS = new DateArithmetic.Seconds ();
    public static final Function YEAR    = new DateArithmetic.Year ();

    // binary predicates
    public static final Predicate AFTER  = new DateArithmetic.After ();
    public static final Predicate BEFORE = new DateArithmetic.Before ();
    public static final Predicate EQUALS = new DateArithmetic.Equals ();

    // all functions and predicates
    public static final Function[]  ALL_FUNCTIONS  = {
        YEAR, MONTH, MONTH_BY_NAME, DAY, DAY_BY_NAME, HOURS, MINUTES, SECONDS
    };
    public static final Predicate[] ALL_PREDICATES = { EQUALS, BEFORE, AFTER };
    
}
