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 ca.uhn.fhir.model.api.TemporalPrecisionEnum; 035import ca.uhn.fhir.model.api.annotation.DatatypeDef; 036 037import java.util.Calendar; 038import java.util.Date; 039import java.util.TimeZone; 040import java.util.zip.DataFormatException; 041 042/** 043 * Represents a FHIR instant datatype. Valid precisions values for this type are: 044 * <ul> 045 * <li>{@link TemporalPrecisionEnum#SECOND} 046 * <li>{@link TemporalPrecisionEnum#MILLI} 047 * </ul> 048 */ 049@DatatypeDef(name="instant") 050public class InstantType extends BaseDateTimeType { 051 052 /** 053 * The default precision for this type 054 */ 055 public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.MILLI; 056 private static final long serialVersionUID = 3L; 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 @Override 158 public InstantType copy() { 159 InstantType retVal = new InstantType(); 160 retVal.setValueAsString(getValueAsString()); 161 return retVal; 162 } 163 164 public String fhirType() { 165 return "instant"; 166 } 167 168 /** 169 * Returns the default precision for this datatype 170 * 171 * @see #DEFAULT_PRECISION 172 */ 173 @Override 174 protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() { 175 return DEFAULT_PRECISION; 176 } 177 178 @Override 179 boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) { 180 switch (thePrecision) { 181 case SECOND: 182 case MILLI: 183 return true; 184 default: 185 return false; 186 } 187 } 188 189 /** 190 * Sets the value of this instant to the current time (from the system 191 * clock) and the local/default timezone (as retrieved using 192 * {@link TimeZone#getDefault()}. This TimeZone is generally obtained from 193 * the underlying OS. 194 */ 195 public void setToCurrentTimeInLocalTimeZone() { 196 setValue(new Date()); 197 setTimeZone(TimeZone.getDefault()); 198 } 199 200 /** 201 * Returns a new instance of DateTimeType with the current system time and MILLI precision and the system local time 202 * zone 203 */ 204 public static InstantType now() { 205 return new InstantType(new Date(), TemporalPrecisionEnum.MILLI, TimeZone.getDefault()); 206 } 207 208 /** 209 * Creates a new instance by parsing an HL7 v3 format date time string 210 */ 211 public static InstantType parseV3(String theV3String) { 212 InstantType retVal = new InstantType(); 213 retVal.setValueAsV3String(theV3String); 214 return retVal; 215 } 216 217 /** 218 * Factory method which creates a new InstantDt with millisecond precision and initializes it with the 219 * current time and the system local timezone. 220 */ 221 public static InstantType withCurrentTime() { 222 return new InstantType(new Date(), TemporalPrecisionEnum.MILLI, TimeZone.getDefault()); 223 } 224}