Example usage for org.joda.time Duration getMillis

List of usage examples for org.joda.time Duration getMillis

Introduction

In this page you can find the example usage for org.joda.time Duration getMillis.

Prototype

public long getMillis() 

Source Link

Document

Gets the length of this duration in milliseconds.

Usage

From source file:com.vaushell.superpipes.tools.HTTPhelper.java

License:Open Source License

/**
 * Is a URI exist (HTTP response code between 200 and 299).
 *
 * @param client HTTP client.//w w  w.  j a  v a 2 s  . c  o  m
 * @param uri the URI.
 * @param timeout how many ms to wait ? (could be null)
 * @return true if it exists.
 * @throws IOException
 */
public static boolean isURIvalid(final CloseableHttpClient client, final URI uri, final Duration timeout)
        throws IOException {
    if (client == null || uri == null) {
        throw new IllegalArgumentException();
    }

    HttpEntity responseEntity = null;
    try {
        // Exec request
        final HttpGet get = new HttpGet(uri);

        if (timeout != null && timeout.getMillis() > 0L) {
            get.setConfig(RequestConfig.custom().setConnectTimeout((int) timeout.getMillis())
                    .setConnectionRequestTimeout((int) timeout.getMillis())
                    .setSocketTimeout((int) timeout.getMillis()).build());
        }

        try (final CloseableHttpResponse response = client.execute(get)) {
            final StatusLine sl = response.getStatusLine();
            responseEntity = response.getEntity();

            return sl.getStatusCode() >= 200 && sl.getStatusCode() < 300;
        }
    } finally {
        if (responseEntity != null) {
            EntityUtils.consumeQuietly(responseEntity);
        }
    }
}

From source file:com.vaushell.superpipes.tools.retry.A_Retry.java

License:Open Source License

/**
 * How long should I wait between 2 checks ? (in milliseconds).
 *
 * @param waitTime Time to wait/*  w w w.  jav a  2  s .c  o  m*/
 * @return this element.
 */
public A_Retry<T> setWaitTime(final Duration waitTime) {
    if (waitTime == null || waitTime.getMillis() < 0L) {
        throw new IllegalArgumentException();
    }

    this.waitTime = waitTime;

    return this;
}

From source file:com.vaushell.superpipes.tools.retry.A_Retry.java

License:Open Source License

/**
 * How long shoud I retry ? (in milliseconds, 0=disabled). Doesn't interrupt code execution.
 *
 * @param maxDuration Maximum duration./*  ww w.j ava 2 s  .c  o  m*/
 * @return this element.
 */
public A_Retry<T> setMaxDuration(final Duration maxDuration) {
    if (maxDuration == null || maxDuration.getMillis() < 0L) {
        throw new IllegalArgumentException();
    }

    this.maxDuration = maxDuration;

    return this;
}

From source file:com.webarch.common.datetime.DateTimeUtils.java

License:Apache License

/**
 * ?/*from  w w w .ja v  a2  s  . com*/
 *
 * @param startTime
 * @param endTime
 * @return
 */
public static long getPeriodMillis(Date startTime, Date endTime) {
    Duration duration = new Duration(new DateTime(startTime), new DateTime(endTime));
    return duration.getMillis();
}

From source file:controllers.api.DashboardsApiController.java

License:Open Source License

protected List<Map<String, Object>> formatWidgetValueResults(final int maxDataPoints, final Object resultValue,
        final String functionType, final String interval, final Map<String, Object> timeRange,
        final boolean allQuery) {
    final ImmutableList.Builder<Map<String, Object>> pointListBuilder = ImmutableList.builder();

    if (resultValue instanceof Map) {
        final Map<?, ?> resultMap = (Map) resultValue;

        DateTime from;//from w w  w .  ja v  a 2s .c  o m
        if (allQuery) {
            String firstTimestamp = (String) resultMap.entrySet().iterator().next().getKey();
            from = new DateTime(Long.parseLong(firstTimestamp) * 1000, DateTimeZone.UTC);
        } else {
            from = DateTime.parse((String) timeRange.get("from")).withZone(DateTimeZone.UTC);
        }
        final DateTime to = DateTime.parse((String) timeRange.get("to"));
        final MutableDateTime currentTime = new MutableDateTime(from);

        final Duration step = estimateIntervalStep(interval);
        final int dataPoints = (int) ((to.getMillis() - from.getMillis()) / step.getMillis());

        // using the absolute value guarantees, that there will always be enough values for the given resolution
        final int factor = (maxDataPoints != -1 && dataPoints > maxDataPoints) ? dataPoints / maxDataPoints : 1;

        int index = 0;
        floorToBeginningOfInterval(interval, currentTime);
        while (currentTime.isBefore(to) || currentTime.isEqual(to)) {
            if (index % factor == 0) {
                String timestamp = Long.toString(currentTime.getMillis() / 1000);
                Object value = resultMap.get(timestamp);
                if (functionType != null && value != null) {
                    value = ((Map) value).get(functionType);
                }
                Object result = value == null ? 0 : value;
                final Map<String, Object> point = ImmutableMap.of("x", Long.parseLong(timestamp), "y", result);
                pointListBuilder.add(point);
            }
            index++;
            nextStep(interval, currentTime);
        }
    }
    return pointListBuilder.build();
}

From source file:controllers.api.SearchApiController.java

License:Open Source License

/**
 * Create a list with histogram results that would be serialized to JSON like this
 * <p/>//w  ww  .  j ava 2  s .c  o  m
 * [{ x: -1893456000, y: 92228531 }, { x: -1577923200, y: 106021568 }]
 */
protected List<Map<String, Long>> formatHistogramResults(DateHistogramResult histogram, int maxDataPoints,
        boolean allQuery) {
    final List<Map<String, Long>> points = Lists.newArrayList();
    final Map<String, Long> histogramResults = histogram.getResults();

    DateTime from;
    if (allQuery) {
        String firstTimestamp = histogramResults.entrySet().iterator().next().getKey();
        from = new DateTime(Long.parseLong(firstTimestamp) * 1000, DateTimeZone.UTC);
    } else {
        from = DateTime.parse(histogram.getHistogramBoundaries().getFrom());
    }
    final DateTime to = DateTime.parse(histogram.getHistogramBoundaries().getTo());
    final MutableDateTime currentTime = new MutableDateTime(from);

    final Duration step = estimateIntervalStep(histogram.getInterval());
    final int dataPoints = (int) ((to.getMillis() - from.getMillis()) / step.getMillis());

    // using the absolute value guarantees, that there will always be enough values for the given resolution
    final int factor = (maxDataPoints != -1 && dataPoints > maxDataPoints) ? dataPoints / maxDataPoints : 1;

    int index = 0;
    floorToBeginningOfInterval(histogram.getInterval(), currentTime);
    while (currentTime.isBefore(to) || currentTime.isEqual(to)) {
        if (index % factor == 0) {
            String timestamp = Long.toString(currentTime.getMillis() / 1000);
            Long result = histogramResults.get(timestamp);
            Map<String, Long> point = Maps.newHashMap();
            point.put("x", Long.parseLong(timestamp));
            point.put("y", result != null ? result : 0);
            points.add(point);
        }
        index++;
        nextStep(histogram.getInterval(), currentTime);
    }

    return points;
}

From source file:de.cubeisland.engine.module.basics.command.general.ChatCommands.java

License:Open Source License

@Command(desc = "Mutes a player")
public void mute(CommandSender context, User player, @Optional String duration) {
    BasicsUserEntity bUser = player.attachOrGet(BasicsAttachment.class, module).getBasicsUser().getEntity();
    Timestamp muted = bUser.getValue(TABLE_BASIC_USER.MUTED);
    if (muted != null && muted.getTime() < System.currentTimeMillis()) {
        context.sendTranslated(NEUTRAL, "{user} was already muted!", player);
    }/*from w  ww .  j  a va  2s  . c om*/
    Duration dura = module.getConfiguration().commands.defaultMuteTime;
    if (duration != null) {
        try {
            dura = converter.fromNode(StringNode.of(duration), null, null);
        } catch (ConversionException e) {
            context.sendTranslated(NEGATIVE, "Invalid duration format!");
            return;
        }
    }
    bUser.setValue(TABLE_BASIC_USER.MUTED, new Timestamp(
            System.currentTimeMillis() + (dura.getMillis() == 0 ? DAYS.toMillis(9001) : dura.getMillis())));
    bUser.updateAsync();
    String timeString = dura.getMillis() == 0 ? player.getTranslation(NONE, "ever")
            : TimeUtil.format(player.getLocale(), dura.getMillis());
    player.sendTranslated(NEGATIVE, "You are now muted for {input#amount}!", timeString);
    context.sendTranslated(NEUTRAL, "You muted {user} globally for {input#amount}!", player, timeString);
}

From source file:de.ifgi.fmt.mongo.conv.DurationConverter.java

License:Open Source License

/**
 * //from  w ww .  j  a  v a 2 s  . c o m
 * @param value
 * @param optionalExtraInfo
 * @return
 */
@Override
public Object encode(Object value, MappedField optionalExtraInfo) {
    if (value == null)
        return null;
    Duration dt = (Duration) value;
    return dt.getMillis();
}

From source file:es.jpons.persistence.TemporalPersistenceManager.java

License:Open Source License

/**
 * Function to close a vtp from another/* w w w .java2s .com*/
 *
 * @param toClose The vtp to close
 * @param newVtp The other vtp to start
 * @return A copy of the object toClose closed to the left.
 * @throws TemporalException If the closure of the vtp can not be computed.
 */
public PossibilisticVTP closeR(PossibilisticVTP toClose, PossibilisticVTP newVtp) throws TemporalException {
    if (toClose.getSide() != null && toClose.getSide().compareTo(OpenInterval.UC) == 0) {
        DateTime startmp = new DateTime(toClose.getStartMP());
        //            DateTime leftmp = startmp.minus(toClose.getStartLeft());
        DateTime rightmp = startmp.plus(toClose.getStartRight());

        DateTime newmp = new DateTime(newVtp.getStartMP());
        DateTime newleft = newmp.minus(newVtp.getStartLeft());
        //            DateTime newright = newmp.plus(newVtp.getStartRight());

        if (rightmp.isBefore(newleft)) {
            log.trace("Closing ending point");
            Duration d = new Duration(startmp, newmp);
            Duration d1 = new Duration(d.getMillis() / 2);

            DateTime closeMp = new DateTime(startmp);
            closeMp = closeMp.plus(d1);

            Duration left = new Duration(startmp, closeMp);
            Duration right = new Duration(closeMp, newleft);

            toClose.setEndMP(closeMp.getMillis());
            toClose.setEndLeft(left.getMillis());
            toClose.setEndRight(right.getMillis());

            toClose.setSide(null);

        } else {
            log.error("The point cannot be closed");
            throw new TemporalException("The point cannot be closed");
        }

        //            
        //            DateTime lefts = startmp.plus( new Instant(newVtp.getStartLeft()));
        //            if(newVtp.getStartMP()> )
    } else {
        log.error("The point is not open");
        throw new TemporalException("The point is not open");
    }

    return toClose;
}

From source file:es.jpons.temporal.types.PossibilisticVTP.java

License:Open Source License

/**
 * Constructor/*from  ww w  .  j  a va2 s . c  om*/
 *
 * @param sday Starting day of month
 * @param smonth Starting month of year
 * @param syear Starting year
 * @param sLeftDays Number of days of the left starting margin.
 * @param sRightDays Number of days of the right starting margin.
 * @param eday Ending day of month.
 * @param emonth Ending month of year.
 * @param eyear Ending year.
 * @param eLeftDays Number of days of the ending margin.
 * @param eRightDays Number of days of the right ending margin.
 */
public PossibilisticVTP(Integer sday, Integer smonth, Integer syear, Integer sLeftDays, Integer sRightDays,
        Integer eday, Integer emonth, Integer eyear, Integer eLeftDays, Integer eRightDays) {
    DateTime dateStartingMainPoint, dateStartingLeft, dateStartingRight;
    DateTime dateEndingMainPoint, dateEndingLeft, dateEndingRight;

    dateStartingMainPoint = new DateTime(syear, smonth, sday, 0, 0);

    dateStartingLeft = dateStartingMainPoint.minusDays(sLeftDays);
    dateStartingRight = dateStartingMainPoint.plusDays(sRightDays);

    Duration d = new Duration(dateStartingLeft, dateStartingMainPoint);
    Duration d1 = new Duration(dateStartingMainPoint, dateStartingRight);

    dateEndingMainPoint = new DateTime(eyear, emonth, eday, 0, 0);
    dateEndingLeft = dateEndingMainPoint.minusDays(eLeftDays);
    dateEndingRight = dateEndingMainPoint.plusDays(eRightDays);

    Duration d2 = new Duration(dateEndingLeft, dateEndingMainPoint);
    Duration d3 = new Duration(dateEndingMainPoint, dateEndingRight);

    //            PossibilisticTP start, end;
    PossibilisticVTP pvp;

    //            
    //            start = new PossibilisticTP(
    //                    dateStartingMainPoint.toDate(),
    //                    dateStartingLeft.toDate(),
    //                    dateStartingRight.toDate());
    //            end = new PossibilisticTP(   
    //                    dateEndingMainPoint.toDate(),
    //                    dateEndingLeft.toDate(),
    //                    dateEndingRight.toDate());

    //PossibilisticVTP(dateStartingMainPoint.toDate(),d.getMillis(),d1.getMillis(),dateEndingMainPoint.toDate(),d2.getMillis(),d3.getMillis());
    this.startMP = dateStartingMainPoint.getMillis();
    this.startLeft = d.getMillis();
    this.startRight = d1.getMillis();
    this.endMP = dateEndingMainPoint.getMillis();
    this.endLeft = d2.getMillis();
    this.endRight = d3.getMillis();
    this.startDate = dateStartingMainPoint.toDate();
    this.endDate = dateEndingMainPoint.toDate();

    convertToTM();

}