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
030package org.hl7.fhir.dstu2.model;
031
032/*-
033 * #%L
034 * org.hl7.fhir.dstu2
035 * %%
036 * Copyright (C) 2014 - 2019 Health Level 7
037 * %%
038 * Licensed under the Apache License, Version 2.0 (the "License");
039 * you may not use this file except in compliance with the License.
040 * You may obtain a copy of the License at
041 * 
042 *      http://www.apache.org/licenses/LICENSE-2.0
043 * 
044 * Unless required by applicable law or agreed to in writing, software
045 * distributed under the License is distributed on an "AS IS" BASIS,
046 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
047 * See the License for the specific language governing permissions and
048 * limitations under the License.
049 * #L%
050 */
051
052
053/**
054 * Primitive type "date" in FHIR: any day in a gregorian calendar
055 */
056
057import java.util.Date;
058import java.util.TimeZone;
059
060import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
061import ca.uhn.fhir.model.api.annotation.DatatypeDef;
062
063/**
064 * Represents a FHIR date datatype. Valid precisions values for this type are:
065 * <ul>
066 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR}
067 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH}
068 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY}
069 * </ul>
070 */
071@DatatypeDef(name = "date")
072public class DateType extends BaseDateTimeType {
073
074        private static final long serialVersionUID = 3L;
075        
076        /**
077         * The default precision for this type
078         */
079        public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.DAY;
080
081        /**
082         * Constructor
083         */
084        public DateType() {
085                super();
086        }
087
088        /**
089         * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type
090         */
091        public DateType(Date theDate) {
092                super(theDate, DEFAULT_PRECISION);
093        }
094
095        /**
096         * Constructor which accepts a date value and a precision value. Valid precisions values for this type are:
097         * <ul>
098         * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR}
099         * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH}
100         * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY}
101         * </ul>
102         *
103         * @throws ca.uhn.fhir.parser.DataFormatException
104         *             If the specified precision is not allowed for this type
105         */
106        public DateType(Date theDate, TemporalPrecisionEnum thePrecision) {
107                super(theDate, thePrecision);
108        }
109
110        /**
111         * Constructor which accepts a date as a string in FHIR format
112         *
113         * @throws ca.uhn.fhir.parser.DataFormatException
114         *             If the precision in the date string is not allowed for this type
115         */
116        public DateType(String theDate) {
117                super(theDate);
118        }
119
120        @Override
121        boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
122                switch (thePrecision) {
123                        case YEAR:
124                        case MONTH:
125                        case DAY:
126                                return true;
127                        default:
128                                return false;
129                }
130        }
131
132        /**
133         * Returns the default precision for this datatype
134         *
135         * @see #DEFAULT_PRECISION
136         */
137        @Override
138        protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() {
139                return DEFAULT_PRECISION;
140        }
141
142        @Override
143        public DateType copy() {
144                return new DateType(getValue());
145        }
146        
147        public static InstantType today() {
148                return new InstantType(new Date(), TemporalPrecisionEnum.DAY, TimeZone.getDefault());
149        }
150
151        /**
152         * Creates a new instance by parsing an HL7 v3 format date time string
153         */
154        public static DateType parseV3(String theV3String) {
155                DateType retVal = new DateType();
156                retVal.setValueAsV3String(theV3String);
157                return retVal;
158        }
159
160        public String fhirType() {
161                return "date";          
162        }
163}