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.macros;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  
19  import org.basepom.mojo.propertyhelper.FieldContext;
20  import org.basepom.mojo.propertyhelper.ValueProvider;
21  import org.basepom.mojo.propertyhelper.definitions.MacroDefinition;
22  
23  import java.util.Objects;
24  import java.util.Optional;
25  
26  import org.codehaus.plexus.component.annotations.Component;
27  
28  @Component(role = MacroType.class, hint = "demo")
29  public class DemoMacro
30      implements MacroType {
31  
32      @Override
33      public Optional<String> getValue(final MacroDefinition macroDefinition,
34          final ValueProvider valueProvider,
35          final FieldContext context) {
36          checkNotNull(valueProvider, "valueProvider is null");
37          checkNotNull(context, "context is null");
38  
39          final String type = Objects.requireNonNullElse(macroDefinition.getProperties().get("type"), "static");
40          if ("static".equals(type)) {
41              return Optional.of("static-value");
42          } else if ("property".equals(type)) {
43              return valueProvider.getValue();
44          } else {
45              return Optional.ofNullable(macroDefinition.getProperties().get("value"));
46          }
47      }
48  }