use of java.util.concurrent.TimeUnit in project neo4j by neo4j.
the class Format method duration.
public static String duration(long durationMillis, TimeUnit highestGranularity, TimeUnit lowestGranularity) {
StringBuilder builder = new StringBuilder();
TimeUnit[] units = TimeUnit.values();
reverse(units);
boolean use = false;
for (TimeUnit unit : units) {
if (unit.equals(highestGranularity)) {
use = true;
}
if (use) {
durationMillis = extractFromDuration(durationMillis, unit, builder);
if (unit.equals(lowestGranularity)) {
break;
}
}
}
return builder.toString();
}
use of java.util.concurrent.TimeUnit in project graphdb by neo4j-attic.
the class TimeUtil method parseTimeMillis.
public static long parseTimeMillis(String timeWithOrWithoutUnit) {
int unitIndex = -1;
for (int i = 0; i < timeWithOrWithoutUnit.length(); i++) {
char ch = timeWithOrWithoutUnit.charAt(i);
if (!Character.isDigit(ch)) {
unitIndex = i;
break;
}
}
if (unitIndex == -1) {
return DEFAULT_TIME_UNIT.toMillis(Integer.parseInt(timeWithOrWithoutUnit));
} else {
int amount = Integer.parseInt(timeWithOrWithoutUnit.substring(0, unitIndex));
String unit = timeWithOrWithoutUnit.substring(unitIndex).toLowerCase();
TimeUnit timeUnit = null;
int multiplyFactor = 1;
if (unit.equals("ms")) {
timeUnit = TimeUnit.MILLISECONDS;
} else if (unit.equals("s")) {
timeUnit = TimeUnit.SECONDS;
} else if (unit.equals("m")) {
// This is only for having to rely on 1.6
timeUnit = TimeUnit.SECONDS;
multiplyFactor = 60;
} else {
throw new RuntimeException("Unrecognized unit " + unit);
}
return timeUnit.toMillis(amount * multiplyFactor);
}
}
use of java.util.concurrent.TimeUnit in project redisson by redisson.
the class RedissonExecutorService method scheduleAsync.
@Override
public RScheduledFuture<?> scheduleAsync(Runnable task, CronSchedule cronSchedule) {
check(task);
byte[] classBody = getClassBody(task);
byte[] state = encode(task);
final Date startDate = cronSchedule.getExpression().getNextValidTimeAfter(new Date());
long startTime = startDate.getTime();
RemotePromise<Void> result = (RemotePromise<Void>) asyncScheduledServiceAtFixed.schedule(task.getClass().getName(), classBody, state, startTime, cronSchedule.getExpression().getCronExpression());
addListener(result);
return new RedissonScheduledFuture<Void>(result, startTime) {
public long getDelay(TimeUnit unit) {
return unit.convert(startDate.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}
;
};
}
use of java.util.concurrent.TimeUnit in project camel by apache.
the class CamelAnnotationsHandler method handleShutdownTimeout.
/**
* Handles updating shutdown timeouts on Camel contexts based on {@link ShutdownTimeout}.
*
* @param context the initialized Spring context
* @param testClass the test class being executed
*/
public static void handleShutdownTimeout(ConfigurableApplicationContext context, Class<?> testClass) throws Exception {
final int shutdownTimeout;
final TimeUnit shutdownTimeUnit;
if (testClass.isAnnotationPresent(ShutdownTimeout.class)) {
shutdownTimeout = testClass.getAnnotation(ShutdownTimeout.class).value();
shutdownTimeUnit = testClass.getAnnotation(ShutdownTimeout.class).timeUnit();
} else {
shutdownTimeout = 10;
shutdownTimeUnit = TimeUnit.SECONDS;
}
CamelSpringTestHelper.doToSpringCamelContexts(context, new CamelSpringTestHelper.DoToSpringCamelContextsStrategy() {
public void execute(String contextName, SpringCamelContext camelContext) throws Exception {
LOGGER.info("Setting shutdown timeout to [{} {}] on CamelContext with name [{}].", new Object[] { shutdownTimeout, shutdownTimeUnit, contextName });
camelContext.getShutdownStrategy().setTimeout(shutdownTimeout);
camelContext.getShutdownStrategy().setTimeUnit(shutdownTimeUnit);
}
});
}
use of java.util.concurrent.TimeUnit in project databus by linkedin.
the class ConfigHelper method parseDuration.
/**
* Parses a duration string of the format: duration_value [duration_unit]. duration_value must be
* a non-negative integer. Available duration_units are:
* <ul>
* <li>ns|nanos|nanosecond|nanoseconds - nanoseconds
* <li>us|micros|microsecond|microseconds - microseconds
* <li>ms|millis|millisecond|milliseconds - milliseconds
* <li>s|sec|second|seconds - seconds
* <li>min|minute|minutes - minutes
* <li>h|hr|hour|hours - hours
* <li>d|day|days - days
* </ul>
* @param durationStr the string to be parsed
* @param defaultUnit the unit to use if none is specified
* @return the duration in defaultUnit units
* @throws InvalidConfigException if the duration string does not follow the above pattern
* */
public static long parseDuration(String durationStr, TimeUnit defaultUnit) throws InvalidConfigException {
if (null == durationStr)
return 0;
TimeUnit unit = defaultUnit;
Matcher m = DURATION_PATTERN.matcher(durationStr);
if (!m.matches())
throw new InvalidConfigException("invalid duration string: " + durationStr);
if (1 < m.groupCount() && null != m.group(2) && 0 < m.group(2).length()) {
char unitChar1 = m.group(2).charAt(0);
switch(unitChar1) {
case 'n':
case 'N':
unit = TimeUnit.NANOSECONDS;
break;
case 'u':
case 'U':
unit = TimeUnit.MICROSECONDS;
break;
case 'm':
case 'M':
char unitChar3 = m.group(2).length() >= 3 ? m.group(2).charAt(2) : ' ';
unit = ('n' == unitChar3 || 'N' == unitChar3) ? TimeUnit.MINUTES : TimeUnit.MILLISECONDS;
break;
case 's':
case 'S':
unit = TimeUnit.SECONDS;
break;
case 'h':
case 'H':
unit = TimeUnit.HOURS;
break;
case 'd':
case 'D':
unit = TimeUnit.DAYS;
break;
}
}
long value = Long.parseLong(m.group(1));
return defaultUnit.convert(value, unit);
}
Aggregations