src.main.java.com.mgnt.utils.TimeUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of MgntUtils Show documentation
Show all versions of MgntUtils Show documentation
Set of various Utils: stacktrace noise filter, String to/from unicode sequence converter, simple Http client JSON parser/serializer,
Silent String parsing to Integer and other numeric types, Parsing String to time intervals with support for time unit suffixes,
JSON parser that provides serialization/deserialization of classes to JSON, Version comparator and Version ranges operations,
Self-throttling binary reader from Http request, File reader, A utility that automatically initiates a Factory with instances of all
classes that implement user provided interface. An infrastructure for writing Scheduled Task classes where time
interval for task execution is provided in humanly readable format (such as "9h" for 9 hours)
package com.mgnt.utils;
import com.mgnt.utils.entities.TimeInterval;
import java.util.concurrent.TimeUnit;
/**
* This is simple Utility class that has a convenience sleep method that "swallows"
* Created by Michael Gantman on 22/12/2015.
*/
public class TimeUtils {
/**
* This method is a convenience method for sleep that "swallows" {@link InterruptedException} and
* has {@link TimeUnit} parameter in addition to time period so it makes it very convenient. So with
* this method there is no need to convert the time into milliseconds. Just simply write
*
* {@code sleepFor(10, TimeUnit.SECONDS);}
*
No Exception catching needed
* @param period time period to sleep
* @param timeUnit {@link TimeUnit} that specifies in which units the time period is measured
*/
public static void sleepFor(long period, TimeUnit timeUnit) {
try {
timeUnit.sleep(period);
} catch (InterruptedException ie) {
}
}
/**
* This is an overload for method {@link TimeUtils#sleepFor(long, TimeUnit)} which is provided for convenience.
* Method {@link TimeUtils#sleepFor(long, TimeUnit)} could be very readable if you use actual values, for example
* {@code sleepFor(10, TimeUnit.SECONDS);}
*
* But it you have your values stored in variables or especially already in instance of {@link TimeInterval}, then
* it doesn't make it any more readable to write
*
* {@code TimeUtils.sleepFor(timeInterval.getValue(), timeInterval.getTimeUnit());}
*
* Hence this convenience method is provided
* @param timeInterval {@link TimeInterval} instance that holds the time period to be waited for
*/
public static void sleepFor(TimeInterval timeInterval) {
sleepFor(timeInterval.getValue(), timeInterval.getTimeUnit());
}
}