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:DurationTypeAdapter.java

License:Apache License

@Override
public Duration read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/*from  w ww .j a v a  2s  . c o  m*/
        return null;
    }
    return new Duration(in.nextString());
}

From source file:br.com.anteros.persistence.metadata.converter.converters.JodaDurationConverter.java

License:Apache License

public Duration convertToEntityAttribute(Long mills) {
    return new Duration(mills);
}

From source file:ch.eitchnet.android.mabea.model.Today.java

License:Open Source License

/**
 * Default constructor/*  w w w.j a  v a 2s.c om*/
 * 
 * @param requiredWorkPerDay
 * @param initialBooking
 */
public Today(Duration requiredWorkPerDay, Booking initialBooking) {

    if (requiredWorkPerDay == null)
        throw new IllegalArgumentException("requiredWorkPerDay must not be null");
    if (initialBooking == null)
        throw new IllegalArgumentException("Initial booking must not be null!");
    if (initialBooking.getState() != State.LOGGED_OUT)
        throw new IllegalArgumentException(
                "Initial booking must be " + MabeaState.State.LOGGED_OUT + " not " + initialBooking.getState());
    if (initialBooking.getTimestamp().compareTo(LocalDate.now().toLocalDateTime(LocalTime.MIDNIGHT)) > 0)
        throw new IllegalArgumentException("The initial booking must be at midnight of today!");

    this.dateCreated = LocalDate.now();
    this.requiredWorkPerDay = requiredWorkPerDay;
    this.startBalance = initialBooking.getBalance();
    this.workToday = new Duration(0L);
    this.bookings = new ArrayList<Booking>();
    this.bookings.add(initialBooking);
}

From source file:ch.oakmountain.tpa.solver.PeriodicalTimeFrame.java

License:Apache License

/**
 * Positive distance ub->this and 0 if this in_T [lb,ub]
 * @param lb/*from www .  ja v a 2 s.co  m*/
 * @param ub
 * @return
 */
public Duration distanceAfterInterval(PeriodicalTimeFrame lb, PeriodicalTimeFrame ub) {
    if (isWithinBounds(lb, ub)) {
        return new Duration(0);
    } else {
        return this.distanceAfter(ub);
    }
}

From source file:ch.oakmountain.tpa.solver.PeriodicalTimeFrame.java

License:Apache License

/**
 * Positive distance this->lb and 0 if this in_T [lb,ub]
 *
 * @param lb//from w w  w .j a  va2 s .co m
 * @param ub
 * @return
 */
public Duration distanceBeforeInterval(PeriodicalTimeFrame lb, PeriodicalTimeFrame ub) {
    if (isWithinBounds(lb, ub)) {
        return new Duration(0);
    } else {
        return lb.distanceAfter(this);
    }
}

From source file:ch.oakmountain.tpa.solver.PeriodicalTimeFrame.java

License:Apache License

/**
 * Returns/*ww  w .j a v  a  2s .  com*/
 *  latest-earliest if earliest<= latest and
 *  T - (earliest-latest) if earliest > latest.
 * Always non-negative.
 *
 * @param reference
 * @return
 */
public Duration distanceAfter(PeriodicalTimeFrame reference) {
    PeriodicalTimeFrame moment = this;

    Duration durationAfter;
    if (moment.equals(reference)) {
        durationAfter = new Duration(0);
    } else if (moment.isBeforeOrEqual(reference)) {
        // 0 = start of week
        // T = end of week
        // X = moment
        // R = reference
        // 0__X___R____T
        durationAfter = END_OF_WEEK.toDuration().minus(reference.toDuration())
                .plus(moment.toDuration().minus(START_OF_WEEK.toDuration()));
    } else {
        // 0__R___X____T
        durationAfter = moment.toDuration().minus(reference.toDuration());
    }
    return durationAfter;
}

From source file:ch.oakmountain.tpa.solver.PeriodicalTimeFrame.java

License:Apache License

public Duration toDuration() {
    return new Duration(iLocalMillis);
}

From source file:ch.oakmountain.tpa.solver.SimpleTrainPathApplication.java

License:Apache License

public String getHTMLDescription(MacroscopicTopology macro, TrainPathSlotCatalogue catalogue)
        throws IllegalAccessException {
    StringBuilder sb = new StringBuilder();
    sb.append("<table>");
    sb.append("<th> Property </th>");
    sb.append("<th> Value </th>");

    for (Field field : this.getClass().getDeclaredFields()) {
        // HACK: JaCoCo adds field $jacocoData; which gives problems in regular expressions at 1 A.M....
        if (field.getName().contains("$") || field.get(this) == null
                || field.get(this).toString().contains("$")) {
            continue;
        }//  ww  w  .j a  v  a  2 s  . c o m
        sb.append("<tr>");
        sb.append("<td> " + field.getName() + " </td>");
        sb.append("<td> " + field.get(this) + " </td>");
        sb.append("</tr>");
    }
    for (List<SystemNode> route : macro.getRoutes(from, to)) {
        sb.append("<tr>");
        sb.append("<td> route </td>");
        sb.append("<td>");
        SystemNode previous = null;
        Duration duration = new Duration(0);
        for (SystemNode systemNode : route) {
            sb.append("- " + systemNode.getName() + " -");
            if (previous != null) {
                List<TrainPathSlot> sortedTrainPathSlots = catalogue.getSortedTrainPathSlots(previous,
                        systemNode, PeriodicalTimeFrame.START_OF_WEEK, PeriodicalTimeFrame.END_OF_WEEK);
                if (sortedTrainPathSlots.size() == 0) {
                    throw new IllegalStateException("No slot");

                }
                TrainPathSlot aSlot = sortedTrainPathSlots.get(0);
                Duration stepDuration = aSlot.getEndTime().distanceAfter(aSlot.getStartTime());
                if (stepDuration.isShorterThan(new Duration(0))) {
                    throw new IllegalStateException("Duration of " + aSlot.getFrom() + " - " + aSlot.getTo()
                            + " is negative: " + PeriodicalTimeFrame.formatDuration(stepDuration));
                }
                duration = duration.plus(stepDuration);
            }
            previous = systemNode;
        }
        sb.append("----- " + PeriodicalTimeFrame.formatDuration(duration));
        sb.append("</td>");
        sb.append("</tr>");
    }
    sb.append("</table>");
    return sb.toString();
}

From source file:ch.oakmountain.tpa.solver.SolutionCandidate.java

License:Apache License

/**
 * Verifies that summed distances and direct distance are the same, assuming a path never takes more than whole week, .
 *
 * @param path//from  ww w . j  a  v  a  2  s .c  o m
 */
public static void sanityCheckPath(List<TrainPathSlot> path,
        SimpleTrainPathApplication simpleTrainPathApplication) {

    if (path.size() == 0) {
        throw new IllegalStateException(
                "Path should have size > 0 for request " + simpleTrainPathApplication.getName());
    } else if (!path.get(0).getFrom().equals(simpleTrainPathApplication.getFrom())
            || !path.get(path.size() - 1).getTo().equals(simpleTrainPathApplication.getTo())) {
        throw new IllegalStateException(
                "Path does not start or end at requested locations in " + simpleTrainPathApplication.getName());
    }

    Duration summedDistance = new Duration(0);
    for (TrainPathSlot trainPathSlot : path) {
        summedDistance = summedDistance
                .plus(trainPathSlot.getEndTime().distanceAfter(trainPathSlot.getStartTime()));
    }
    for (int i = 1; i < path.size(); i++) {
        TrainPathSlot previous = path.get(i - 1);
        TrainPathSlot current = path.get(i);
        summedDistance = summedDistance.plus(current.getStartTime().distanceAfter(previous.getEndTime()));

        if (!previous.getTo().equals(current.getFrom())) {
            throw new IllegalStateException("Path is not consistent" + simpleTrainPathApplication.getName());
        }

    }
    Duration directDistance = path.get(path.size() - 1).getEndTime().distanceAfter(path.get(0).getStartTime());
    if (!directDistance.isEqual(summedDistance)) {
        throw new IllegalStateException("Direct and summed distance should be the same in path for request"
                + simpleTrainPathApplication.getName());
    }
}

From source file:ch.oakmountain.tpa.solver.SolutionCandidateFinder.java

License:Apache License

private static void derivePruningParamsFromEarliestPath(SimpleTrainPathApplication application,
        List<TrainPathSlot> earliestPath) {
    PeriodicalTimeFrame earliestPathEndTime = earliestPath.get(earliestPath.size() - 1).getEndTime();
    PeriodicalTimeFrame earliestPathStartTime = earliestPath.get(0).getStartTime();

    // Maximum earlier/later arrival from path's arrival time
    Duration maxEarlierArrivalFromEarliestPath;
    Duration maxLaterArrivalFromEarliestPath;
    if (earliestPathEndTime.isWithinBounds(application.getStartTime(), application.getEndTime())) {
        maxEarlierArrivalFromEarliestPath = application.getEndTime().distanceAfter(earliestPathEndTime);
        maxLaterArrivalFromEarliestPath = new Duration(0);
    } else {//from w  w  w  .j a  v  a2 s  . c  o  m
        maxEarlierArrivalFromEarliestPath = new Duration(0);
        maxLaterArrivalFromEarliestPath = earliestPathEndTime.distanceAfter(application.getEndTime());
    }
    application.getParams().setMAXIMUM_EARLIER_ARRIVAL(maxEarlierArrivalFromEarliestPath);
    application.getParams().setMAXIMUM_LATER_ARRIVAL(maxLaterArrivalFromEarliestPath);

    // Maximum earlier/later departure from path's departure time
    Duration maxLaterDepartureFromEarliestPath;
    Duration maxEarlierDepartureFromEarliestPath;
    if (earliestPathStartTime.isWithinBounds(application.getStartTime(), application.getEndTime())) {
        maxLaterDepartureFromEarliestPath = earliestPathStartTime.distanceAfter(application.getStartTime());
        maxEarlierDepartureFromEarliestPath = new Duration(0);
    } else {
        maxLaterDepartureFromEarliestPath = new Duration(0);
        maxEarlierDepartureFromEarliestPath = application.getStartTime().distanceAfter(earliestPathStartTime);
    }
    application.getParams().setMAXIMUM_LATER_DEPARTURE(maxLaterDepartureFromEarliestPath);
    application.getParams().setMAXIMUM_EARLIER_DEPARTURE(maxEarlierDepartureFromEarliestPath);

    // Mnimum dwell time as hard minimum dwell time
    application.getParams().setMINIMUM_DWELL_TIME(application.getParams().getHARD_MINIMUM_DWELL_TIME());

    // Maximum additional dwell time per system node
    for (int i = 1; i < earliestPath.size(); i++) {
        TrainPathSlot fromSlot = earliestPath.get(i - 1);
        TrainPathSlot toSlot = earliestPath.get(i);
        SystemNode weAreAt = toSlot.getFrom();
        Duration dwellTimeAdditionalToHardMinimum = toSlot.getStartTime().distanceAfter(fromSlot.getEndTime())
                .minus(application.getParams().getHARD_MINIMUM_DWELL_TIME());
        application.getParams().setMAXIMUM_ADDITIONAL_DWELL_TIME(weAreAt, dwellTimeAdditionalToHardMinimum);
    }
}