View Javadoc
1   package ca.uhn.fhir.model.primitive;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import java.util.Calendar;
24  import java.util.Date;
25  import java.util.TimeZone;
26  
27  import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
28  import ca.uhn.fhir.model.api.annotation.DatatypeDef;
29  import ca.uhn.fhir.model.api.annotation.SimpleSetter;
30  import ca.uhn.fhir.parser.DataFormatException;
31  
32  /**
33   * Represents a FHIR instant datatype. Valid precisions values for this type are:
34   * <ul>
35   * <li>{@link TemporalPrecisionEnum#SECOND}
36   * <li>{@link TemporalPrecisionEnum#MILLI}
37   * </ul>
38   */
39  @DatatypeDef(name = "instant")
40  public class InstantDt extends BaseDateTimeDt {
41  
42  	/**
43  	 * The default precision for this type
44  	 */
45  	public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.MILLI;
46  
47  	/**
48  	 * Constructor which creates an InstantDt with <b>no timne value</b>. Note
49  	 * that unlike the default constructor for the Java {@link Date} or
50  	 * {@link Calendar} objects, this constructor does not initialize the object
51  	 * with the current time.
52  	 * 
53  	 * @see #withCurrentTime() to create a new object that has been initialized
54  	 *      with the current time.
55  	 */
56  	public InstantDt() {
57  		super();
58  	}
59  
60  	/**
61  	 * Create a new DateTimeDt
62  	 */
63  	public InstantDt(Calendar theCalendar) {
64  		super(theCalendar.getTime(), DEFAULT_PRECISION, theCalendar.getTimeZone());
65  	}
66  
67  	/**
68  	 * Create a new instance using the given date, precision level, and time zone
69  	 * 
70  	 * @throws DataFormatException
71  	 *             If the specified precision is not allowed for this type
72  	 */
73  	public InstantDt(Date theDate, TemporalPrecisionEnum thePrecision, TimeZone theTimezone) {
74  		super(theDate, thePrecision, theTimezone);
75  	}
76  
77  
78  	/**
79  	 * Create a new DateTimeDt using an existing value. <b>Use this constructor with caution</b>,
80  	 * as it may create more precision than warranted (since for example it is possible to pass in
81  	 * a DateTime with only a year, and this constructor will convert to an InstantDt with 
82  	 * milliseconds precision).
83  	 */
84  	public InstantDt(BaseDateTimeDt theDateTime) {
85  		// Do not call super(foo) here, we don't want to trigger a DataFormatException
86  		setValue(theDateTime.getValue());
87  		setPrecision(DEFAULT_PRECISION);
88  		setTimeZone(theDateTime.getTimeZone());
89  	}
90  
91  	/**
92  	 * Create a new DateTimeDt with the given date/time and {@link TemporalPrecisionEnum#MILLI} precision
93  	 */
94  	@SimpleSetter(suffix = "WithMillisPrecision")
95  	public InstantDt(@SimpleSetter.Parameter(name = "theDate") Date theDate) {
96  		super(theDate, DEFAULT_PRECISION, TimeZone.getDefault());
97  	}
98  
99  	/**
100 	 * Constructor which accepts a date value and a precision value. Valid
101 	 * precisions values for this type are:
102 	 * <ul>
103 	 * <li>{@link TemporalPrecisionEnum#SECOND}
104 	 * <li>{@link TemporalPrecisionEnum#MILLI}
105 	 * </ul>
106 	 */
107 	@SimpleSetter
108 	public InstantDt(@SimpleSetter.Parameter(name = "theDate") Date theDate, @SimpleSetter.Parameter(name = "thePrecision") TemporalPrecisionEnum thePrecision) {
109 		setValue(theDate);
110 		setPrecision(thePrecision);
111 		setTimeZone(TimeZone.getDefault());
112 	}
113 
114 	/**
115 	 * Create a new InstantDt from a string value
116 	 * 
117 	 * @param theString
118 	 *            The string representation of the string. Must be in a valid
119 	 *            format according to the FHIR specification
120 	 * @throws DataFormatException
121 	 */
122 	public InstantDt(String theString) {
123 		super(theString);
124 	}
125 
126 	/**
127 	 * Invokes {@link Date#after(Date)} on the contained Date against the given
128 	 * date
129 	 * 
130 	 * @throws NullPointerException
131 	 *             If the {@link #getValue() contained Date} is null
132 	 */
133 	public boolean after(Date theDate) {
134 		return getValue().after(theDate);
135 	}
136 
137 	/**
138 	 * Invokes {@link Date#before(Date)} on the contained Date against the given
139 	 * date
140 	 * 
141 	 * @throws NullPointerException
142 	 *             If the {@link #getValue() contained Date} is null
143 	 */
144 	public boolean before(Date theDate) {
145 		return getValue().before(theDate);
146 	}
147 
148 	/**
149 	 * Sets the value of this instant to the current time (from the system
150 	 * clock) and the local/default timezone (as retrieved using
151 	 * {@link TimeZone#getDefault()}. This TimeZone is generally obtained from
152 	 * the underlying OS.
153 	 */
154 	public void setToCurrentTimeInLocalTimeZone() {
155 		setValue(new Date());
156 		setTimeZone(TimeZone.getDefault());
157 	}
158 
159 	@Override
160 	protected boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
161 		switch (thePrecision) {
162 		case SECOND:
163 		case MILLI:
164 			return true;
165 		default:
166 			return false;
167 		}
168 	}
169 
170 	/**
171 	 * Factory method which creates a new InstantDt with millisecond precision and initializes it with the
172 	 * current time and the system local timezone.
173 	 */
174 	public static InstantDt withCurrentTime() {
175 		return new InstantDt(new Date(), TemporalPrecisionEnum.MILLI, TimeZone.getDefault());
176 	}
177 
178 	/**
179 	 * Returns the default precision for this datatype
180 	 * 
181 	 * @see #DEFAULT_PRECISION
182 	 */
183 	@Override
184 	protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() {
185 		return DEFAULT_PRECISION;
186 	}
187 
188 }