001package ball.util.ant.types;
002/*-
003 * ##########################################################################
004 * Utilities
005 * %%
006 * Copyright (C) 2008 - 2022 Allen D. Ball
007 * %%
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *      http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 * ##########################################################################
020 */
021import lombok.NoArgsConstructor;
022import lombok.ToString;
023import org.apache.tools.ant.Project;
024
025import static org.apache.commons.lang3.StringUtils.EMPTY;
026import static org.apache.commons.lang3.StringUtils.isNotEmpty;
027
028/**
029 * Class to provide a {@link String} base text type for
030 * {@link.uri http://ant.apache.org/ Ant} {@link org.apache.tools.ant.Task}
031 * implementations.  Support "if" and "unless" property predicates.
032 *
033 * {@bean.info}
034 *
035 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
036 */
037@NoArgsConstructor @ToString
038public class OptionalTextType {
039    private String text = null;
040    private String ifP = null;
041    private String unlessP = null;
042
043    public void addText(String text) {
044        this.text = (isNotEmpty(this.text) ? this.text : EMPTY) + text;
045    }
046
047    /**
048     * Sets the "if" condition to test on execution.  If the named property
049     * is set, the {@link OptionalTextType} should be included.
050     *
051     * @param   ifP             The property condition to test on execution.
052     *                          If the value is {@code null} no "if" test
053     *                          will not be performed.
054     */
055    public void setIf(String ifP) { this.ifP = ifP; }
056    public String getIf() { return ifP; }
057
058    /**
059     * Sets the "unless" condition to test on execution.  If the named
060     * property is set, the {@link OptionalTextType} should not be included.
061     *
062     * @param   unlessP         The property condition to test on execution.
063     *                          If the value is {@code null} no "unlessP"
064     *                          test will not be performed.
065     */
066    public void setUnless(String unlessP) { this.unlessP = unlessP; }
067    public String getUnless() { return unlessP; }
068
069    /**
070     * Method to determine if the "if" and "unless" tests have been
071     * satisfied.
072     *
073     * @param       project The {@link Project}.
074     *
075     * @return      {@code true} if the "if" and "unless" tests have
076     *              been satisfied; {@code false} otherwise.
077     */
078    public boolean isActive(Project project) {
079        return ((getIf() == null || project.getProperty(getIf()) != null)
080                && (getUnless() == null || project.getProperty(getUnless()) == null));
081    }
082}