Example usage for org.joda.time Duration Duration

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

Introduction

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

Prototype

public Duration(Object duration) 

Source Link

Document

Creates a duration from the specified object using the org.joda.time.convert.ConverterManager ConverterManager .

Usage

From source file:com.thoughtworks.go.domain.Stage.java

License:Apache License

public RunDuration getDuration() {
    if (!state.completed()) {
        return RunDuration.IN_PROGRESS_DURATION;
    }/* w  w  w  .  j  av a2s.c  o  m*/
    Date start = new Date(createdTime.getTime());
    Date end = new Date(lastTransitionedTime.getTime());
    return new RunDuration.ActualDuration(new Duration(end.getTime() - start.getTime()));
}

From source file:com.thoughtworks.go.i18n.Localizer.java

License:Apache License

public String localize(Duration d) {
    if (d.equals(new Duration(0)))
        return "";
    return PeriodFormat.getDefault().withLocale(currentLocale.getLocale()).print(d.toPeriod());
}

From source file:com.thoughtworks.go.server.ui.JobInstanceModel.java

License:Apache License

private Duration eta() {
    return new Duration(jobDurationStrategy.getExpectedDurationMillis(getIdentifier().getPipelineName(),
            getIdentifier().getStageName(), instance));
}

From source file:com.tkmtwo.timex.convert.DurationConverter.java

License:Apache License

@Override
public Duration convert(String s) {
    logger.debug("Converting '{}' to a Duration.", s);
    checkArgument(!isNullOrEmpty(s), "String may not be empty.");

    Long l = null;/*from ww  w.  ja  va  2s.  c  o m*/

    try {
        l = parser.parseExpression(s).getValue(Long.class);
    } catch (Exception ex) {
        throw new IllegalArgumentException("String '" + s + "' cannot be converted.", ex);
    }

    checkNotNull(l, "String '" + s + "' evaluated to null.");
    return new Duration(l.longValue());
}

From source file:com.vaushell.superpipes.dispatch.ConfigProperties.java

License:Open Source License

/**
 * Get config. throws IllegalArgumentException if the key is not a long or doesn't exist.
 *
 * @param key Key of parameter./*from w  w w.  j a  v  a 2s  . c  om*/
 * @return the value.
 */
public Duration getConfigDuration(final String key) {
    final String value = getConfig(key, false);

    try {
        final long duration = Long.parseLong(value);
        if (duration < 0L) {
            throw new IllegalArgumentException("Property '" + key + "' can't be <=0. Should be null or empty.");
        }

        return new Duration(duration);
    } catch (final NumberFormatException ex) {
        throw new IllegalArgumentException("Property '" + key + "' must be a long", ex);
    }
}

From source file:com.vaushell.superpipes.dispatch.ConfigProperties.java

License:Open Source License

/**
 * Get config.//w w w . j a va 2s . c  o m
 *
 * @param key Key of parameter.
 * @param defaultValue Default value if the key doesn't exist.
 * @return the value.
 */
public Duration getConfigDuration(final String key, final Duration defaultValue) {
    final String value = getConfig(key, true);

    if (value == null) {
        return defaultValue;
    } else {
        try {
            final long duration = Long.parseLong(value);
            if (duration < 0L) {
                throw new IllegalArgumentException(
                        "Property '" + key + "' can't be <=0. Should be null or empty.");
            }

            return new Duration(duration);
        } catch (final NumberFormatException ex) {
            throw new IllegalArgumentException("Property '" + key + "' must be a long", ex);
        }
    }
}

From source file:com.vaushell.superpipes.dispatch.ErrorMailer.java

License:Open Source License

public ErrorMailer() {
    super();//from   w w w .  j a va  2  s .  c  o  m

    this.activated = true;
    this.internalStack = new ArrayList<>();
    this.properties = new ConfigProperties();
    this.antiBurst = new Duration(1000L);
}

From source file:com.vaushell.superpipes.dispatch.ErrorMailer.java

License:Open Source License

private List<String> getLastErrorsOrWait() throws InterruptedException {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("[" + getClass().getSimpleName() + "] getLastErrorsOrWait");
    }//from  w  ww.j  a v  a  2  s  . c o m

    synchronized (internalStack) {
        Duration remaining;
        if (lastPop == null) {
            remaining = new Duration(0L);
        } else {
            // Null for now
            final Duration elapsed = new Duration(lastPop, null);
            remaining = antiBurst.minus(elapsed);
        }

        while (internalStack.isEmpty() || remaining.getMillis() > 0L) {
            if (internalStack.isEmpty() || remaining.getMillis() <= 0L) {
                internalStack.wait();
            } else {
                internalStack.wait(remaining.getMillis());
            }

            if (lastPop == null) {
                remaining = new Duration(0L);
            } else {
                final Duration elapsed = new Duration(lastPop, null);
                remaining = antiBurst.minus(elapsed);
            }
        }

        final List<String> ret = new ArrayList<>();
        ret.addAll(internalStack);

        internalStack.clear();

        lastPop = new DateTime();

        return ret;
    }
}

From source file:com.vaushell.superpipes.nodes.buffer.N_Buffer.java

License:Open Source License

private void pushMessage(final Message message) throws IOException {
    final DateTime now = new DateTime();

    final Duration delta;
    if (getProperties().containsKey("wait-min") && getProperties().containsKey("wait-max")) {
        final int waitMin = getProperties().getConfigInteger("wait-min");
        final int waitMax = getProperties().getConfigInteger("wait-max");

        if (waitMin == waitMax) {
            delta = new Duration((long) waitMin);
        } else {//  w w  w .  j  av a2  s  . com
            delta = new Duration((long) (rnd.nextInt(waitMax - waitMin) + waitMin));
        }
    } else {
        delta = new Duration(0L);
    }

    final DateTime ID;
    if (messageIDs.isEmpty()) {
        ID = now.plus(delta);
    } else {
        final DateTime askedTime = now.plus(delta);

        final long lastID = messageIDs.last();
        final DateTime lastTime = new DateTime(lastID);

        if (askedTime.isBefore(lastTime)) {
            ID = lastTime.plusMillis(1);
        } else {
            ID = askedTime;
        }
    }

    final Path p = messagesPath.resolve(Long.toString(ID.getMillis()));

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("[" + getNodeID() + "] write message with ID=" + ID);
    }

    writeMessage(p, message);

    messageIDs.add(ID.getMillis());
}

From source file:com.vaushell.superpipes.nodes.buffer.N_Buffer.java

License:Open Source License

private Duration getTimeToWait(final DateTime from) {
    // Best slot//from w ww  . ja v  a  2 s  . c  o m
    Duration minDuration;
    if (slots.isEmpty()) {
        minDuration = new Duration(0L);
    } else {
        minDuration = null;
        for (final Slot slot : slots) {
            final Duration duration = slot.getSmallestDiff(from);
            if (minDuration == null || duration.isShorterThan(minDuration)) {
                minDuration = duration;

                if (minDuration.getMillis() <= 0L) {
                    break;
                }
            }
        }
    }

    // Anti burst
    if (getProperties().containsKey("flow-limit") && lastWrite != null) {
        final Duration diff = new Duration(lastWrite, from);

        final Duration toAdd = getProperties().getConfigDuration("flow-limit").minus(diff);
        if (toAdd.isLongerThan(minDuration)) {
            minDuration = toAdd;
        }
    }

    // First message
    if (!messageIDs.isEmpty()) {
        final long firstID = messageIDs.first();
        final DateTime first = new DateTime(firstID);

        if (first.isAfter(from)) {
            final Duration diff = new Duration(from, first);
            if (diff.isLongerThan(minDuration)) {
                minDuration = diff;
            }
        }
    }

    // Result
    return minDuration;
}