001/**
002 * Copyright (C) 2006-2020 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.api.configuration.condition;
017
018import static java.lang.annotation.ElementType.FIELD;
019import static java.lang.annotation.ElementType.PARAMETER;
020import static java.lang.annotation.RetentionPolicy.RUNTIME;
021
022import java.lang.annotation.Repeatable;
023import java.lang.annotation.Retention;
024import java.lang.annotation.Target;
025import java.util.function.Predicate;
026
027import org.talend.sdk.component.api.configuration.condition.meta.Condition;
028import org.talend.sdk.component.api.meta.Documentation;
029
030@Repeatable(ActiveIfs.class)
031@Documentation("If the evaluation of the element at the location matches value then the element is considered active, "
032        + "otherwise it is deactivated.")
033@Target({ FIELD, PARAMETER })
034@Retention(RUNTIME)
035@Condition("if")
036public @interface ActiveIf {
037
038    /**
039     * @return the path to evaluate.
040     */
041    String target();
042
043    /**
044     * @return the value to compare with the evaluated path.
045     */
046    String[] value();
047
048    /**
049     * Should the condition deduced from the target comparison to the value(s) be compared to true or false.
050     * It is equivalent to see target and value defining a {@link java.util.function.Predicate} and this toggle
051     * calling {@link Predicate#negate()}.
052     *
053     * @return if set to true it will be compared to false (reversed), otherwise it is compared to true.
054     */
055    boolean negate() default false;
056
057    /**
058     * @return the strategy to use to evaluate the value compared to value array.
059     */
060    EvaluationStrategy evaluationStrategy() default EvaluationStrategy.DEFAULT;
061
062    EvaluationStrategyOption[] evaluationStrategyOptions() default {};
063
064    /**
065     * Allows to pass custom options to the evaluation strategy.
066     * The supported ones are:
067     * <ul>
068     * <li>
069     * For <code>CONTAINS</code> strategy:
070     * <ul>
071     * <li>lowercase: [true|false]</li>
072     * </ul>
073     * </li>
074     * </ul>
075     */
076    @Target({})
077    @Retention(RUNTIME)
078    @interface EvaluationStrategyOption {
079
080        /**
081         * @return option name;
082         */
083        String name();
084
085        /**
086         * @return option value.
087         */
088        String value() default "true";
089    }
090
091    enum EvaluationStrategy {
092        /**
093         * Use the raw value.
094         */
095        DEFAULT,
096
097        /**
098         * For an array or string, evaluate the size of the value instead of the value itself.
099         */
100        LENGTH,
101
102        /**
103         * Check if a string or list of string contains a value.
104         */
105        CONTAINS
106    }
107}