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.base.Preconditions.checkNotNull; 018import static com.google.common.base.Preconditions.checkState; 019 020import org.basepom.mojo.propertyhelper.FieldContext; 021import org.basepom.mojo.propertyhelper.ValueCache; 022import org.basepom.mojo.propertyhelper.ValueProvider; 023import org.basepom.mojo.propertyhelper.fields.MacroField; 024 025import java.io.IOException; 026import java.util.Map; 027import java.util.Objects; 028import java.util.Optional; 029import java.util.Properties; 030import java.util.StringJoiner; 031 032import com.google.common.annotations.VisibleForTesting; 033import com.google.common.collect.ImmutableMap; 034import com.google.common.collect.Maps; 035 036/** 037 * Represents a macro configuration in the POM. 038 */ 039public class MacroDefinition extends FieldDefinition<String> { 040 041 /** 042 * Macro type. This value is used to look up a plexus component with this value as hint. Field injected by Maven. 043 */ 044 String macroType = null; 045 046 /** 047 * Class for this macro. If no macro type is given and this value is provided, the plugin will try to instantiate this class. Field injected by Maven. 048 */ 049 String macroClass = null; 050 051 /** 052 * Macro specific properties. Field injected by Maven. 053 */ 054 Properties properties = new Properties(); 055 056 public MacroDefinition() { 057 } 058 059 @VisibleForTesting 060 MacroDefinition(String id) { 061 super(id); 062 } 063 064 public Optional<String> getMacroType() { 065 return Optional.ofNullable(macroType); 066 } 067 068 public Optional<String> getMacroClass() { 069 return Optional.ofNullable(macroClass); 070 } 071 072 public Map<String, String> getProperties() { 073 return ImmutableMap.copyOf(Maps.fromProperties(properties)); 074 } 075 076 @Override 077 public void check() { 078 super.check(); 079 080 checkState(macroClass != null || macroType != null, "neither macro class nor macro type is defined!"); 081 } 082 083 @Override 084 public MacroField createField(FieldContext context, ValueCache valueCache) throws IOException { 085 checkNotNull(context, "context is null"); 086 checkNotNull(valueCache, "valueCache is null"); 087 088 check(); 089 090 final ValueProvider macroValue = valueCache.getValueProvider(this); 091 return new MacroField(this, macroValue, context); 092 } 093 094 @Override 095 public String toString() { 096 return new StringJoiner(", ", MacroDefinition.class.getSimpleName() + "[", "]") 097 .add("macroType='" + macroType + "'") 098 .add("macroClass='" + macroClass + "'") 099 .add("properties=" + properties) 100 .add(super.toString()) 101 .toString(); 102 } 103 104 @Override 105 public boolean equals(Object o) { 106 if (this == o) { 107 return true; 108 } 109 if (o == null || getClass() != o.getClass()) { 110 return false; 111 } 112 if (!super.equals(o)) { 113 return false; 114 } 115 MacroDefinition that = (MacroDefinition) o; 116 return Objects.equals(macroType, that.macroType) && Objects.equals(macroClass, that.macroClass) && Objects.equals(properties, 117 that.properties); 118 } 119 120 @Override 121 public int hashCode() { 122 return Objects.hash(super.hashCode(), macroType, macroClass, properties); 123 } 124}