001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package org.basepom.mojo.propertyhelper.definitions;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018
019import org.basepom.mojo.propertyhelper.FieldContext;
020import org.basepom.mojo.propertyhelper.IgnoreWarnFail;
021import org.basepom.mojo.propertyhelper.ValueCache;
022import org.basepom.mojo.propertyhelper.ValueProvider;
023import org.basepom.mojo.propertyhelper.fields.StringField;
024
025import java.io.IOException;
026import java.util.List;
027import java.util.Objects;
028import java.util.StringJoiner;
029
030import com.google.common.annotations.VisibleForTesting;
031import com.google.common.collect.ImmutableList;
032
033/**
034 * Defines a string field. This is a config element that is populated by maven.
035 */
036public class StringDefinition extends FieldDefinition<String> {
037
038    private List<String> values = ImmutableList.of();
039
040
041    /**
042     * called by maven
043     */
044    public void setValues(final List<String> values) {
045        checkNotNull(values, "values is null");
046        this.values = values.stream().map(v -> Objects.requireNonNullElse(v, "")).collect(ImmutableList.toImmutableList());
047    }
048
049
050    /**
051     * Whether a blank string is a valid value. Field injected by Maven.
052     */
053    boolean blankIsValid = true;
054
055    /**
056     * Default action on missing value.
057     */
058    private IgnoreWarnFail onMissingValue = IgnoreWarnFail.FAIL;
059
060    /**
061     * called by maven
062     */
063    public void setOnMissingValue(String onMissingValue) {
064        this.onMissingValue = IgnoreWarnFail.forString(onMissingValue);
065    }
066
067    /**
068     * called by maven
069     */
070    public StringDefinition() {
071    }
072
073    @VisibleForTesting
074    StringDefinition(String id) {
075        super(id);
076    }
077
078    public List<String> getValues() {
079        return values;
080    }
081
082    public boolean isBlankIsValid() {
083        return blankIsValid;
084    }
085
086    public IgnoreWarnFail getOnMissingValue() {
087        return onMissingValue;
088    }
089
090    @Override
091    public StringField createField(FieldContext context, ValueCache valueCache) throws IOException {
092        checkNotNull(context, "context is null");
093        checkNotNull(valueCache, "valueCache is null");
094
095        check();
096
097        final ValueProvider stringValue = valueCache.getValueProvider(this);
098        return new StringField(this, stringValue, context);
099    }
100
101    @Override
102    public String toString() {
103        return new StringJoiner(", ", StringDefinition.class.getSimpleName() + "[", "]")
104            .add("values=" + values)
105            .add("blankIsValid=" + blankIsValid)
106            .add("onMissingValue=" + onMissingValue)
107            .add(super.toString())
108            .toString();
109    }
110
111    @Override
112    public boolean equals(Object o) {
113        if (this == o) {
114            return true;
115        }
116        if (o == null || getClass() != o.getClass()) {
117            return false;
118        }
119        if (!super.equals(o)) {
120            return false;
121        }
122        StringDefinition that = (StringDefinition) o;
123        return blankIsValid == that.blankIsValid && Objects.equals(values, that.values) && onMissingValue == that.onMissingValue;
124    }
125
126    @Override
127    public int hashCode() {
128        return Objects.hash(super.hashCode(), values, blankIsValid, onMissingValue);
129    }
130}