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