001/*
002 * Configurate
003 * Copyright (C) zml and Configurate contributors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.spongepowered.configurate.objectmapping.meta;
018
019import org.checkerframework.checker.regex.qual.Regex;
020
021import java.lang.annotation.Documented;
022import java.lang.annotation.ElementType;
023import java.lang.annotation.Retention;
024import java.lang.annotation.RetentionPolicy;
025import java.lang.annotation.Target;
026import java.util.regex.Pattern;
027
028/**
029 * Constrains a field value to ensure it matches the provided expression.
030 *
031 * <p>This constraint will always pass with an empty field. See {@link Required}
032 * to enforce a non-null value.</p>
033 *
034 * @since 4.0.0
035 */
036@Documented
037@Retention(RetentionPolicy.RUNTIME)
038@Target(ElementType.FIELD)
039public @interface Matches {
040
041    /**
042     * Pattern to test string value against.
043     *
044     * @return pattern to test against
045     * @since 4.0.0
046     */
047    @Regex String value();
048
049    /**
050     * Flags to pass to the compiled {@link Pattern}.
051     *
052     * @return the regex pattern parsing flags
053     * @since 4.1.0
054     * @see Pattern for the bitflags accepted here
055     */
056    int flags() default 0;
057
058    /**
059     * Message to throw in an exception when a match fails.
060     *
061     * <p>This message will be formatted as a MessageFormat with two
062     * parameters:</p>
063     * <ol start="0">
064     *     <li>the input string</li>
065     *     <li>the pattern being matched</li>
066     * </ol>
067     *
068     * @return message format.
069     * @since 4.0.0
070     */
071    String failureMessage() default "";
072
073}