]> git.basschouten.com Git - openhab-addons.git/blob
c6c8704861863e96af35f93693c95c0b8bf7b4d6
[openhab-addons.git] /
1 /*
2  * Copyright 2017 Gregory Moyer
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openhab.binding.sleepiq.api.model;
17
18 import java.time.Duration;
19 import java.util.Objects;
20 import java.util.concurrent.TimeUnit;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23
24 public class TimeSince {
25     private static final Pattern PATTERN = Pattern.compile("(([0-9]+) d )?([0-9]{2}):([0-9]{2}):([0-9]{2})",
26             Pattern.CASE_INSENSITIVE);
27
28     private Duration duration;
29
30     public Duration getDuration() {
31         return duration;
32     }
33
34     public void setDuration(Duration duration) {
35         this.duration = duration == null ? null : duration.abs();
36     }
37
38     public TimeSince withDuration(long days, long hours, long minutes, long seconds) {
39         return withDuration(Duration.ofSeconds(TimeUnit.DAYS.toSeconds(days) + TimeUnit.HOURS.toSeconds(hours)
40                 + TimeUnit.MINUTES.toSeconds(minutes) + seconds));
41     }
42
43     public TimeSince withDuration(Duration duration) {
44         setDuration(duration);
45         return this;
46     }
47
48     @Override
49     public int hashCode() {
50         final int prime = 31;
51         int result = 1;
52         result = prime * result + ((duration == null) ? 0 : duration.hashCode());
53         return result;
54     }
55
56     @Override
57     public boolean equals(Object obj) {
58         if (this == obj) {
59             return true;
60         }
61         if (obj == null) {
62             return false;
63         }
64         if (!(obj instanceof TimeSince)) {
65             return false;
66         }
67         TimeSince other = (TimeSince) obj;
68         if (duration == null) {
69             if (other.duration != null) {
70                 return false;
71             }
72         } else if (!duration.equals(other.duration)) {
73             return false;
74         }
75         return true;
76     }
77
78     @Override
79     public String toString() {
80         long totalDays = duration.toDays();
81         long totalHours = duration.toHours();
82         long totalMinutes = duration.toMinutes();
83         long totalSeconds = duration.getSeconds();
84
85         long hours = totalHours - TimeUnit.DAYS.toHours(totalDays);
86         long minutes = totalMinutes - TimeUnit.HOURS.toMinutes(totalHours);
87         long seconds = totalSeconds - TimeUnit.MINUTES.toSeconds(totalMinutes);
88
89         if (totalDays > 0) {
90             return String.format("%d d %02d:%02d:%02d", totalDays, hours, minutes, seconds);
91         }
92
93         return String.format("%02d:%02d:%02d", hours, minutes, seconds);
94     }
95
96     public static TimeSince parse(CharSequence text) {
97         Objects.requireNonNull(text, "text");
98
99         Matcher matcher = PATTERN.matcher(text);
100         if (!matcher.matches()) {
101             return new TimeSince().withDuration(Duration.ZERO);
102         }
103
104         String dayMatch = matcher.group(2);
105         String hourMatch = matcher.group(3);
106         String minuteMatch = matcher.group(4);
107         String secondMatch = matcher.group(5);
108
109         StringBuilder sb = new StringBuilder("P");
110         if (dayMatch != null) {
111             sb.append(dayMatch).append('D');
112         }
113         sb.append('T').append(hourMatch).append('H').append(minuteMatch).append('M').append(secondMatch).append('S');
114
115         return new TimeSince().withDuration(Duration.parse(sb.toString()));
116     }
117 }