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