001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import java.net.URL;
005import java.util.regex.Matcher;
006import java.util.regex.Pattern;
007
008/**
009 * Download URL pattern.
010 * @since 15784
011 */
012@FunctionalInterface
013public interface UrlPattern {
014    /**
015     * Returns the URL pattern.
016     * @return the URL pattern
017     */
018    String pattern();
019
020    /**
021     * Creates a matcher that will match the given input against this pattern.
022     * @param input The character sequence to be matched
023     * @return A new matcher for this pattern
024     */
025    default Matcher matcher(String input) {
026        return Pattern.compile(pattern()).matcher(input);
027    }
028
029    /**
030     * Attempts to match the given input against the pattern.
031     * @param input The character sequence to be matched
032     * @return {@code true} if the given input matches this pattern
033     */
034    default boolean matches(String input) {
035        return input != null && matcher(input).matches();
036    }
037
038    /**
039     * Attempts to match the given URL external form against the pattern.
040     * @param url URL to be matched
041     * @return {@code true} if the given URL matches this pattern
042     */
043    default boolean matches(URL url) {
044        return url != null && matches(url.toExternalForm());
045    }
046}