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

com.elephantdrummer.trigger.EveryTrigger Maven / Gradle / Ivy

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

import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;

import com.elephantdrummer.dictionary.DictPeriod;
import com.elephantdrummer.exception.DrummerException;
import com.elephantdrummer.parser.ParserDateMaskToDate;
import com.elephantdrummer.parser.ParserDateMaskToStep;
import com.elephantdrummer.trigger.base.DrumTrigger;
import com.elephantdrummer.trigger.base.TriggerType;

/**
 * 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 class EveryTrigger extends DrumTrigger{

	public EveryTrigger() {
		setStep(DictPeriod.PERIOD_MINUTE);
	}
	




	@Override
	public String getDescription() {
		return "Executes every some time period.";
	}
	
	

	 @Override
	public Date getNextRunTime( Date currDate) {

		Date retVal=null;
		try{
			retVal=  calculateForPeriod(currDate,getStep());

		}catch(DrummerException dr){
			log.log(Level.SEVERE, ""+dr.getDrummerError().toString()+" - "+dr.getMessage());
		}
		
		
		return retVal;
	}


   

		@Override
		public final TriggerType getTriggerType() {
			return TriggerType.EVERY;
		}
		
		
		

		protected Date calculateForPeriod(Date currDate,long period) {
			if (currDate==null) return null;
			
		        if (currDate.before(new Date())){
		        	currDate=new Date();
		        } 

	        
	    	Calendar cal = Calendar.getInstance();
	        Date next= toNearest(currDate,period,getShift());
	        
	        if (next.before(currDate)){
	            cal.setTime(next);
	            cal.add(Calendar.MILLISECOND,getStep().intValue());
	            next=cal.getTime();
	        }
	        
	        Date from=ParserDateMaskToDate.applyAfterToDate(getAfter(),next);
	        Date to=ParserDateMaskToDate.applyBeforeToDate(getBefore(),next);
	        
	        if (to==null==false&&to.before(next)){
	        	next=to;
	        	from=ParserDateMaskToStep.addDateStepToDate(to,next,getBefore());
	        	from=ParserDateMaskToDate.applyAfterToDate(getAfter(),from);
	        }
	        
	        if (from==null==false&& next.before(from)){
	        	next=toNearest(from,period);
	        }
	        
	        
	        Date nd=new Date();
	        if (next.before(nd)||next.equals(nd)){
	        	next=toNearest(next,period);
	        }
	        return next;
	        
		}


		  private Date toNearest(Date d,long period) {
			  long roundedtimeMs = Math.round( (double)( (double)d.getTime()/(double)(period) ) ) * period ;
			  if (roundedtimeMs




© 2015 - 2024 Weber Informatics LLC | Privacy Policy