001package org.hl7.fhir.dstu2.model;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032/**
033 * Primitive type "date" in FHIR: any day in a gregorian calendar
034 */
035
036import java.util.Date;
037import java.util.TimeZone;
038
039import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
040import ca.uhn.fhir.model.api.annotation.DatatypeDef;
041
042/**
043 * Represents a FHIR date datatype. Valid precisions values for this type are:
044 * <ul>
045 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR}
046 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH}
047 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY}
048 * </ul>
049 */
050@DatatypeDef(name = "date")
051public class DateType extends BaseDateTimeType {
052
053  private static final long serialVersionUID = 3L;
054
055  /**
056   * The default precision for this type
057   */
058  public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.DAY;
059
060  /**
061   * Constructor
062   */
063  public DateType() {
064    super();
065  }
066
067  /**
068   * Constructor which accepts a date value and uses the
069   * {@link #DEFAULT_PRECISION} for this type
070   */
071  public DateType(Date theDate) {
072    super(theDate, DEFAULT_PRECISION);
073  }
074
075  /**
076   * Constructor which accepts a date value and a precision value. Valid
077   * precisions values for this type are:
078   * <ul>
079   * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR}
080   * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH}
081   * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY}
082   * </ul>
083   *
084   * @throws ca.uhn.fhir.parser.DataFormatException If the specified precision is
085   *                                                not allowed for this type
086   */
087  public DateType(Date theDate, TemporalPrecisionEnum thePrecision) {
088    super(theDate, thePrecision);
089  }
090
091  /**
092   * Constructor which accepts a date as a string in FHIR format
093   *
094   * @throws ca.uhn.fhir.parser.DataFormatException If the precision in the date
095   *                                                string is not allowed for this
096   *                                                type
097   */
098  public DateType(String theDate) {
099    super(theDate);
100  }
101
102  @Override
103  boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
104    switch (thePrecision) {
105    case YEAR:
106    case MONTH:
107    case DAY:
108      return true;
109    default:
110      return false;
111    }
112  }
113
114  /**
115   * Returns the default precision for this datatype
116   *
117   * @see #DEFAULT_PRECISION
118   */
119  @Override
120  protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() {
121    return DEFAULT_PRECISION;
122  }
123
124  @Override
125  public DateType copy() {
126    return new DateType(getValue());
127  }
128
129  public static InstantType today() {
130    return new InstantType(new Date(), TemporalPrecisionEnum.DAY, TimeZone.getDefault());
131  }
132
133  /**
134   * Creates a new instance by parsing an HL7 v3 format date time string
135   */
136  public static DateType parseV3(String theV3String) {
137    DateType retVal = new DateType();
138    retVal.setValueAsV3String(theV3String);
139    return retVal;
140  }
141
142  public String fhirType() {
143    return "date";
144  }
145}