All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.elephantdrummer.parser.ParserDateMaskToDate Maven / Gradle / Ivy

There is a newer version: 1.2.5
Show newest version
package com.elephantdrummer.parser;

import java.time.YearMonth;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.elephantdrummer.annotation.DayOfWeek;
import com.elephantdrummer.annotation.trigger.After;
import com.elephantdrummer.annotation.trigger.At;
import com.elephantdrummer.annotation.trigger.Before;
import com.elephantdrummer.dictionary.DictTimeParameter;
import com.elephantdrummer.validator.IValidatorDrummerJob;

/**
 * Copyright 2018 Elephant Software Klaudiusz Wojtkowiak e-mail: [email protected]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0 
 *  
 *  Unless required by applicable law or agreed to in writing, software 
 *  distributed under the License is distributed on an "AS IS" BASIS, 
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 *  See the License for the specific language governing permissions and 
 *  limitations under the License. 
 */
public interface ParserDateMaskToDate {

	

	public static Date applyToCurrentDate(At from) {
		return applyAtToDate(from,new Date());
	}
	
	
	
	
	public static Date applyAtToDate(At from,Date source) {
		if (from==null||source==null) return null;
		if (IValidatorDrummerJob.isAtNonDefault(from)==false) return source;
		IValidatorDrummerJob.isDateMaskFilledWithNonDefaultContentAndHasCorrectValues(from);
		Calendar cal=Calendar.getInstance(from.timezone().getTimeZone());
    	cal.setTime(source);

    	int firstfilled=IValidatorDrummerJob.getFirstFilletParameterForAt(from);
    	
    	if (from.year()>0)cal.set(Calendar.YEAR, from.year());
    	//Przesuniecie na miesiacu... :(
    	if (from.month()>0){
    		if (cal.get(Calendar.MONTH)>from.month()-1){
    			cal.add(Calendar.YEAR, 1);
    		}
    		cal.set(Calendar.MONTH, from.month()-1);
    	}
    	if (from.month()<0){
    		if (cal.get(Calendar.MONTH)>(12+from.month()-1)){
    			cal.add(Calendar.YEAR, 1);
    		}
    		cal.set(Calendar.MONTH, 12+from.month()-1);
    	}
    	if (from.month()==0&&firstfilled>DictTimeParameter.PARAMETER_MONTH) {
    		cal.set(Calendar.MONTH, Calendar.JANUARY);
    	}
    	if (from.day()>0){
    		if (cal.get(Calendar.DAY_OF_MONTH)>from.day()){
    			cal.add(Calendar.MONTH, 1);
    		}
    		cal.set(Calendar.DAY_OF_MONTH, from.day());
    	}
    	if (from.day()<0){
    		
    		Calendar cl=Calendar.getInstance(from.timezone().getTimeZone());
    		cl.setTime(source);
    		
    		YearMonth yearMonthObject = YearMonth.of(cl.get(Calendar.YEAR), cl.get(Calendar.MONTH)+1);
    		int montdays=yearMonthObject.lengthOfMonth();
    		int pointedday= montdays+from.day()+1;
    		if (pointedday<1) pointedday=1;
    		cal.set(Calendar.DAY_OF_MONTH,pointedday);
    	}
    	
    	if (from.day()==0 && IValidatorDrummerJob.isDayOfWeekArrayCorrect(from.dayOfWeek())==false){
    		
    		Integer dow=cal.get(Calendar.DAY_OF_WEEK);
    		
    		DayOfWeek drummerDoW=ParserDayOfWeekToCalendarDay.toDrummerDayOfWeek(dow);
    		if (IValidatorDrummerJob.isThisDayOfWeekOK(drummerDoW, from.dayOfWeek())==false){
    			int shiftindays=getNearestDayOfWeek(drummerDoW,from.dayOfWeek());
    			cal.add(Calendar.DAY_OF_YEAR,shiftindays);
    		}
    	}else {
    		if (from.day()==0 && firstfilled>DictTimeParameter.PARAMETER_DAY) {
    			cal.set(Calendar.DAY_OF_MONTH,1);
    		}
    	}
    	if (from.hour()>=0) {
    		cal.set(Calendar.HOUR_OF_DAY, from.hour());
    	}
    	if (from.hour()==-1&&firstfilled>DictTimeParameter.PARAMETER_HOUR) {
    		cal.set(Calendar.HOUR_OF_DAY, 0);
    	}
    	if (from.minute()>=0)cal.set(Calendar.MINUTE, from.minute());
    	if (from.minute()==-1&&firstfilled>DictTimeParameter.PARAMETER_MINUTE) {
    		cal.set(Calendar.MINUTE, 0);
    	}
    	if (from.second()>=0)cal.set(Calendar.SECOND, from.second());
    	if (from.second()==-1&&firstfilled>DictTimeParameter.PARAMETER_SECOND) {
    		cal.set(Calendar.SECOND, 0);
    	}
    	
    	
		return cal.getTime();
	}
	
	public static Date applyAfterToDate(After from,Date source) {
		if (from==null||source==null) return null;
		if (IValidatorDrummerJob.isAfterNotDefault(from)==false) return source;
		IValidatorDrummerJob.isDateMaskFilledWithNonDefaultContentAndHasCorrectValues(from);
		Calendar cal=Calendar.getInstance(from.timezone().getTimeZone());
    	cal.setTime(source);


    	int firstfilled=IValidatorDrummerJob.getFirstFilletParameterForAfter(from);
    	
    	if (from.year()>0)cal.set(Calendar.YEAR, from.year());
    	//Przesuniecie na miesiacu... :(
    	if (from.month()>0){
    		if (cal.get(Calendar.MONTH)>from.month()-1){
    			cal.add(Calendar.YEAR, 1);
    		}
    		cal.set(Calendar.MONTH, from.month()-1);
    	}
    	if (from.month()<0){
    		if (cal.get(Calendar.MONTH)>(12+from.month()-1)){
    			cal.add(Calendar.YEAR, 1);
    		}
    		cal.set(Calendar.MONTH, 12+from.month()-1);
    	}
    	if (from.month()==0&&firstfilled>DictTimeParameter.PARAMETER_MONTH) {
    		cal.set(Calendar.MONTH, Calendar.JANUARY);
    	}
    	if (from.day()>0){
    		if (cal.get(Calendar.DAY_OF_MONTH)>from.day()){
    			cal.add(Calendar.MONTH, 1);
    		}
    		cal.set(Calendar.DAY_OF_MONTH, from.day());
    	}
    	if (from.day()<0){
    		
    		Calendar cl=Calendar.getInstance(from.timezone().getTimeZone());
    		cl.setTime(source);
    		
    		YearMonth yearMonthObject = YearMonth.of(cl.get(Calendar.YEAR), cl.get(Calendar.MONTH)+1);
    		int montdays=yearMonthObject.lengthOfMonth();
    		int pointedday= montdays+from.day()+1;
    		if (pointedday<1) pointedday=1;
    		cal.set(Calendar.DAY_OF_MONTH,pointedday);
    	}
    	
    	if (from.day()==0 && IValidatorDrummerJob.isDayOfWeekArrayCorrect(from.dayOfWeek())==false){
    		
    		Integer dow=cal.get(Calendar.DAY_OF_WEEK);
    		
    		DayOfWeek drummerDoW=ParserDayOfWeekToCalendarDay.toDrummerDayOfWeek(dow);
    		if (IValidatorDrummerJob.isThisDayOfWeekOK(drummerDoW, from.dayOfWeek())==false){
    			int shiftindays=getNearestDayOfWeek(drummerDoW,from.dayOfWeek());
    			cal.add(Calendar.DAY_OF_YEAR,shiftindays);
    		}
    	}else {
    		if (from.day()==0 && firstfilled>DictTimeParameter.PARAMETER_DAY) {
    			cal.set(Calendar.DAY_OF_MONTH,1);
    		}
    	}
    	if (from.hour()>=0) {
    		cal.set(Calendar.HOUR_OF_DAY, from.hour());
    	}
    	if (from.hour()==-1&&firstfilled>DictTimeParameter.PARAMETER_HOUR) {
    		cal.set(Calendar.HOUR_OF_DAY, 0);
    	}
    	if (from.minute()>=0)cal.set(Calendar.MINUTE, from.minute());
    	if (from.minute()==-1&&firstfilled>DictTimeParameter.PARAMETER_MINUTE) {
    		cal.set(Calendar.MINUTE, 0);
    	}
    	if (from.second()>=0)cal.set(Calendar.SECOND, from.second());
    	if (from.second()==-1&&firstfilled>DictTimeParameter.PARAMETER_SECOND) {
    		cal.set(Calendar.SECOND, 0);
    	}
    	
    	
		return cal.getTime();
	}
	
	
	public static Date applyBeforeToDate(Before from,Date source) {
		if (from==null||source==null) return null;
		if (IValidatorDrummerJob.isDateMaskFilledWithNonDefaultContent(from)==false) return source;
		IValidatorDrummerJob.isDateMaskFilledWithNonDefaultContentAndHasCorrectValues(from);
		Calendar cal=Calendar.getInstance(from.timezone().getTimeZone());
    	cal.setTime(source);

    	int firstfilled=IValidatorDrummerJob.getFirstFilletParameterForBefore(from);
    	
    	if (from.year()>0)cal.set(Calendar.YEAR, from.year());
    	//Przesuniecie na miesiacu... :(
    	if (from.month()>0){
    		if (cal.get(Calendar.MONTH)>from.month()-1){
    			cal.add(Calendar.YEAR, 1);
    		}
    		cal.set(Calendar.MONTH, from.month()-1);
    	}
    	if (from.month()<0){
    		if (cal.get(Calendar.MONTH)>(12+from.month()-1)){
    			cal.add(Calendar.YEAR, 1);
    		}
    		cal.set(Calendar.MONTH, 12+from.month()-1);
    	}
    	if (from.month()==0&&firstfilled>DictTimeParameter.PARAMETER_MONTH) {
    		cal.set(Calendar.MONTH, Calendar.JANUARY);
    	}
    	if (from.day()>0){
    		if (cal.get(Calendar.DAY_OF_MONTH)>from.day()){
    			cal.add(Calendar.MONTH, 1);
    		}
    		cal.set(Calendar.DAY_OF_MONTH, from.day());
    	}
    	if (from.day()<0){
    		
    		Calendar cl=Calendar.getInstance(from.timezone().getTimeZone());
    		cl.setTime(source);
    		
    		YearMonth yearMonthObject = YearMonth.of(cl.get(Calendar.YEAR), cl.get(Calendar.MONTH)+1);
    		int montdays=yearMonthObject.lengthOfMonth();
    		int pointedday= montdays+from.day()+1;
    		if (pointedday<1) pointedday=1;
    		cal.set(Calendar.DAY_OF_MONTH,pointedday);
    	}
    	
    	if (from.day()==0 && IValidatorDrummerJob.isDayOfWeekArrayCorrect(from.dayOfWeek())==false){
    		
    		Integer dow=cal.get(Calendar.DAY_OF_WEEK);
    		
    		DayOfWeek drummerDoW=ParserDayOfWeekToCalendarDay.toDrummerDayOfWeek(dow);
    		if (IValidatorDrummerJob.isThisDayOfWeekOK(drummerDoW, from.dayOfWeek())==false){
    			int shiftindays=getNearestDayOfWeek(drummerDoW,from.dayOfWeek());
    			cal.add(Calendar.DAY_OF_YEAR,shiftindays);
    		}
    	}else {
    		if (from.day()==0 && firstfilled>DictTimeParameter.PARAMETER_DAY) {
    			cal.set(Calendar.DAY_OF_MONTH,1);
    		}
    	}
    	if (from.hour()>=0) {
    		cal.set(Calendar.HOUR_OF_DAY, from.hour());
    	}
    	if (from.hour()==-1&&firstfilled>DictTimeParameter.PARAMETER_HOUR) {
    		cal.set(Calendar.HOUR_OF_DAY, 0);
    	}
    	if (from.minute()>=0)cal.set(Calendar.MINUTE, from.minute());
    	if (from.minute()==-1&&firstfilled>DictTimeParameter.PARAMETER_MINUTE) {
    		cal.set(Calendar.MINUTE, 0);
    	}
    	if (from.second()>=0)cal.set(Calendar.SECOND, from.second());
    	if (from.second()==-1&&firstfilled>DictTimeParameter.PARAMETER_SECOND) {
    		cal.set(Calendar.SECOND, 0);
    	}
    	
    	
		return cal.getTime();
	}
	
	public static int getNearestDayOfWeek(DayOfWeek day,DayOfWeek[] days){
		if (day==null||days==null||days.length==0) return 0;
		List list=Arrays.asList(days);
		for (int i=0;i<7;i++){
			if (list.contains(day)){
				return i;
			}
			day=ParserDayOfWeekToCalendarDay.getNextDayOfWeek(day);
		}
		return 0;
	}
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy