package uk.ac.ebi.quickgo.rest.cache;

import java.time.Duration;
import java.time.LocalTime;
import java.util.function.Function;

/**
 * Holds algorithms to facilitate response caching.
 * <p>
 * Created by Tony on 04-Apr-17.
 */
public class CacheStrategy {

    public Function<LocalTime, Long> maxAgeCountDown(LocalTime s, LocalTime e) {
        return (n) -> {
            long maxAge;
            final LocalTime now = LocalTime.now();
            if (now.isAfter(s)) {
                maxAge = Duration.between(now, LocalTime.MAX).getSeconds() + Duration.between(LocalTime.MIN, e).getSeconds();
            } else {
                if (now.isBefore(e)) {
                    maxAge = Duration.between(now, e).getSeconds();
                } else {
                    maxAge = 0;
                }
            }
            assert maxAge >= 0;
            return maxAge;
        };
    }
}

@@ -32,16 +32,61 @@
 cache;%0A%0A
+import com.google.common.base.Preconditions;%0A
 import j
@@ -158,24 +158,24 @@
 unction.
-Function
+Supplier
 ;%0A%0A/**%0A 
@@ -307,56 +307,1860 @@
     
-public Function%3CLocalTime, Long%3E maxAgeCountDown
+/**%0A     * This algorithm models the following situation%0A     *%0A     * Time A - we are approaching the time of day when the data we use to populate requests is going to be%0A     * refreshed, therefore any subsequent requests to a RESTful service should have the following  header%0A     * %3Ccode%3Ecache-control: max-age=0%3C/code%3E%0A     * so User Agents, gateway caches don't cache this information (as it is likely to be out of data soon).%0A     *%0A     * Time B - we have high confidence data refresh will have taken place, and will not place until after Time A the%0A     * next day. Calculate the time left (in seconds) between a call to this method and Time A, and set the cache%0A     * control header so this data can be cached by UAs, or gateway caches for this period. The response HTTP header%0A     * will be the following%0A     * %3Ccode%3Ecache-control: max-age=36000%3C/code%3E%0A     * if there is exact ten hours left between a call to this method and Time A.%0A     *%0A     * See %3Ca href=%22https://jakearchibald.com/2016/caching-best-practices/%22%3ECaching best practices & max-age%0A     * gotchas%3C/a%3E,%0A     * %3Ca href=%22https://www.mnot.net/cache_docs/%22%3ECaching Tutorial for Web Authors and Webmasters%3C/a%3E,%0A     * %3Ca href=%22https://devcenter.heroku.com/articles/increasing-application-performance-with-http-cache-headers%22%3EIncreasing Application Performance with HTTP Cache Headers %7C Heroku Dev Center%3C/a%3E%0A     *%0A     * @param start The time of day after which a max-age time will be calculated, up until the end time, the next day%0A     * @param end The time of day after which the max-age will be reported as zero, so no more caching takes place%0A     * for all subsequent requests.%0A     * @return a String that holds the number of seconds left before the cut-of time after which requests will not be%0A     * cached.%0A     */%0A    public static Supplier%3CString%3E maxAgeTimeLeft
 (Loc
@@ -2167,16 +2167,20 @@
 alTime s
+tart
 , LocalT
@@ -2184,28 +2184,31 @@
 alTime e
+nd
 ) %7B%0A
+%0A
         
 return (
@@ -2203,17 +2203,74 @@
     
+P
 re
+conditions.checkArgument(start.isAfter(end));%0A%0A        re
 turn (
-n
 ) -%3E
@@ -2377,16 +2377,20 @@
 sAfter(s
+tart
 )) %7B%0A   
@@ -2496,16 +2496,18 @@
 e.MIN, e
+nd
 ).getSec
@@ -2569,16 +2569,18 @@
 Before(e
+nd
 )) %7B%0A   
@@ -2628,16 +2628,18 @@
 n(now, e
+nd
 ).getSec
@@ -2786,22 +2786,37 @@
  return 
+Long.toString(
 maxAge
+)
 ;%0A      

Add some javadoc. Return algorithm as Supplier instead of Function. Have the supplier return a String rather than a long. Make variable names clearer.