Example #1
0
  public void ensureIncreasingTime() {
    if (getPositionCount() < 2) return;

    long completeTime = getTime(); // ms
    double completeDistance = getDistance(); // m
    double averageSpeed = completeTime > 0 ? completeDistance / completeTime * 1000 : 1.0; // m/s

    List<P> positions = getPositions();
    P first = positions.get(0);
    if (!first.hasTime()) first.setTime(fromCalendar(Calendar.getInstance(UTC)));

    P previous = first;
    for (int i = 1; i < positions.size(); i++) {
      P next = positions.get(i);
      CompactCalendar time = next.getTime();
      if (time == null || time.equals(previous.getTime())) {
        Double distance = next.calculateDistance(previous);
        Long millis = distance != null ? (long) (distance / averageSpeed * 1000) : null;
        if (millis == null || millis < 1000) millis = 1000L;
        next.setTime(
            fromMillisAndTimeZone(
                previous.getTime().getTimeInMillis() + millis, previous.getTime().getTimeZoneId()));
      }
      previous = next;
    }
  }
Example #2
0
  public long getTime() {
    CompactCalendar minimum = null, maximum = null;
    long totalTimeMilliSeconds = 0;
    List<P> positions = getPositions();
    P previous = null;
    for (P next : positions) {
      if (previous != null) {
        Long time = previous.calculateTime(next);
        if (time != null && time > 0) totalTimeMilliSeconds += time;
      }

      CompactCalendar calendar = next.getTime();
      if (calendar == null) continue;
      if (minimum == null || calendar.before(minimum)) minimum = calendar;
      if (maximum == null || calendar.after(maximum)) maximum = calendar;

      previous = next;
    }

    long maxMinusMin = minimum != null ? maximum.getTimeInMillis() - minimum.getTimeInMillis() : 0;
    return max(maxMinusMin, totalTimeMilliSeconds);
  }