001/*
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2024 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.primitive;
021
022import ca.uhn.fhir.i18n.Msg;
023import ca.uhn.fhir.model.api.BasePrimitive;
024import ca.uhn.fhir.model.api.annotation.DatatypeDef;
025import ca.uhn.fhir.model.api.annotation.SimpleSetter;
026import ca.uhn.fhir.parser.DataFormatException;
027import org.hl7.fhir.instance.model.api.IBaseIntegerDatatype;
028
029@DatatypeDef(name = "integer")
030public class IntegerDt extends BasePrimitive<Integer> implements IBaseIntegerDatatype {
031
032        /**
033         * Constructor
034         */
035        public IntegerDt() {
036                super();
037        }
038
039        /**
040         * Constructor
041         */
042        @SimpleSetter
043        public IntegerDt(@SimpleSetter.Parameter(name = "theInteger") int theInteger) {
044                setValue(theInteger);
045        }
046
047        /**
048         * Constructor
049         *
050         * @param theIntegerAsString
051         *            A string representation of an integer
052         * @throws DataFormatException
053         *             If the string is not a valid integer representation
054         */
055        public IntegerDt(String theIntegerAsString) {
056                setValueAsString(theIntegerAsString);
057        }
058
059        @Override
060        protected Integer parse(String theValue) {
061                try {
062                        return Integer.parseInt(theValue);
063                } catch (NumberFormatException e) {
064                        throw new DataFormatException(Msg.code(1873) + e);
065                }
066        }
067
068        @Override
069        protected String encode(Integer theValue) {
070                return Integer.toString(theValue);
071        }
072}