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.api;
021
022import com.fasterxml.jackson.annotation.JsonProperty;
023import org.apache.commons.lang3.Validate;
024
025import java.util.HashMap;
026import java.util.Map;
027
028import static org.apache.commons.lang3.StringUtils.isNotBlank;
029
030public abstract class BaseBatchJobParameters implements IModelJson {
031        /**
032         * A serializable map of key-value pairs that can be
033         * added to any extending job.
034         */
035        @JsonProperty("userData")
036        private Map<String, Object> myUserData;
037
038        public Map<String, Object> getUserData() {
039                if (myUserData == null) {
040                        myUserData = new HashMap<>();
041                }
042                return myUserData;
043        }
044
045        public void setUserData(String theKey, Object theValue) {
046                Validate.isTrue(isNotBlank(theKey), "Invalid key; key must be non-empty, non-null.");
047                if (theValue == null) {
048                        getUserData().remove(theKey);
049                } else {
050                        Validate.isTrue(
051                                        validateValue(theValue),
052                                        String.format(
053                                                        "Invalid data type provided %s", theValue.getClass().getName()));
054                        getUserData().put(theKey, theValue);
055                }
056        }
057
058        private boolean validateValue(Object theValue) {
059                if (theValue instanceof Boolean) {
060                        return true;
061                }
062                if (theValue instanceof Number) {
063                        return true;
064                }
065                if (theValue instanceof String) {
066                        return true;
067                }
068                return false;
069        }
070}