]> git.basschouten.com Git - openhab-addons.git/blob
3b83451cfc278cfd7e4cfe98eb0955062b8c7d41
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.sleepiq.internal.api.dto;
14
15 import java.time.Duration;
16 import java.util.Objects;
17 import java.util.concurrent.TimeUnit;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21 /**
22  * The {@link TimeSince} converts from "nn d hh:mm:ss" to Duration
23  *
24  * @author Gregory Moyer - Initial contribution
25  */
26 public class TimeSince {
27     private static final Pattern PATTERN = Pattern.compile("(([0-9]+) d )?([0-9]{2}):([0-9]{2}):([0-9]{2})",
28             Pattern.CASE_INSENSITIVE);
29
30     private Duration duration;
31
32     public Duration getDuration() {
33         return duration;
34     }
35
36     public void setDuration(Duration duration) {
37         this.duration = duration == null ? null : duration.abs();
38     }
39
40     public TimeSince withDuration(long days, long hours, long minutes, long seconds) {
41         return withDuration(Duration.ofSeconds(TimeUnit.DAYS.toSeconds(days) + TimeUnit.HOURS.toSeconds(hours)
42                 + TimeUnit.MINUTES.toSeconds(minutes) + seconds));
43     }
44
45     public TimeSince withDuration(Duration duration) {
46         setDuration(duration);
47         return this;
48     }
49
50     @Override
51     public int hashCode() {
52         final int prime = 31;
53         int result = 1;
54         result = prime * result + ((duration == null) ? 0 : duration.hashCode());
55         return result;
56     }
57
58     @Override
59     public boolean equals(Object obj) {
60         if (this == obj) {
61             return true;
62         }
63         if (obj == null) {
64             return false;
65         }
66         if (!(obj instanceof TimeSince)) {
67             return false;
68         }
69         TimeSince other = (TimeSince) obj;
70         if (duration == null) {
71             if (other.duration != null) {
72                 return false;
73             }
74         } else if (!duration.equals(other.duration)) {
75             return false;
76         }
77         return true;
78     }
79
80     @Override
81     public String toString() {
82         long totalDays = duration.toDays();
83         long totalHours = duration.toHours();
84         long totalMinutes = duration.toMinutes();
85         long totalSeconds = duration.getSeconds();
86
87         long hours = totalHours - TimeUnit.DAYS.toHours(totalDays);
88         long minutes = totalMinutes - TimeUnit.HOURS.toMinutes(totalHours);
89         long seconds = totalSeconds - TimeUnit.MINUTES.toSeconds(totalMinutes);
90
91         if (totalDays > 0) {
92             return String.format("%d d %02d:%02d:%02d", totalDays, hours, minutes, seconds);
93         }
94
95         return String.format("%02d:%02d:%02d", hours, minutes, seconds);
96     }
97
98     public static TimeSince parse(CharSequence text) {
99         Objects.requireNonNull(text, "text");
100
101         Matcher matcher = PATTERN.matcher(text);
102         if (!matcher.matches()) {
103             return new TimeSince().withDuration(Duration.ZERO);
104         }
105
106         String dayMatch = matcher.group(2);
107         String hourMatch = matcher.group(3);
108         String minuteMatch = matcher.group(4);
109         String secondMatch = matcher.group(5);
110
111         StringBuilder sb = new StringBuilder("P");
112         if (dayMatch != null) {
113             sb.append(dayMatch).append('D');
114         }
115         sb.append('T').append(hourMatch).append('H').append(minuteMatch).append('M').append(secondMatch).append('S');
116
117         return new TimeSince().withDuration(Duration.parse(sb.toString()));
118     }
119 }