This is an automated email from the ASF dual-hosted git repository. khmarbaise pushed a commit to branch MNG-6825 in repository https://gitbox.apache.org/repos/asf/maven.git
commit 833e61fd91688c0cc112f401c90ac128d7c445eb Author: Karl Heinz Marbaise <khmarba...@apache.org> AuthorDate: Mon Nov 25 20:13:38 2019 +0100 First step. --- maven-artifact/pom.xml | 4 +- maven-utils/pom.xml | 43 +++++++ .../java/org/apache/maven/utils/Precondition.java | 125 +++++++++++++++++++++ pom.xml | 1 + 4 files changed, 171 insertions(+), 2 deletions(-) diff --git a/maven-artifact/pom.xml b/maven-artifact/pom.xml index 336505b..9dd4fe9 100644 --- a/maven-artifact/pom.xml +++ b/maven-artifact/pom.xml @@ -38,8 +38,8 @@ under the License. <artifactId>plexus-utils</artifactId> </dependency> <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-lang3</artifactId> + <groupId>org.apache.maven</groupId> + <artifactId>maven-utils</artifactId> </dependency> </dependencies> diff --git a/maven-utils/pom.xml b/maven-utils/pom.xml new file mode 100644 index 0000000..21e066d --- /dev/null +++ b/maven-utils/pom.xml @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.maven</groupId> + <artifactId>maven</artifactId> + <version>3.6.3-SNAPSHOT</version> + </parent> + + <artifactId>maven-utils</artifactId> + + <name>Maven Utils</name> + + <dependencies> + <dependency> + <groupId>org.apiguardian</groupId> + <artifactId>apiguardian-api</artifactId> + <version>1.1.0</version> + </dependency> + </dependencies> +</project> diff --git a/maven-utils/src/main/java/org/apache/maven/utils/Precondition.java b/maven-utils/src/main/java/org/apache/maven/utils/Precondition.java new file mode 100644 index 0000000..73b50a1 --- /dev/null +++ b/maven-utils/src/main/java/org/apache/maven/utils/Precondition.java @@ -0,0 +1,125 @@ +package org.apache.maven.utils; + +import com.sun.tools.javac.util.StringUtils; +import org.apiguardian.api.API; + +import java.util.List; + +import static org.apiguardian.api.API.Status.INTERNAL; + +/** + * Convenient utility methods for assertion of different conditions. + * + * @author Karl Heinz Marbaise + */ +@API( status = INTERNAL, since = "3.6.4" ) +public final class Precondition +{ + private Precondition() + { + // no-op + } + + public static boolean notBlank(String str, String message) + { + for ( int i = 0; i < str.length(); i++ ) + { + if ( !Character.isWhitespace( str.charAt( i ) ) ) + { + return false; + } + } + throw new IllegalArgumentException( message ); + } + + /** + * assert that the given {@code obj} is not {@code null}. + * + * @param obj The instance which should be not {@code null}. + * @param message The message will be part of the exception. + * @return The supplied object as convenient. + */ + public static < T > T requireNotNull(T obj, String message) + { + if ( obj == null ) + throw new IllegalArgumentException( message ); + return obj; + } + + /** + * assert that the given {@code List<T>} is not {@code empty}. + * + * @param obj The instance which should be not {@code empty}. + * @param message The message will be part of the exception. + * @return The supplied object as convenient. + */ + public static < T > List< T > requireNotEmpty(List< T > obj, String message) + { + if ( obj.isEmpty() ) + { + throw new IllegalArgumentException( message ); + } + return obj; + } + + /** + * assert that the given {@code longValue} is greater than {@code 0}. + * + * @param longValue The instance which should be not {@code null} + * and has to be greater than {@code 0}. + * @param message The message will be part of the exception. + * @return The supplied object as convenient. + */ + public static Long requireGreaterThanZero(Long longValue, String message) + { + if ( longValue == null ) + { + throw new IllegalArgumentException( message ); + } + + if ( longValue <= 0 ) + { + throw new IllegalArgumentException( message ); + } + return longValue; + } + + /** + * assert that the given {@code integerValue} is greater than {@code 0}. + * + * @param integerValue The instance which should be not {@code null} + * and has to be greater than {@code 0}. + * @param message The message will be part of the exception. + * @return The supplied object as convenient. + */ + public static Integer requireGreaterThanZero(Integer integerValue, String message) + { + if ( integerValue == null ) + { + throw new IllegalArgumentException( message ); + } + + if ( integerValue <= 0 ) + { + throw new IllegalArgumentException( message ); + } + return integerValue; + } + + /** + * assert that the given {@code str} is not {@code null} and not {@code empty}. + * + * @param str The str which should not be {@code null} and not be empty. + * @param message The message for the exception in case of {@code null}. + * @return The supplied object as convenient. + */ + public static String requireNotEmpty(String str, String message) + { + requireNotNull( str, message ); + if ( StringUtils.isBlank( str ) ) + { + throw new IllegalArgumentException( message ); + } + return str; + } + diff --git a/pom.xml b/pom.xml index ef2764d..c9ec5a3 100644 --- a/pom.xml +++ b/pom.xml @@ -89,6 +89,7 @@ under the License. <module>maven-settings</module> <module>maven-settings-builder</module> <module>maven-artifact</module> + <module>maven-utils</module> <module>maven-resolver-provider</module> <module>maven-repository-metadata</module> <module>maven-slf4j-provider</module>