001/*
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2025 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.util;
021
022import ca.uhn.fhir.i18n.Msg;
023import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
024import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
025import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
026
027import static org.apache.commons.lang3.StringUtils.isBlank;
028import static org.apache.commons.lang3.StringUtils.length;
029
030public class ValidateUtil {
031        /**
032         * Throws {@link IllegalArgumentException} if theValue is less than or equal to theMinimum
033         */
034        public static void isGreaterThan(long theValue, long theMinimum, String theMessage) {
035                if (theValue <= theMinimum) {
036                        throw new IllegalArgumentException(Msg.code(1762) + theMessage);
037                }
038        }
039
040        /**
041         * Throws {@link IllegalArgumentException} if theValue is less than theMinimum
042         */
043        public static void isGreaterThanOrEqualTo(long theValue, long theMinimum, String theMessage) {
044                if (theValue < theMinimum) {
045                        throw new IllegalArgumentException(Msg.code(1763) + theMessage);
046                }
047        }
048
049        public static void isNotBlankOrThrowIllegalArgument(String theString, String theMessage) {
050                if (isBlank(theString)) {
051                        throw new IllegalArgumentException(Msg.code(1764) + theMessage);
052                }
053        }
054
055        public static void isNotBlankOrThrowInvalidRequest(String theString, String theMessage) {
056                if (isBlank(theString)) {
057                        throw new InvalidRequestException(Msg.code(1765) + theMessage);
058                }
059        }
060
061        public static void isNotBlankOrThrowUnprocessableEntity(String theString, String theMessage) {
062                if (isBlank(theString)) {
063                        throw new UnprocessableEntityException(Msg.code(1766) + theMessage);
064                }
065        }
066
067        public static void isNotNullOrThrowUnprocessableEntity(Object theObject, String theMessage, Object... theValues) {
068                if (theObject == null) {
069                        throw new UnprocessableEntityException(Msg.code(1767) + String.format(theMessage, theValues));
070                }
071        }
072
073        public static void isNotTooLongOrThrowIllegalArgument(String theString, int theMaxLength, String theMessage) {
074                if (length(theString) > theMaxLength) {
075                        throw new IllegalArgumentException(Msg.code(1768) + theMessage);
076                }
077        }
078
079        public static void isTrueOrThrowInvalidRequest(boolean theSuccess, String theMessage, Object... theValues) {
080                if (!theSuccess) {
081                        throw new InvalidRequestException(Msg.code(1769) + String.format(theMessage, theValues));
082                }
083        }
084
085        public static void isTrueOrThrowResourceNotFound(boolean theSuccess, String theMessage, Object... theValues) {
086                if (!theSuccess) {
087                        throw new ResourceNotFoundException(Msg.code(2494) + String.format(theMessage, theValues));
088                }
089        }
090
091        public static void exactlyOneNotNullOrThrowInvalidRequestException(Object[] theObjects, String theMessage) {
092                int count = 0;
093                for (Object next : theObjects) {
094                        if (next != null) {
095                                count++;
096                        }
097                }
098                if (count != 1) {
099                        throw new InvalidRequestException(Msg.code(1770) + theMessage);
100                }
101        }
102}