001package ca.uhn.fhir.util;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
024import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
025
026import static org.apache.commons.lang3.StringUtils.*;
027
028public class ValidateUtil {
029
030        /**
031         * Throws {@link IllegalArgumentException} if theValue is <= theMinimum
032         */
033        public static void isGreaterThan(long theValue, long theMinimum, String theMessage) {
034                if (theValue <= theMinimum) {
035                        throw new IllegalArgumentException(theMessage);
036                }
037        }
038
039        /**
040         * Throws {@link IllegalArgumentException} if theValue is < theMinimum
041         */
042        public static void isGreaterThanOrEqualTo(long theValue, long theMinimum, String theMessage) {
043                if (theValue < theMinimum) {
044                        throw new IllegalArgumentException(theMessage);
045                }
046        }
047
048        public static void isNotBlankOrThrowIllegalArgument(String theString, String theMessage) {
049                if (isBlank(theString)) {
050                        throw new IllegalArgumentException(theMessage);
051                }
052        }
053
054        public static void isNotBlankOrThrowInvalidRequest(String theString, String theMessage) {
055                if (isBlank(theString)) {
056                        throw new InvalidRequestException(theMessage);
057                }
058        }
059
060        public static void isNotBlankOrThrowUnprocessableEntity(String theString, String theMessage) {
061                if (isBlank(theString)) {
062                        throw new UnprocessableEntityException(theMessage);
063                }
064        }
065
066        public static void isNotNullOrThrowUnprocessableEntity(Object theObject, String theMessage, Object... theValues) {
067                if (theObject == null) {
068                        throw new UnprocessableEntityException(String.format(theMessage, theValues));
069                }
070        }
071
072        public static void isNotTooLongOrThrowIllegalArgument(String theString, int theMaxLength, String theMessage) {
073                if (length(theString) > theMaxLength) {
074                        throw new IllegalArgumentException(theMessage);
075                }
076        }
077
078        public static void isTrueOrThrowInvalidRequest(boolean theSuccess, String theMessage, Object... theValues) {
079                if (!theSuccess) {
080                        throw new InvalidRequestException(String.format(theMessage, theValues));
081                }
082        }
083
084        public static void exactlyOneNotNullOrThrowInvalidRequestException(Object[] theObjects, String theMessage) {
085                int count = 0;
086                for (Object next : theObjects) {
087                        if (next != null) {
088                                count++;
089                        }
090                }
091                if (count != 1) {
092                        throw new InvalidRequestException(theMessage);
093                }
094        }
095
096}