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