Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TimeStamp |
|
| 2.142857142857143;2.143 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.policy; | |
11 | ||
12 | import java.util.Calendar; | |
13 | import java.util.Date; | |
14 | import java.util.TimeZone; | |
15 | ||
16 | ||
17 | ||
18 | /** | |
19 | * A read-only point in time. | |
20 | * @since 0.1.0 | |
21 | */ | |
22 | public class TimeStamp { | |
23 | ||
24 | //---- Static | |
25 | ||
26 | /** | |
27 | * Creates a time object from the current values of the specified calendar. | |
28 | * @param calendar a {@link Calendar} object. | |
29 | * @return a {@link TimeStamp} whose date and time zone correspond to that of the specified | |
30 | * calendar. | |
31 | * @since 0.1.0 | |
32 | */ | |
33 | public static TimeStamp fromCalendar (Calendar calendar) { | |
34 | 3 | return new TimeStamp(calendar.getTime(), calendar.getTimeZone()); |
35 | } | |
36 | ||
37 | //---- State | |
38 | ||
39 | /** | |
40 | * @since 0.1.0 | |
41 | */ | |
42 | private final Date date; | |
43 | ||
44 | /** | |
45 | * @since 0.1.0 | |
46 | */ | |
47 | private final TimeZone timeZone; | |
48 | ||
49 | //---- Constructors | |
50 | ||
51 | /** | |
52 | * Creates point in time with date and zone. | |
53 | * @param date a {@link Date}. | |
54 | * @param timeZone a {@link TimeZone}. | |
55 | * @since 0.1.0 | |
56 | */ | |
57 | 93 | public TimeStamp (Date date, TimeZone timeZone) { |
58 | 93 | if (date == null || timeZone == null) { |
59 | 0 | throw new IllegalArgumentException("Date or zone is null."); |
60 | } | |
61 | 93 | this.date = (Date) date.clone(); |
62 | 93 | this.timeZone = timeZone; |
63 | 93 | } |
64 | ||
65 | //---- Methods | |
66 | ||
67 | /** | |
68 | * Returns the date. | |
69 | * @return the date. | |
70 | * @since 0.1.0 | |
71 | */ | |
72 | public Date getDate () { | |
73 | 21 | return (Date) this.date.clone(); |
74 | } | |
75 | ||
76 | /** | |
77 | * Returns the time zone. | |
78 | * @return the time zone. | |
79 | * @since 0.1.0 | |
80 | */ | |
81 | public TimeZone getTimeZone () { | |
82 | 18 | return this.timeZone; |
83 | } | |
84 | ||
85 | //---- Object | |
86 | ||
87 | /** | |
88 | * @since 0.3.0 | |
89 | */ | |
90 | @Override | |
91 | public boolean equals (Object obj) { | |
92 | 4 | if (this == obj) { |
93 | 4 | return true; |
94 | } | |
95 | 0 | if (!(obj instanceof TimeStamp)) { |
96 | 0 | return false; |
97 | } | |
98 | 0 | final TimeStamp that = (TimeStamp) obj; |
99 | 0 | return this.date.equals(that.date) && this.timeZone.equals(that.timeZone); |
100 | } | |
101 | ||
102 | /** | |
103 | * @since 0.3.0 | |
104 | */ | |
105 | @Override | |
106 | public int hashCode () { | |
107 | 0 | final int factor = 41; |
108 | 0 | return this.date.hashCode() + factor * this.timeZone.hashCode(); |
109 | } | |
110 | ||
111 | /** | |
112 | * @since 0.3.0 | |
113 | */ | |
114 | @Override | |
115 | public String toString () { | |
116 | 0 | Calendar cal = Calendar.getInstance(getTimeZone()); |
117 | 0 | cal.setTime(getDate()); |
118 | 0 | return cal.toString(); |
119 | } | |
120 | ||
121 | } |