View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package org.basepom.mojo.propertyhelper.groups;
16  
17  import org.basepom.mojo.propertyhelper.FieldContext;
18  import org.basepom.mojo.propertyhelper.IgnoreWarnFail;
19  import org.basepom.mojo.propertyhelper.definitions.PropertyDefinition;
20  import org.basepom.mojo.propertyhelper.definitions.PropertyGroupDefinition;
21  
22  import java.util.Map;
23  import java.util.Optional;
24  import java.util.Set;
25  
26  import com.google.common.collect.ImmutableSet;
27  
28  public class PropertyGroup {
29  
30      private final PropertyGroupDefinition propertyGroupDefinition;
31      private final FieldContext context;
32  
33      public PropertyGroup(PropertyGroupDefinition propertyGroupDefinition, FieldContext context) {
34          this.propertyGroupDefinition = propertyGroupDefinition;
35          this.context = context;
36      }
37  
38      public String getId() {
39          return propertyGroupDefinition.getId();
40      }
41  
42      public boolean checkActive(boolean isSnapshot) {
43          return (propertyGroupDefinition.isActiveOnRelease() && !isSnapshot)
44              || (propertyGroupDefinition.isActiveOnSnapshot() && isSnapshot);
45      }
46  
47      public IgnoreWarnFail getOnDuplicateProperty() {
48          return propertyGroupDefinition.getOnDuplicateProperty();
49      }
50  
51      public String getPropertyValue(final PropertyDefinition propertyDefinition, final Map<String, String> propElements) {
52  
53          return Optional.ofNullable(propertyDefinition.getValue())
54              .map(context.getInterpolatorFactory().interpolate(propertyDefinition.getName(), propertyGroupDefinition.getOnMissingField(), propElements))
55              .map(context.getTransformerRegistry().applyTransformers(propertyDefinition.getTransformers()))
56              .orElse("");
57      }
58  
59      public Set<PropertyResult> createProperties(final Map<String, String> values) {
60          return propertyGroupDefinition.getPropertyDefinitions().stream()
61              .map(propertyDefinition -> new PropertyResult(propertyDefinition.getName(),
62                  getPropertyValue(propertyDefinition, values)))
63              .collect(ImmutableSet.toImmutableSet());
64      }
65  }