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.fields;
016
017import static java.lang.String.format;
018import static org.basepom.mojo.propertyhelper.IgnoreWarnFail.checkIgnoreWarnFailState;
019
020import org.basepom.mojo.propertyhelper.Field;
021import org.basepom.mojo.propertyhelper.FieldContext;
022import org.basepom.mojo.propertyhelper.ValueProvider;
023import org.basepom.mojo.propertyhelper.definitions.StringDefinition;
024
025import java.util.List;
026import java.util.Optional;
027import java.util.StringJoiner;
028
029import com.google.common.annotations.VisibleForTesting;
030import com.google.common.base.Strings;
031import com.google.common.collect.Lists;
032
033public final class StringField extends Field<String, StringDefinition> {
034
035    private final ValueProvider valueProvider;
036
037    @VisibleForTesting
038    public static StringField forTesting(StringDefinition stringDefinition, ValueProvider valueProvider) {
039        return new StringField(stringDefinition, valueProvider, FieldContext.forTesting());
040    }
041
042    public StringField(final StringDefinition stringDefinition, final ValueProvider valueProvider,
043        FieldContext fieldContext) {
044        super(stringDefinition, fieldContext);
045
046        this.valueProvider = valueProvider;
047    }
048
049    @Override
050    public String getFieldName() {
051        // This is not the property name (because many definitions can map onto one prop)
052        // but the actual id.
053        return fieldDefinition.getId();
054    }
055
056    @Override
057    public String getValue() {
058        final List<String> values = Lists.newArrayList();
059
060        final Optional<String> propValue = valueProvider.getValue();
061        final List<String> definedValues = fieldDefinition.getValues();
062
063        // Only add the value from the provider if it is not null.
064        propValue.ifPresent(values::add);
065
066        values.addAll(definedValues);
067
068        for (String value : values) {
069            var stringResult = Strings.nullToEmpty(value);
070            if (fieldDefinition.isBlankIsValid() || !stringResult.isBlank()) {
071                return formatResult(value);
072            }
073        }
074
075        checkIgnoreWarnFailState(false, fieldDefinition.getOnMissingValue(),
076            () -> "",
077            () -> format("No value for string field %s found, using an empty value!", getFieldName()));
078
079        return "";
080    }
081
082    @Override
083    public String toString() {
084        return new StringJoiner(", ", StringField.class.getSimpleName() + "[", "]")
085            .add("stringDefinition=" + fieldDefinition)
086            .add("valueProvider=" + valueProvider)
087            .toString();
088    }
089}