All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softicar.platform.common.string.regex.Patterns Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.string.regex;

import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * Provides auxiliary methods for patterns (as in {@link Pattern}).
 *
 * @author Alexander Schmidt
 */
public class Patterns {

	/**
	 * Determines whether the given {@link String} matches the given
	 * {@link Pattern}.
	 *
	 * @param input
	 *            the {@link String} to match (never null)
	 * @param pattern
	 *            the {@link Pattern} against which the given {@link String}
	 *            shall be matched (never null)
	 * @return true if the given {@link String} matches the given
	 *         {@link Pattern}; false otherwise
	 * @throws NullPointerException
	 *             if any of the given arguments is null
	 */
	public static boolean matches(String input, Pattern pattern) {

		Objects.requireNonNull(pattern);
		return anyMatch(input, pattern);
	}

	/**
	 * Determines whether the given {@link String} matches any of the given
	 * {@link Pattern} instances.
	 *
	 * @param input
	 *            the {@link String} to match (never null)
	 * @param patterns
	 *            the {@link Pattern} instances against which the given
	 *            {@link String} shall be matched (never null)
	 * @return true if the given {@link String} matches at least one of
	 *         the given {@link Pattern} instances; false otherwise, or
	 *         if no {@link Pattern} instances are given
	 * @throws NullPointerException
	 *             if any of the given arguments is null
	 */
	public static boolean anyMatch(String input, Pattern...patterns) {

		Objects.requireNonNull(input);
		Objects.requireNonNull(patterns);
		return anyMatch(input, Arrays.asList(patterns));
	}

	/**
	 * Determines whether the given {@link String} matches any of the given
	 * {@link Pattern} instances.
	 *
	 * @param input
	 *            the {@link String} to match (never null)
	 * @param patterns
	 *            the {@link Pattern} instances against which the given
	 *            {@link String} shall be matched (never null)
	 * @return true if the given {@link String} matches at least one of
	 *         the given {@link Pattern} instances; false otherwise, or
	 *         if no {@link Pattern} instances are given
	 * @throws NullPointerException
	 *             if any of the given arguments is null
	 */
	public static boolean anyMatch(String input, Collection patterns) {

		Objects.requireNonNull(input);
		Objects.requireNonNull(patterns);
		return patterns.stream().anyMatch(pattern -> pattern.matcher(input).matches());
	}

	/**
	 * Determines whether the given {@link String} matches none of the given
	 * {@link Pattern} instances.
	 *
	 * @param input
	 *            the {@link String} to match (never null)
	 * @param patterns
	 *            the {@link Pattern} instances against which the given
	 *            {@link String} shall be matched (never null)
	 * @return true if the given {@link String} does not match any of the
	 *         given {@link Pattern} instances, or if no {@link Pattern}
	 *         instances are given; false otherwise
	 * @throws NullPointerException
	 *             if any of the given arguments is null
	 */
	public static boolean noneMatch(String input, Pattern...patterns) {

		Objects.requireNonNull(input);
		Objects.requireNonNull(patterns);
		return noneMatch(input, Arrays.asList(patterns));
	}

	/**
	 * Determines whether the given {@link String} matches none of the given
	 * {@link Pattern} instances.
	 *
	 * @param input
	 *            the {@link String} to match (never null)
	 * @param patterns
	 *            the {@link Pattern} instances against which the given
	 *            {@link String} shall be matched (never null)
	 * @return true if the given {@link String} does not match any of the
	 *         given {@link Pattern} instances, or if no {@link Pattern}
	 *         instances are given; false otherwise
	 * @throws NullPointerException
	 *             if any of the given arguments is null
	 */
	public static boolean noneMatch(String input, Collection patterns) {

		Objects.requireNonNull(input);
		Objects.requireNonNull(patterns);
		return patterns.stream().noneMatch(pattern -> pattern.matcher(input).matches());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy