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.collect.ImmutableSet.toImmutableSet;
018
019import org.basepom.mojo.propertyhelper.FieldContext;
020import org.basepom.mojo.propertyhelper.IgnoreWarnFail;
021import org.basepom.mojo.propertyhelper.groups.PropertyGroup;
022
023import java.util.Arrays;
024import java.util.Set;
025
026import com.google.common.annotations.VisibleForTesting;
027import com.google.common.collect.ImmutableSet;
028
029public class PropertyGroupDefinition {
030
031    /**
032     * Property group id.
033     */
034    String id;
035
036    /**
037     * Activate the group if the current project version does not contain SNAPSHOT. Field injected by Maven.
038     */
039    boolean activeOnRelease = true;
040
041    /**
042     * Activate the group if the current project version contains SNAPSHOT. Field injected by Maven.
043     */
044    boolean activeOnSnapshot = true;
045
046    /**
047     * Action if this property group defines a duplicate property. Field injected by Maven.
048     */
049    private IgnoreWarnFail onDuplicateProperty = IgnoreWarnFail.FAIL;
050
051    public void setOnDuplicateProperty(String onDuplicateProperty) {
052        this.onDuplicateProperty = IgnoreWarnFail.forString(onDuplicateProperty);
053    }
054
055    /**
056     * Action if any property from that group could not be defined. Field injected by Maven.
057     */
058    private IgnoreWarnFail onMissingField = IgnoreWarnFail.FAIL;
059
060    public void setOnMissingField(String onMissingField) {
061        this.onMissingField = IgnoreWarnFail.forString(onMissingField);
062    }
063
064    /**
065     * Property definitions in this group.
066     */
067    Set<PropertyDefinition> propertyDefinitions = Set.of();
068
069    // called by maven
070    public void setProperties(PropertyDefinition... propertyDefinitions) {
071        this.propertyDefinitions = ImmutableSet.copyOf(Arrays.asList(propertyDefinitions));
072
073        this.propertyDefinitions.forEach(PropertyDefinition::check);
074    }
075
076    public PropertyGroupDefinition() {
077    }
078
079    @VisibleForTesting
080    PropertyGroupDefinition(String id) {
081        this.id = id;
082    }
083
084    public String getId() {
085        return id;
086    }
087
088    public boolean isActiveOnRelease() {
089        return activeOnRelease;
090    }
091
092    public boolean isActiveOnSnapshot() {
093        return activeOnSnapshot;
094    }
095
096    public IgnoreWarnFail getOnDuplicateProperty() {
097        return onDuplicateProperty;
098    }
099
100    public IgnoreWarnFail getOnMissingField() {
101        return onMissingField;
102    }
103
104    public Set<PropertyDefinition> getPropertyDefinitions() {
105        return propertyDefinitions;
106    }
107
108    public PropertyGroup createGroup(FieldContext context) {
109        return new PropertyGroup(this, context);
110    }
111
112    public Set<String> getPropertyNames() {
113        return propertyDefinitions.stream()
114            .map(PropertyDefinition::getName)
115            .collect(toImmutableSet());
116    }
117}