001/*
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.model.api;
021
022import java.util.Calendar;
023import java.util.Date;
024
025import org.apache.commons.lang3.time.DateUtils;
026
027public enum TemporalPrecisionEnum {
028
029        YEAR(Calendar.YEAR) {
030                @Override
031                public Date add(Date theInput, int theAmount) {
032                        return DateUtils.addYears(theInput, theAmount);
033                }
034        },
035        
036        MONTH(Calendar.MONTH) {
037                @Override
038                public Date add(Date theInput, int theAmount) {
039                        return DateUtils.addMonths(theInput, theAmount);
040                }
041        },
042        DAY(Calendar.DATE) {
043                @Override
044                public Date add(Date theInput, int theAmount) {
045                        return DateUtils.addDays(theInput, theAmount);
046                }
047        },
048        MINUTE(Calendar.MINUTE) {
049                @Override
050                public Date add(Date theInput, int theAmount) {
051                        return DateUtils.addMinutes(theInput, theAmount);
052                }
053                
054        },
055        SECOND(Calendar.SECOND) {
056                @Override
057                public Date add(Date theInput, int theAmount) {
058                        return DateUtils.addSeconds(theInput, theAmount);
059                }
060        },
061        
062        MILLI(Calendar.MILLISECOND) {
063                @Override
064                public Date add(Date theInput, int theAmount) {
065                        return DateUtils.addMilliseconds(theInput, theAmount);
066                }
067        }, 
068        
069        ;
070        
071        private int myCalendarConstant;
072
073        TemporalPrecisionEnum(int theCalendarConstant) {
074                myCalendarConstant = theCalendarConstant;
075        }
076
077        public abstract Date add(Date theInput, int theAmount);
078        
079        public int getCalendarConstant() {
080                return myCalendarConstant;
081        }
082
083        /**
084         * Given the standard string representation - YYYY-DD-MMTHH:NN:SS.SSS - how long is the string for the stated precision?
085         */
086        public int stringLength() {
087                switch (this) {
088                        case YEAR: return 4;
089                        case MONTH: return 7;
090                        case DAY: return 10;
091                        case MINUTE: return 16;
092                        case SECOND: return 19;
093                        case MILLI: return 23;
094                }
095                return 0; // ??
096        }
097}