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.*;
035import java.util.zip.DataFormatException;
036
037import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
038import ca.uhn.fhir.model.api.annotation.DatatypeDef;
039
040/**
041 * Represents a FHIR instant datatype. Valid precisions values for this type are:
042 * <ul>
043 * <li>{@link TemporalPrecisionEnum#SECOND}
044 * <li>{@link TemporalPrecisionEnum#MILLI}
045 * </ul>
046 */
047@DatatypeDef(name="instant")
048public class InstantType extends BaseDateTimeType {
049
050        private static final long serialVersionUID = 3L;
051        
052        /**
053         * The default precision for this type
054         */
055        public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.MILLI;
056
057        /**
058         * Constructor which creates an InstantDt with <b>no timne value</b>. Note
059         * that unlike the default constructor for the Java {@link Date} or
060         * {@link Calendar} objects, this constructor does not initialize the object
061         * with the current time.
062         * 
063         * @see #withCurrentTime() to create a new object that has been initialized
064         *      with the current time.
065         */
066        public InstantType() {
067                super();
068        }
069
070        /**
071         * Create a new DateTimeDt
072         */
073        public InstantType(Calendar theCalendar) {
074                super(theCalendar.getTime(), DEFAULT_PRECISION, theCalendar.getTimeZone());
075        }
076
077        /**
078         * Create a new instance using the given date, precision level, and time zone
079         * 
080         * @throws DataFormatException
081         *             If the specified precision is not allowed for this type
082         */
083        public InstantType(Date theDate, TemporalPrecisionEnum thePrecision, TimeZone theTimezone) {
084                super(theDate, thePrecision, theTimezone);
085        }
086
087
088        /**
089         * Create a new DateTimeDt using an existing value. <b>Use this constructor with caution</b>,
090         * as it may create more precision than warranted (since for example it is possible to pass in
091         * a DateTime with only a year, and this constructor will convert to an InstantDt with 
092         * milliseconds precision).
093         */
094        public InstantType(BaseDateTimeType theDateTime) {
095                // Do not call super(foo) here, we don't want to trigger a DataFormatException
096                setValue(theDateTime.getValue());
097                setPrecision(DEFAULT_PRECISION);
098                setTimeZone(theDateTime.getTimeZone());
099        }
100
101        /**
102         * Create a new DateTimeDt with the given date/time and {@link TemporalPrecisionEnum#MILLI} precision
103         */
104        public InstantType(Date theDate) {
105                super(theDate, DEFAULT_PRECISION, TimeZone.getDefault());
106        }
107
108        /**
109         * Constructor which accepts a date value and a precision value. Valid
110         * precisions values for this type are:
111         * <ul>
112         * <li>{@link TemporalPrecisionEnum#SECOND}
113         * <li>{@link TemporalPrecisionEnum#MILLI}
114         * </ul>
115         */
116        public InstantType(Date theDate, TemporalPrecisionEnum thePrecision) {
117                setValue(theDate);
118                setPrecision(thePrecision);
119                setTimeZone(TimeZone.getDefault());
120        }
121
122        /**
123         * Create a new InstantDt from a string value
124         * 
125         * @param theString
126         *            The string representation of the string. Must be in a valid
127         *            format according to the FHIR specification
128         * @throws DataFormatException
129         */
130        public InstantType(String theString) {
131                super(theString);
132        }
133
134        /**
135         * Invokes {@link Date#after(Date)} on the contained Date against the given
136         * date
137         * 
138         * @throws NullPointerException
139         *             If the {@link #getValue() contained Date} is null
140         */
141        public boolean after(Date theDate) {
142                return getValue().after(theDate);
143        }
144
145        /**
146         * Invokes {@link Date#before(Date)} on the contained Date against the given
147         * date
148         * 
149         * @throws NullPointerException
150         *             If the {@link #getValue() contained Date} is null
151         */
152        public boolean before(Date theDate) {
153                return getValue().before(theDate);
154        }
155
156        /**
157         * Sets the value of this instant to the current time (from the system
158         * clock) and the local/default timezone (as retrieved using
159         * {@link TimeZone#getDefault()}. This TimeZone is generally obtained from
160         * the underlying OS.
161         */
162        public void setToCurrentTimeInLocalTimeZone() {
163                setValue(new Date());
164                setTimeZone(TimeZone.getDefault());
165        }
166
167        @Override
168        boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
169                switch (thePrecision) {
170                case SECOND:
171                case MILLI:
172                        return true;
173                default:
174                        return false;
175                }
176        }
177
178        /**
179         * Factory method which creates a new InstantDt with millisecond precision and initializes it with the
180         * current time and the system local timezone.
181         */
182        public static InstantType withCurrentTime() {
183                return new InstantType(new Date(), TemporalPrecisionEnum.MILLI, TimeZone.getDefault());
184        }
185
186        /**
187         * Returns the default precision for this datatype
188         * 
189         * @see #DEFAULT_PRECISION
190         */
191        @Override
192        protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() {
193                return DEFAULT_PRECISION;
194        }
195
196
197        @Override
198        public InstantType copy() {
199                return new InstantType(getValueAsString());
200        }
201
202        /**
203         * Returns a new instance of DateTimeType with the current system time and MILLI precision and the system local time
204         * zone
205         */
206        public static InstantType now() {
207                return new InstantType(new Date(), TemporalPrecisionEnum.MILLI, TimeZone.getDefault());
208        }
209
210        /**
211         * Creates a new instance by parsing an HL7 v3 format date time string
212         */
213        public static InstantType parseV3(String theV3String) {
214                InstantType retVal = new InstantType();
215                retVal.setValueAsV3String(theV3String);
216                return retVal;
217        }
218
219        public String fhirType() {
220                return "instant";
221        }
222}