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.groups;
016
017import org.basepom.mojo.propertyhelper.FieldContext;
018import org.basepom.mojo.propertyhelper.IgnoreWarnFail;
019import org.basepom.mojo.propertyhelper.definitions.PropertyDefinition;
020import org.basepom.mojo.propertyhelper.definitions.PropertyGroupDefinition;
021
022import java.util.Map;
023import java.util.Optional;
024import java.util.Set;
025
026import com.google.common.collect.ImmutableSet;
027
028public class PropertyGroup {
029
030    private final PropertyGroupDefinition propertyGroupDefinition;
031    private final FieldContext context;
032
033    public PropertyGroup(PropertyGroupDefinition propertyGroupDefinition, FieldContext context) {
034        this.propertyGroupDefinition = propertyGroupDefinition;
035        this.context = context;
036    }
037
038    public String getId() {
039        return propertyGroupDefinition.getId();
040    }
041
042    public boolean checkActive(boolean isSnapshot) {
043        return (propertyGroupDefinition.isActiveOnRelease() && !isSnapshot)
044            || (propertyGroupDefinition.isActiveOnSnapshot() && isSnapshot);
045    }
046
047    public IgnoreWarnFail getOnDuplicateProperty() {
048        return propertyGroupDefinition.getOnDuplicateProperty();
049    }
050
051    public String getPropertyValue(final PropertyDefinition propertyDefinition, final Map<String, String> propElements) {
052
053        return Optional.ofNullable(propertyDefinition.getValue())
054            .map(context.getInterpolatorFactory().interpolate(propertyDefinition.getName(), propertyGroupDefinition.getOnMissingField(), propElements))
055            .map(context.getTransformerRegistry().applyTransformers(propertyDefinition.getTransformers()))
056            .orElse("");
057    }
058
059    public Set<PropertyResult> createProperties(final Map<String, String> values) {
060        return propertyGroupDefinition.getPropertyDefinitions().stream()
061            .map(propertyDefinition -> new PropertyResult(propertyDefinition.getName(),
062                getPropertyValue(propertyDefinition, values)))
063            .collect(ImmutableSet.toImmutableSet());
064    }
065}