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.IBaseBooleanDatatype;
028
029@DatatypeDef(name = "boolean")
030public class BooleanDt extends BasePrimitive<Boolean> implements IBaseBooleanDatatype {
031
032        /**
033         * Constructor
034         */
035        public BooleanDt() {
036                super();
037        }
038
039        /**
040         * Constructor
041         */
042        @SimpleSetter
043        public BooleanDt(@SimpleSetter.Parameter(name = "theBoolean") boolean theBoolean) {
044                setValue(theBoolean);
045        }
046
047        @Override
048        protected Boolean parse(String theValue) {
049                if ("true".equals(theValue)) {
050                        return Boolean.TRUE;
051                } else if ("false".equals(theValue)) {
052                        return Boolean.FALSE;
053                } else {
054                        throw new DataFormatException(Msg.code(1872) + "Invalid boolean string: '" + theValue + "'");
055                }
056        }
057
058        @Override
059        protected String encode(Boolean theValue) {
060                if (Boolean.TRUE.equals(theValue)) {
061                        return "true";
062                }
063                return "false";
064        }
065}