]> git.basschouten.com Git - openhab-addons.git/blob
674e2ef7449d98dbb6ac976bbc0b78c02adb93b7
[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.time.format.DateTimeParseException;
20 import java.util.Objects;
21 import java.util.concurrent.TimeUnit;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 public class TimeSince
26 {
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     {
34         return duration;
35     }
36
37     public void setDuration(Duration duration)
38     {
39         this.duration = duration == null ? null : duration.abs();
40     }
41
42     public TimeSince withDuration(long days, long hours, long minutes, long seconds)
43     {
44         return withDuration(Duration.ofSeconds(TimeUnit.DAYS.toSeconds(days)
45                                                + TimeUnit.HOURS.toSeconds(hours)
46                                                + TimeUnit.MINUTES.toSeconds(minutes)
47                                                + seconds));
48     }
49
50     public TimeSince withDuration(Duration duration)
51     {
52         setDuration(duration);
53         return this;
54     }
55
56     @Override
57     public int hashCode()
58     {
59         final int prime = 31;
60         int result = 1;
61         result = prime * result + ((duration == null) ? 0 : duration.hashCode());
62         return result;
63     }
64
65     @Override
66     public boolean equals(Object obj)
67     {
68         if (this == obj)
69         {
70             return true;
71         }
72         if (obj == null)
73         {
74             return false;
75         }
76         if (!(obj instanceof TimeSince))
77         {
78             return false;
79         }
80         TimeSince other = (TimeSince)obj;
81         if (duration == null)
82         {
83             if (other.duration != null)
84             {
85                 return false;
86             }
87         }
88         else if (!duration.equals(other.duration))
89         {
90             return false;
91         }
92         return true;
93     }
94
95     @Override
96     public String toString()
97     {
98         long totalDays = duration.toDays();
99         long totalHours = duration.toHours();
100         long totalMinutes = duration.toMinutes();
101         long totalSeconds = duration.getSeconds();
102
103         long hours = totalHours - TimeUnit.DAYS.toHours(totalDays);
104         long minutes = totalMinutes - TimeUnit.HOURS.toMinutes(totalHours);
105         long seconds = totalSeconds - TimeUnit.MINUTES.toSeconds(totalMinutes);
106
107         if (totalDays > 0)
108         {
109             return String.format("%d d %02d:%02d:%02d", totalDays, hours, minutes, seconds);
110         }
111
112         return String.format("%02d:%02d:%02d", hours, minutes, seconds);
113     }
114
115     public static TimeSince parse(CharSequence text)
116     {
117         Objects.requireNonNull(text, "text");
118
119         Matcher matcher = PATTERN.matcher(text);
120         if (!matcher.matches())
121         {
122             throw new DateTimeParseException("Text cannot be parsed", text, 0);
123         }
124
125         String dayMatch = matcher.group(2);
126         String hourMatch = matcher.group(3);
127         String minuteMatch = matcher.group(4);
128         String secondMatch = matcher.group(5);
129
130         StringBuilder sb = new StringBuilder("P");
131         if (dayMatch != null)
132         {
133             sb.append(dayMatch).append('D');
134         }
135         sb.append('T')
136           .append(hourMatch)
137           .append('H')
138           .append(minuteMatch)
139           .append('M')
140           .append(secondMatch)
141           .append('S');
142
143         return new TimeSince().withDuration(Duration.parse(sb.toString()));
144     }
145 }