package io.github.triglav_dataflow.jenkins.trigger.polling_triglav;

import java.util.Calendar;
import java.util.Date;

public class AlternativeExecutionTime
{
    private AlternativeExecutionTime()
    {
    }

    public static final boolean shouldBuild(Date alternativeExecutionTime, JenkinsJob jenkinsJob, TimeUnit timeUnit)
    {
        // timeUnit is hourly, daily
        // alternativeExecutionTime is set
        // alternativeExecutionTime has passed
        if (!TimeUnit.SINGULAR.equals(timeUnit) &&
            null != alternativeExecutionTime &&
            alternativeExecutionTime.before(new Date())) {
            Date lastBuildDate = jenkinsJob.getLastBuildDate();
            Calendar calendarLastBuild = null;

            if (null != lastBuildDate) {
                calendarLastBuild = Calendar.getInstance();
                calendarLastBuild.setTime(lastBuildDate);
            }

            Calendar calendarCheck = Calendar.getInstance();
            if (TimeUnit.HOURLY.equals(timeUnit)) {
                calendarCheck.set(Calendar.MINUTE, 0);
                calendarCheck.set(Calendar.SECOND, 0);
            }
            else if (TimeUnit.DAILY.equals(timeUnit)) {
                calendarCheck.set(Calendar.HOUR_OF_DAY, 0);
                calendarCheck.set(Calendar.MINUTE, 0);
                calendarCheck.set(Calendar.SECOND, 0);
            }

            // If last build was before TODAY (daily) or before CURRENT HOUR (hourly), or if there is no build
            if (null == calendarLastBuild || calendarLastBuild.before(calendarCheck)) {
                return true;
            }

            return false;
        }

        return false;
    }
}

@@ -340,47 +340,57 @@
     
-// timeUnit is hourly, daily%0A        //
+if (TimeUnit.SINGULAR.equals(timeUnit) %7C%7C null ==
  alt
@@ -414,16 +414,16 @@
 Time
- is set%0A
+) %7B%0A    
     
@@ -433,115 +433,83 @@
  // 
-alternativeExecutionTime has passed%0A        if (!TimeUnit.SINGULAR.equals(timeUnit) &&%0A
+insufficient requirements%0A            return false;%0A  
       
+%7D%0A%0A
       
-null != 
+  if (
 alte
@@ -532,11 +532,29 @@
 Time
- &&
+.after(new Date())) %7B
 %0A   
@@ -561,16 +561,19 @@
         
+ //
  alterna
@@ -593,35 +593,61 @@
 Time
-.before(new Date())) %7B%0A
+ has not passed%0A            return false;%0A    
     
+%7D%0A%0A
     
@@ -702,36 +702,32 @@
 Date();%0A        
-    
 Calendar calenda
@@ -746,36 +746,32 @@
  null;%0A%0A        
-    
 if (null != last
@@ -787,36 +787,32 @@
 ) %7B%0A            
-   
- 
 calendarLastBuil
@@ -831,36 +831,32 @@
 .getInstance();%0A
-    
             cale
@@ -893,36 +893,32 @@
 dDate);%0A        
-    
 %7D%0A%0A            C
@@ -908,28 +908,24 @@
  %7D%0A%0A        
-  
-  
 Calendar cal
@@ -953,36 +953,32 @@
 .getInstance();%0A
-    
         if (Time
@@ -1013,36 +1013,32 @@
 ) %7B%0A            
-    
 calendarCheck.se
@@ -1064,36 +1064,32 @@
 0);%0A            
-  
-  
 calendarCheck.se
@@ -1103,36 +1103,32 @@
 dar.SECOND, 0);%0A
-    
         %7D%0A      
@@ -1129,20 +1129,16 @@
         
-    
 else if 
@@ -1177,36 +1177,32 @@
 ) %7B%0A            
- 
-   
 calendarCheck.se
@@ -1233,36 +1233,32 @@
 0);%0A            
-   
- 
 calendarCheck.se
@@ -1272,36 +1272,32 @@
 dar.MINUTE, 0);%0A
-    
             cale
@@ -1335,35 +1335,27 @@
 0);%0A        
- 
-   %7D%0A%0A    
+%7D%0A%0A
         // I
@@ -1449,20 +1449,16 @@
 o build%0A
-    
         
@@ -1541,28 +1541,24 @@
             
-    
 return true;
@@ -1562,49 +1562,8 @@
 ue;%0A
-            %7D%0A%0A            return false;%0A
     

simplify AlternativeExecutionTime logic