Java TimeUnit Usage areEqualTTL(final Integer ttlDuration1, final Integer ttlDuration2)

Here you can find the source of areEqualTTL(final Integer ttlDuration1, final Integer ttlDuration2)

Description

are Equal TTL

License

Open Source License

Declaration

public static boolean areEqualTTL(final Integer ttlDuration1,
            final Integer ttlDuration2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.concurrent.TimeUnit;

public class Main {
    public static boolean areEqualTTL(final Integer ttlDuration1,
            final Integer ttlDuration2) {
        return areEqualTTL(TimeUnit.SECONDS, ttlDuration1,
                TimeUnit.SECONDS, ttlDuration2);
    }// www  .  j  a v a  2 s .  c o  m

    public static boolean areEqualTTL(final TimeUnit timeUnit1,
            final Integer ttlDuration1, final TimeUnit timeUnit2,
            final Integer ttlDuration2) {

        // If any one of them is not valid then we simply can't compare so return false
        if (!isValidTTL(timeUnit1, ttlDuration1)
                || !isValidTTL(timeUnit2, ttlDuration2)) {
            return false;
        }
        // Now both of them are valid TTL so compare seconds
        return timeUnit1.toSeconds(ttlDuration1) == timeUnit2
                .toSeconds(ttlDuration2);
    }

    public static boolean isValidTTL(final Integer ttlDurationInSeconds) {
        if (ttlDurationInSeconds == null) {
            return false;
        }
        return isValidTTL(TimeUnit.SECONDS, ttlDurationInSeconds);
    }

    public static boolean isValidTTL(final TimeUnit timeUnit,
            final Integer ttlDuration) {
        if (timeUnit == null || ttlDuration == null) {
            return false;
        }
        if (timeUnit.toSeconds(ttlDuration) <= 0) {
            return false;
        }
        if (timeUnit.toSeconds(ttlDuration) > Integer.MAX_VALUE) {
            return false;
        }
        return true;
    }
}

Related

  1. age(long creationDate)
  2. calculateDaysBetween(final Calendar start, final Calendar end)
  3. calculateRuntime(long startTime, long endTime)
  4. calculateTime(final int seconds)
  5. calculateTime(float seconds)