001package ca.uhn.fhir.util;
002
003import ca.uhn.fhir.i18n.Msg;
004import org.apache.commons.lang3.StringUtils;
005
006import java.util.Objects;
007import java.util.Optional;
008
009/*
010 * #%L
011 * HAPI FHIR - Core Library
012 * %%
013 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
014 * %%
015 * Licensed under the Apache License, Version 2.0 (the "License");
016 * you may not use this file except in compliance with the License.
017 * You may obtain a copy of the License at
018 *
019 *      http://www.apache.org/licenses/LICENSE-2.0
020 *
021 * Unless required by applicable law or agreed to in writing, software
022 * distributed under the License is distributed on an "AS IS" BASIS,
023 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
024 * See the License for the specific language governing permissions and
025 * limitations under the License.
026 * #L%
027 */
028
029public class ObjectUtil {
030
031        // hide
032        private ObjectUtil() {}
033
034        /**
035         * @deprecated Just use Objects.equals() instead;
036         */
037        @Deprecated(since = "6.2")
038        public static boolean equals(Object object1, Object object2) {
039                return Objects.equals(object1, object2);
040        }
041        
042        public static <T> T requireNonNull(T obj, String message) {
043        if (obj == null)
044            throw new NullPointerException(Msg.code(1776) + message);
045        return obj;
046    }
047
048        public static void requireNotEmpty(String str, String message) {
049                if (StringUtils.isBlank(str)) {
050                        throw new IllegalArgumentException(Msg.code(1777) + message);
051                }
052        }
053
054        /**
055         * Cast the object to the type using Optional.
056         * Useful for streaming with flatMap.
057         * @param theObject any object
058         * @param theClass the class to check instanceof
059         * @return Optional present if theObject is of type theClass
060         */
061        @SuppressWarnings("unchecked")
062        public static <T> Optional<T> castIfInstanceof(Object theObject, Class<T> theClass) {
063                if (theClass.isInstance(theObject)) {
064                        return Optional.of((T) theObject);
065                } else {
066                        return Optional.empty();
067                }
068        }
069        
070}