Package clips.delegate.shedule.individual

Source Code of clips.delegate.shedule.individual.WeekIndex

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package clips.delegate.shedule.individual;

import TimeTable.Day;
import TimeTable.DayOfWeek;
import cli_fmw.delegate.utils.TimeLocal;
import cli_fmw.main.ClipsException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
*
* @author axe
*/
public class WeekIndex {

    final static Calendar point;
    static {
        point = GregorianCalendar.getInstance();
        point.set(2007, 0, 1, 0, 0, 0);
        point.set(Calendar.MILLISECOND, 0);
    }
       
    long mondayLocalUnixStamp;
    private int weekIndex;
    String cached;
   
    public WeekIndex (int weekIndex) {
        setWeekIndex(weekIndex);
    }

    /**
     *
     * @param weekIndex - Unix format Local time
     */
    public WeekIndex (Calendar time) {
        setWeekIndex(getWeekOffset(point, time));       
    }
   
    public WeekIndex (Day day) {
        GregorianCalendar now = (GregorianCalendar) GregorianCalendar.getInstance();
        now.set(day.getYear(), day.getMonth(), day.getDay(), 0, 0, 0);
        now.set(Calendar.MILLISECOND, 0);
        setWeekIndex(getWeekOffset(point, now));       
    }
   
    public WeekIndex () throws ClipsException {
        GregorianCalendar now = TimeLocal.getCurrentTime();
        setWeekIndex(getWeekOffset(point, now));
    }   
   
    /**
     *  Вычичляет относительный индекс недели
     * @param zero точка, относительно которой вычисляется индекс
     * @param point точка для которой вычисляется индекс недели
     * @return
     * @throws cli_fmw.delegate.exception.ClipsException
     */
    private int getWeekOffset(final Calendar zero, Calendar point) {
        if(point.before(point)) {
            throw new IllegalArgumentException("Errornouse date detected");
        }
        int index = 0;
        while (zero.before(point) || zero.equals(point)) {
            index++;
            point.add(Calendar.DAY_OF_YEAR, -7);       
        }
        return index-1;       
    }
    
   
    public GregorianCalendar getBegin() {
        GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
        gc.set(2007, 0, 1, 0, 0, 0);
        gc.set(Calendar.MILLISECOND, 0);
        gc.add(Calendar.WEEK_OF_YEAR, weekIndex);

        return gc;       
    }
   
    public GregorianCalendar getEnd() {
        GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
        gc.set(2007, 0, 1, 0, 0, 0);
        gc.set(Calendar.MILLISECOND, 0);
        gc.add(Calendar.WEEK_OF_YEAR, weekIndex);
        gc.add(GregorianCalendar.DAY_OF_YEAR, 7);
        gc.add(GregorianCalendar.MILLISECOND, -1);
        return gc;       
    }   
   
   
    public int getIndex() {
        return weekIndex;
    }

    public void setWeekIndex(int weekIndex) {
        GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
        gc.set(2007, 0, 1, 0, 0, 0);
        gc.set(Calendar.MILLISECOND, 0);
        gc.add(Calendar.WEEK_OF_YEAR, weekIndex);
       
        mondayLocalUnixStamp = gc.getTimeInMillis();
        this.weekIndex = weekIndex;
    }
   
    @Override
    public String toString() {
        if(cached == null) {
            GregorianCalendar first = (GregorianCalendar) GregorianCalendar.getInstance();
            first.set(2007, 0, 1, 0, 0, 0);
            first.set(Calendar.MILLISECOND, 0);
            first.add(Calendar.WEEK_OF_YEAR, weekIndex);


            GregorianCalendar last = (GregorianCalendar) first.clone();
            last.add(GregorianCalendar.DAY_OF_YEAR, 6);
            Locale locale = new Locale("ru");

            if(last.get(GregorianCalendar.YEAR) == first.get(GregorianCalendar.YEAR)) {
                cached = String.format("%d %s - %d %s %d г.",
                        first.get(GregorianCalendar.DAY_OF_MONTH),
                        first.getDisplayName(GregorianCalendar.MONTH, Calendar.SHORT, locale),
                        last.get(GregorianCalendar.DAY_OF_MONTH),
                        last.getDisplayName(GregorianCalendar.MONTH, Calendar.SHORT, locale),
                        last.get(GregorianCalendar.YEAR));
            } else {
                cached = String.format("%d %s %d г. - %d %s %d г.",
                        first.get(GregorianCalendar.DAY_OF_MONTH),
                        first.getDisplayName(GregorianCalendar.MONTH, Calendar.SHORT, locale),
                        first.get(GregorianCalendar.YEAR),
                        last.get(GregorianCalendar.DAY_OF_MONTH),
                        last.getDisplayName(GregorianCalendar.MONTH, Calendar.SHORT, locale),
                        last.get(GregorianCalendar.YEAR));
            }
        }
       
        return cached;
    }
   
    /**
     * returns day object
     * @param dayOfWeek 0-6 (Monday == 0)
     * @return
     */
    public Day getDay(DayOfWeek dayOfWeek) {
        GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
        gc.setTimeInMillis(mondayLocalUnixStamp);
        gc.add(GregorianCalendar.DAY_OF_YEAR, dayOfWeek.getEuropean());

        return new Day(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc.get(Calendar.DAY_OF_MONTH));       
    }
   
}
TOP

Related Classes of clips.delegate.shedule.individual.WeekIndex

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.