1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools.date;
|
---|
3 |
|
---|
4 | import java.time.Instant;
|
---|
5 | import java.time.format.DateTimeFormatter;
|
---|
6 | import java.time.format.FormatStyle;
|
---|
7 | import java.util.Objects;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.tools.Utils;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * A timespan defined by a start and end instant.
|
---|
13 | */
|
---|
14 | public final class Interval {
|
---|
15 |
|
---|
16 | private final Instant start;
|
---|
17 | private final Instant end;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Constructs a new {@code Interval}
|
---|
21 | * @param start start instant
|
---|
22 | * @param end end instant
|
---|
23 | */
|
---|
24 | public Interval(Instant start, Instant end) {
|
---|
25 | this.start = start;
|
---|
26 | this.end = end;
|
---|
27 | }
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Returns an ISO 8601 compatible string
|
---|
31 | * @return an ISO 8601 compatible string
|
---|
32 | */
|
---|
33 | @Override
|
---|
34 | public String toString() {
|
---|
35 | return start + "/" + end;
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Formats the interval of the given track as a human readable string
|
---|
40 | * @return The interval as a string
|
---|
41 | */
|
---|
42 | public String format() {
|
---|
43 | String ts = "";
|
---|
44 | DateTimeFormatter df = DateUtils.getDateFormatter(FormatStyle.SHORT);
|
---|
45 | String earliestDate = df.format(getStart());
|
---|
46 | String latestDate = df.format(getEnd());
|
---|
47 |
|
---|
48 | if (earliestDate.equals(latestDate)) {
|
---|
49 | DateTimeFormatter tf = DateUtils.getTimeFormatter(FormatStyle.SHORT);
|
---|
50 | ts += earliestDate + ' ';
|
---|
51 | ts += tf.format(getStart()) + " \u2013 " + tf.format(getEnd());
|
---|
52 | } else {
|
---|
53 | DateTimeFormatter dtf = DateUtils.getDateTimeFormatter(FormatStyle.SHORT, FormatStyle.MEDIUM);
|
---|
54 | ts += dtf.format(getStart()) + " \u2013 " + dtf.format(getEnd());
|
---|
55 | }
|
---|
56 |
|
---|
57 | ts += String.format(" (%s)", Utils.getDurationString(getEnd().toEpochMilli() - getStart().toEpochMilli()));
|
---|
58 | return ts;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Returns start instant.
|
---|
63 | * @return start instant
|
---|
64 | */
|
---|
65 | public Instant getStart() {
|
---|
66 | return start;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Returns end instant.
|
---|
71 | * @return end instant
|
---|
72 | */
|
---|
73 | public Instant getEnd() {
|
---|
74 | return end;
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public boolean equals(Object o) {
|
---|
79 | if (this == o) return true;
|
---|
80 | if (!(o instanceof Interval)) return false;
|
---|
81 | Interval interval = (Interval) o;
|
---|
82 | return Objects.equals(start, interval.start) && Objects.equals(end, interval.end);
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public int hashCode() {
|
---|
87 | return Objects.hash(start, end);
|
---|
88 | }
|
---|
89 | }
|
---|