Example usage for java.util.concurrent TimeUnit toString

List of usage examples for java.util.concurrent TimeUnit toString

Introduction

In this page you can find the example usage for java.util.concurrent TimeUnit toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:Main.java

public static String toString(TimeUnit unit) {
    switch (unit) {
    case HOURS://  w  w w. j  a v a 2  s .c o m
    case DAYS:
    case MINUTES:
    case SECONDS:
        return unit.toString().substring(0, 1).toLowerCase();
    case MILLISECONDS:
        return "ms";
    case MICROSECONDS:
        return "micros";
    case NANOSECONDS:
        return "ns";
    }
    return "";
}

From source file:com.neoteric.starter.metrics.report.elastic.MetricsElasticsearchModule.java

private static String calculateRateUnit(TimeUnit unit, String name) {
    final String s = unit.toString().toLowerCase(Locale.US);
    return name + '/' + s.substring(0, s.length() - 1);
}

From source file:io.horizondb.model.core.fields.TimestampField.java

/**
 * Returns the symbol for the specified unit of time.
 * /* w ww.ja va  2  s  . c om*/
 * @param timeUnit the time unit
 * @return the symbol for the specified unit of time
 */
private static String getTimeUnitSymbol(TimeUnit timeUnit) {

    switch (timeUnit) {

    case NANOSECONDS:
        return "ns";
    case MICROSECONDS:
        return "s";
    case MILLISECONDS:
        return "ms";
    case SECONDS:
        return "s";
    default:
        return timeUnit.toString().toLowerCase();
    }
}

From source file:org.blocks4j.reconf.infra.http.layer.SimpleHttpClient.java

private static SimpleHttpResponse execute(CloseableHttpClient httpClient, SimpleHttpRequest request,
        long timeout, TimeUnit timeunit) throws Exception {

    RequestTask task = new RequestTask(httpClient, request);
    Future<SimpleHttpResponse> futureResponse = null;

    try {/*from   w  w  w  .  j a  v  a2 s .c  o m*/
        futureResponse = requestExecutor.submit(task);
        return futureResponse.get(timeout, timeunit);

    } catch (TimeoutException e) {
        httpClient.close();
        RequestLine line = request.getRequestLine();
        String method = request.getMethod();

        if (line != null && method != null) {
            throw new TimeoutException(msg.format("error.complete", method.toUpperCase(), line.getUri(),
                    timeout, timeunit.toString().toLowerCase()));

        } else {
            throw new TimeoutException(msg.format("error", timeout, timeunit.toString().toLowerCase()));
        }

    } catch (Exception e) {
        httpClient.close();
        throw e;

    } finally {
        if (futureResponse != null) {
            futureResponse.cancel(true);
        }
    }
}

From source file:com.simiacryptus.util.Util.java

/**
 * Cvt temporal unit.//from   w  ww. j  a  va2 s  . c  o m
 *
 * @param units the units
 * @return the temporal unit
 */
@javax.annotation.Nonnull
public static TemporalUnit cvt(@javax.annotation.Nonnull final TimeUnit units) {
    switch (units) {
    case DAYS:
        return ChronoUnit.DAYS;
    case HOURS:
        return ChronoUnit.HOURS;
    case MINUTES:
        return ChronoUnit.MINUTES;
    case SECONDS:
        return ChronoUnit.SECONDS;
    case NANOSECONDS:
        return ChronoUnit.NANOS;
    case MICROSECONDS:
        return ChronoUnit.MICROS;
    case MILLISECONDS:
        return ChronoUnit.MILLIS;
    default:
        throw new IllegalArgumentException(units.toString());
    }
}

From source file:io.horizondb.model.core.fields.TimestampField.java

/**
 * {@inheritDoc}//  w  w  w.j  a  v  a  2  s. c o m
 */
@Override
public Field setTimestamp(long sourceTimestamp, TimeUnit unit) {

    if (unit.compareTo(this.sourceUnit) < 0) {

        throw new TypeConversionException("A timestamp in " + unit.toString().toLowerCase()
                + " cannot be stored in a field of type: " + getType() + ".");
    }

    this.sourceTimestamp = this.sourceUnit.convert(sourceTimestamp, unit);
    return this;
}

From source file:com.linkedin.pinot.core.segment.index.SegmentMetadataImpl.java

public SegmentMetadataImpl(RealtimeSegmentZKMetadata segmentMetadata) {

    _segmentMetadataPropertiesConfiguration = new PropertiesConfiguration();

    _segmentMetadataPropertiesConfiguration.addProperty(Segment.SEGMENT_CREATOR_VERSION, null);

    _segmentMetadataPropertiesConfiguration.addProperty(Segment.SEGMENT_PADDING_CHARACTER,
            V1Constants.Str.DEFAULT_STRING_PAD_CHAR);

    _segmentMetadataPropertiesConfiguration.addProperty(V1Constants.MetadataKeys.Segment.SEGMENT_START_TIME,
            Long.toString(segmentMetadata.getStartTime()));
    _segmentMetadataPropertiesConfiguration.addProperty(V1Constants.MetadataKeys.Segment.SEGMENT_END_TIME,
            Long.toString(segmentMetadata.getEndTime()));
    _segmentMetadataPropertiesConfiguration.addProperty(V1Constants.MetadataKeys.Segment.TABLE_NAME,
            segmentMetadata.getTableName());

    final TimeUnit timeUnit = segmentMetadata.getTimeUnit();
    if (timeUnit != null) {
        _segmentMetadataPropertiesConfiguration.addProperty(V1Constants.MetadataKeys.Segment.TIME_UNIT,
                timeUnit.toString());
    } else {/*from w  w w  . j  a  va 2s.c om*/
        _segmentMetadataPropertiesConfiguration.addProperty(V1Constants.MetadataKeys.Segment.TIME_UNIT, null);
    }

    _segmentMetadataPropertiesConfiguration.addProperty(Segment.SEGMENT_TOTAL_DOCS,
            segmentMetadata.getTotalRawDocs());

    _crc = segmentMetadata.getCrc();
    _creationTime = segmentMetadata.getCreationTime();
    setTimeIntervalAndGranularity();
    _columnMetadataMap = null;
    _segmentName = segmentMetadata.getSegmentName();
    _schema = new Schema();
    _allColumns = new HashSet<String>();
    _indexDir = null;
    _metadataFile = null;
}

From source file:com.linkedin.pinot.core.segment.index.SegmentMetadataImpl.java

public SegmentMetadataImpl(OfflineSegmentZKMetadata offlineSegmentZKMetadata) {
    _segmentMetadataPropertiesConfiguration = new PropertiesConfiguration();

    _segmentMetadataPropertiesConfiguration.addProperty(Segment.SEGMENT_CREATOR_VERSION, null);

    _segmentMetadataPropertiesConfiguration.addProperty(Segment.SEGMENT_PADDING_CHARACTER,
            V1Constants.Str.DEFAULT_STRING_PAD_CHAR);

    _segmentMetadataPropertiesConfiguration.addProperty(V1Constants.MetadataKeys.Segment.SEGMENT_START_TIME,
            Long.toString(offlineSegmentZKMetadata.getStartTime()));
    _segmentMetadataPropertiesConfiguration.addProperty(V1Constants.MetadataKeys.Segment.SEGMENT_END_TIME,
            Long.toString(offlineSegmentZKMetadata.getEndTime()));
    _segmentMetadataPropertiesConfiguration.addProperty(V1Constants.MetadataKeys.Segment.TABLE_NAME,
            offlineSegmentZKMetadata.getTableName());

    final TimeUnit timeUnit = offlineSegmentZKMetadata.getTimeUnit();
    if (timeUnit != null) {
        _segmentMetadataPropertiesConfiguration.addProperty(V1Constants.MetadataKeys.Segment.TIME_UNIT,
                timeUnit.toString());
    } else {//w w w  .  j  a  va  2s  . co m
        _segmentMetadataPropertiesConfiguration.addProperty(V1Constants.MetadataKeys.Segment.TIME_UNIT, null);
    }

    _segmentMetadataPropertiesConfiguration.addProperty(Segment.SEGMENT_TOTAL_DOCS,
            offlineSegmentZKMetadata.getTotalRawDocs());

    _crc = offlineSegmentZKMetadata.getCrc();
    _creationTime = offlineSegmentZKMetadata.getCreationTime();
    _pushTime = offlineSegmentZKMetadata.getPushTime();
    _refreshTime = offlineSegmentZKMetadata.getRefreshTime();
    setTimeIntervalAndGranularity();
    _columnMetadataMap = null;
    _segmentName = offlineSegmentZKMetadata.getSegmentName();
    _schema = new Schema();
    _allColumns = new HashSet<String>();
    _indexDir = null;
    _metadataFile = null;
}

From source file:hudson.Proc.java

/**
 * Like {@link #join} but can be given a maximum time to wait.
 * @param timeout number of time units//from  w  w w . j a v a2 s  .com
 * @param unit unit of time
 * @param listener place to send messages if there are problems, incl. timeout
 * @return exit code from the process
 * @throws IOException for the same reasons as {@link #join}
 * @throws InterruptedException for the same reasons as {@link #join}
 * @since 1.363
 */
public final int joinWithTimeout(final long timeout, final TimeUnit unit, final TaskListener listener)
        throws IOException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    try {
        executor.submit(new Runnable() {
            public void run() {
                try {
                    if (!latch.await(timeout, unit)) {
                        listener.error(
                                "Timeout after " + timeout + " " + unit.toString().toLowerCase(Locale.ENGLISH));
                        kill();
                    }
                } catch (InterruptedException x) {
                    x.printStackTrace(listener.error("Failed to join a process"));
                } catch (IOException x) {
                    x.printStackTrace(listener.error("Failed to join a process"));
                } catch (RuntimeException x) {
                    x.printStackTrace(listener.error("Failed to join a process"));
                }
            }
        });
        return join();
    } finally {
        latch.countDown();
    }
}

From source file:org.apache.camel.impl.DefaultShutdownStrategy.java

protected boolean doShutdown(CamelContext context, List<RouteStartupOrder> routes, long timeout,
        TimeUnit timeUnit, boolean suspendOnly, boolean abortAfterTimeout) throws Exception {
    StopWatch watch = new StopWatch();

    // at first sort according to route startup order
    List<RouteStartupOrder> routesOrdered = new ArrayList<RouteStartupOrder>(routes);
    Collections.sort(routesOrdered, new Comparator<RouteStartupOrder>() {
        public int compare(RouteStartupOrder o1, RouteStartupOrder o2) {
            return o1.getStartupOrder() - o2.getStartupOrder();
        }//from ww  w .ja  v a  2  s . c o m
    });
    if (shutdownRoutesInReverseOrder) {
        Collections.reverse(routesOrdered);
    }

    if (timeout > 0) {
        LOG.info("Starting to graceful shutdown " + routesOrdered.size() + " routes (timeout " + timeout + " "
                + timeUnit.toString().toLowerCase() + ")");
    } else {
        LOG.info("Starting to graceful shutdown " + routesOrdered.size() + " routes (no timeout)");
    }

    // use another thread to perform the shutdowns so we can support timeout
    Future future = getExecutorService()
            .submit(new ShutdownTask(context, routesOrdered, suspendOnly, abortAfterTimeout));
    try {
        if (timeout > 0) {
            future.get(timeout, timeUnit);
        } else {
            future.get();
        }
    } catch (TimeoutException e) {
        // timeout then cancel the task
        future.cancel(true);

        // if set, stop processing and return false to indicate that the shutdown is aborting
        if (abortAfterTimeout) {
            LOG.warn("Timeout occurred. Aborting the shutdown now.");
            return false;
        } else {
            if (shutdownNowOnTimeout) {
                LOG.warn("Timeout occurred. Now forcing the routes to be shutdown now.");
                // force the routes to shutdown now
                shutdownRoutesNow(routesOrdered);
            } else {
                LOG.warn("Timeout occurred. Will ignore shutting down the remainder routes.");
            }
        }
    } catch (ExecutionException e) {
        // unwrap execution exception
        throw ObjectHelper.wrapRuntimeCamelException(e.getCause());
    }

    // convert to seconds as its easier to read than a big milli seconds number
    long seconds = TimeUnit.SECONDS.convert(watch.stop(), TimeUnit.MILLISECONDS);

    LOG.info("Graceful shutdown of " + routesOrdered.size() + " routes completed in " + seconds + " seconds");
    return true;
}