001/*
002Copyright (c) 2011+, HL7, Inc
003All rights reserved.
004
005Redistribution and use in source and binary forms, with or without modification, 
006are permitted provided that the following conditions are met:
007
008 * Redistributions of source code must retain the above copyright notice, this 
009   list of conditions and the following disclaimer.
010 * Redistributions in binary form must reproduce the above copyright notice, 
011   this list of conditions and the following disclaimer in the documentation 
012   and/or other materials provided with the distribution.
013 * Neither the name of HL7 nor the names of its contributors may be used to 
014   endorse or promote products derived from this software without specific 
015   prior written permission.
016
017THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
018ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
019WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
020IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
021INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
022NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
023PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
024WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
025ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
026POSSIBILITY OF SUCH DAMAGE.
027
028*/
029/**
030 * 
031 */
032package org.hl7.fhir.dstu3.model;
033
034import java.util.Calendar;
035import java.util.Date;
036import java.util.TimeZone;
037import java.util.zip.DataFormatException;
038
039import ca.uhn.fhir.model.api.annotation.DatatypeDef;
040
041/**
042 * Represents a FHIR instant datatype. Valid precisions values for this type are:
043 * <ul>
044 * <li>{@link TemporalPrecisionEnum#SECOND}
045 * <li>{@link TemporalPrecisionEnum#MILLI}
046 * </ul>
047 */
048@DatatypeDef(name="instant")
049public class InstantType extends BaseDateTimeType {
050
051        private static final long serialVersionUID = 3L;
052        
053        /**
054         * The default precision for this type
055         */
056        public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.MILLI;
057
058        /**
059         * Constructor which creates an InstantDt with <b>no timne value</b>. Note
060         * that unlike the default constructor for the Java {@link Date} or
061         * {@link Calendar} objects, this constructor does not initialize the object
062         * with the current time.
063         * 
064         * @see #withCurrentTime() to create a new object that has been initialized
065         *      with the current time.
066         */
067        public InstantType() {
068                super();
069        }
070
071        /**
072         * Create a new DateTimeDt
073         */
074        public InstantType(Calendar theCalendar) {
075                super(theCalendar.getTime(), DEFAULT_PRECISION, theCalendar.getTimeZone());
076        }
077
078        /**
079         * Create a new instance using the given date, precision level, and time zone
080         * 
081         * @throws DataFormatException
082         *             If the specified precision is not allowed for this type
083         */
084        public InstantType(Date theDate, TemporalPrecisionEnum thePrecision, TimeZone theTimezone) {
085                super(theDate, thePrecision, theTimezone);
086        }
087
088
089        /**
090         * Create a new DateTimeDt using an existing value. <b>Use this constructor with caution</b>,
091         * as it may create more precision than warranted (since for example it is possible to pass in
092         * a DateTime with only a year, and this constructor will convert to an InstantDt with 
093         * milliseconds precision).
094         */
095        public InstantType(BaseDateTimeType theDateTime) {
096                // Do not call super(foo) here, we don't want to trigger a DataFormatException
097                setValue(theDateTime.getValue());
098                setPrecision(DEFAULT_PRECISION);
099                setTimeZone(theDateTime.getTimeZone());
100        }
101
102        /**
103         * Create a new DateTimeDt with the given date/time and {@link TemporalPrecisionEnum#MILLI} precision
104         */
105        public InstantType(Date theDate) {
106                super(theDate, DEFAULT_PRECISION, TimeZone.getDefault());
107        }
108
109        /**
110         * Constructor which accepts a date value and a precision value. Valid
111         * precisions values for this type are:
112         * <ul>
113         * <li>{@link TemporalPrecisionEnum#SECOND}
114         * <li>{@link TemporalPrecisionEnum#MILLI}
115         * </ul>
116         */
117        public InstantType(Date theDate, TemporalPrecisionEnum thePrecision) {
118                setValue(theDate);
119                setPrecision(thePrecision);
120                setTimeZone(TimeZone.getDefault());
121        }
122
123        /**
124         * Create a new InstantDt from a string value
125         * 
126         * @param theString
127         *            The string representation of the string. Must be in a valid
128         *            format according to the FHIR specification
129         * @throws DataFormatException
130         */
131        public InstantType(String theString) {
132                super(theString);
133        }
134
135        /**
136         * Invokes {@link Date#after(Date)} on the contained Date against the given
137         * date
138         * 
139         * @throws NullPointerException
140         *             If the {@link #getValue() contained Date} is null
141         */
142        public boolean after(Date theDate) {
143                return getValue().after(theDate);
144        }
145
146        /**
147         * Invokes {@link Date#before(Date)} on the contained Date against the given
148         * date
149         * 
150         * @throws NullPointerException
151         *             If the {@link #getValue() contained Date} is null
152         */
153        public boolean before(Date theDate) {
154                return getValue().before(theDate);
155        }
156
157        /**
158         * Sets the value of this instant to the current time (from the system
159         * clock) and the local/default timezone (as retrieved using
160         * {@link TimeZone#getDefault()}. This TimeZone is generally obtained from
161         * the underlying OS.
162         */
163        public void setToCurrentTimeInLocalTimeZone() {
164                setValue(new Date());
165                setTimeZone(TimeZone.getDefault());
166        }
167
168        @Override
169        boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
170                switch (thePrecision) {
171                case SECOND:
172                case MILLI:
173                        return true;
174                default:
175                        return false;
176                }
177        }
178
179        /**
180         * Factory method which creates a new InstantDt with millisecond precision and initializes it with the
181         * current time and the system local timezone.
182         */
183        public static InstantType withCurrentTime() {
184                return new InstantType(new Date(), TemporalPrecisionEnum.MILLI, TimeZone.getDefault());
185        }
186
187        /**
188         * Returns the default precision for this datatype
189         * 
190         * @see #DEFAULT_PRECISION
191         */
192        @Override
193        protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() {
194                return DEFAULT_PRECISION;
195        }
196
197
198        @Override
199        public InstantType copy() {
200                return new InstantType(getValueAsString());
201        }
202
203        /**
204         * Returns a new instance of DateTimeType with the current system time and MILLI precision and the system local time
205         * zone
206         */
207        public static InstantType now() {
208                return new InstantType(new Date(), TemporalPrecisionEnum.MILLI, TimeZone.getDefault());
209        }
210
211        /**
212         * Creates a new instance by parsing an HL7 v3 format date time string
213         */
214        public static InstantType parseV3(String theV3String) {
215                InstantType retVal = new InstantType();
216                retVal.setValueAsV3String(theV3String);
217                return retVal;
218        }
219
220        public String fhirType() {
221                return "instant";
222        }
223}