View Javadoc
1   package it.serendigity.maven.plugin.lifecycle.helper.vo;
2   
3   import java.util.Objects;
4   
5   import jakarta.annotation.Generated;
6   
7   import org.apache.commons.lang3.builder.EqualsBuilder;
8   import org.apache.commons.lang3.builder.HashCodeBuilder;
9   import org.apache.maven.lifecycle.DefaultLifecycles;
10  import org.apache.maven.plugin.MojoExecution;
11  
12  import it.serendigity.maven.plugin.lifecycle.helper.utils.MavenUtils;
13  
14  public final class MavenExecutionInfo {
15  
16  	private int executionOrder;
17  	private String lifecycle;
18  	private String phase;
19  
20  	private String pluginArtifactId;
21  	private String pluginVersion;
22  	private String pluginExecutionId;
23  	private String pluginGoal;
24  
25  	public MavenExecutionInfo() {
26  		super();
27  	}
28  
29  	public int getExecutionOrder() {
30  		return executionOrder;
31  	}
32  
33  	public String getLifecycle() {
34  		return lifecycle;
35  	}
36  
37  	public String getPhase() {
38  		return phase;
39  	}
40  
41  	public String getPluginArtifactId() {
42  		return pluginArtifactId;
43  	}
44  
45  	public String getPluginVersion() {
46  		return pluginVersion;
47  	}
48  
49  	public String getPluginExecutionId() {
50  		return pluginExecutionId;
51  	}
52  
53  	public String getPluginGoal() {
54  		return pluginGoal;
55  	}
56  
57  	public static MavenExecutionInfo from( int order, MojoExecution mojoExecution, DefaultLifecycles defaultLifecycles ) {
58  
59  		return MavenExecutionInfo.builder()
60  				.withExecutionOrder( order )
61  				.withLifecycle( MavenUtils.retrieveLifecycle( mojoExecution, defaultLifecycles ) )
62  				.withPhase( mojoExecution.getLifecyclePhase() )
63  				.withPluginArtifactId( mojoExecution.getArtifactId() )
64  				.withPluginVersion( mojoExecution.getVersion() )
65  				.withPluginExecutionId( mojoExecution.getExecutionId() )
66  				.withPluginGoal( mojoExecution.getGoal() )
67  
68  				.build();
69  
70  	}
71  
72  	@Generated("SparkTools")
73  	private MavenExecutionInfo( Builder builder ) {
74  		this.lifecycle = builder.lifecycle;
75  		this.phase = builder.phase;
76  		this.pluginArtifactId = builder.pluginArtifactId;
77  		this.pluginVersion = builder.pluginVersion;
78  		this.pluginExecutionId = builder.pluginExecutionId;
79  		this.pluginGoal = builder.pluginGoal;
80  		this.executionOrder = builder.executionOrder;
81  
82  	}
83  
84  	/**
85  	 * Creates builder to build {@link MavenExecutionInfo}.
86  	 *
87  	 * @return created builder
88  	 */
89  	@Generated("SparkTools")
90  	public static Builder builder() {
91  		return new Builder();
92  	}
93  
94  	/**
95  	 * Builder to build {@link MavenExecutionInfo}.
96  	 */
97  	@Generated("SparkTools")
98  	public static final class Builder {
99  
100 		private int executionOrder;
101 		private String lifecycle;
102 		private String phase;
103 		private String pluginArtifactId;
104 		private String pluginVersion;
105 		private String pluginExecutionId;
106 		private String pluginGoal;
107 
108 		private Builder() {
109 		}
110 
111 		public Builder withExecutionOrder( int executionOrder ) {
112 			this.executionOrder = executionOrder;
113 			return this;
114 		}
115 
116 		public Builder withLifecycle( String lifecycle ) {
117 			this.lifecycle = lifecycle;
118 			return this;
119 		}
120 
121 		public Builder withPhase( String phase ) {
122 			this.phase = phase;
123 			return this;
124 		}
125 
126 		public Builder withPluginArtifactId( String pluginArtifactId ) {
127 			this.pluginArtifactId = pluginArtifactId;
128 			return this;
129 		}
130 
131 		public Builder withPluginVersion( String pluginVersion ) {
132 			this.pluginVersion = pluginVersion;
133 			return this;
134 		}
135 
136 		public Builder withPluginExecutionId( String pluginExecutionId ) {
137 			this.pluginExecutionId = pluginExecutionId;
138 			return this;
139 		}
140 
141 		public Builder withPluginGoal( String pluginGoal ) {
142 			this.pluginGoal = pluginGoal;
143 			return this;
144 		}
145 
146 		public MavenExecutionInfo build() {
147 			return new MavenExecutionInfo( this );
148 		}
149 	}
150 
151 	/**
152 	 * Retrieve value of the attribute {@code mavenExecutionAttribute}.
153 	 * The null value are converted in empty value
154 	 *
155 	 * @param mavenExecutionAttribute the attribute
156 	 * @return the value of the attribute or empty string otherwise
157 	 */
158 	public Object getValueOrEmpty( MavenExecutionAttribute mavenExecutionAttribute ) {
159 		return Objects.toString( getValue( mavenExecutionAttribute ), "" );
160 	}
161 
162 	/**
163 	 * Retrieve value of the attribute {@code mavenExecutionAttribute}
164 	 *
165 	 * @param mavenExecutionAttribute the attribute
166 	 * @return the value of the attribute or empty string otherwise
167 	 */
168 	public Object getValue( MavenExecutionAttribute mavenExecutionAttribute ) {
169 
170 		Object result = null;
171 
172 		switch ( mavenExecutionAttribute ) {
173 			case PLAN_ORDER:
174 				result = getExecutionOrder();
175 				break;
176 			case LIFECYCLE:
177 				result = getLifecycle();
178 				break;
179 
180 			case PHASE:
181 				result = getPhase();
182 				break;
183 			case PLUGIN:
184 				result = getPluginArtifactId();
185 				break;
186 
187 			case PLUGIN_VERSION:
188 				result = getPluginVersion();
189 				break;
190 
191 			case PLUGIN_EXECUTION_ID:
192 				result = getPluginExecutionId();
193 				break;
194 
195 			case PLUGIN_GOAL:
196 				result = getPluginGoal();
197 				break;
198 			default:
199 				break;
200 		}
201 
202 		return result;
203 	}
204 
205 	public int getStringLength( MavenExecutionAttribute mavenExecutionAttribute ) {
206 
207 		Object value = getValue( mavenExecutionAttribute );
208 
209 		int result = 0;
210 		if ( value != null ) {
211 			result = String.valueOf( value ).length();
212 		}
213 
214 		return result;
215 	}
216 
217 	public void setValue( MavenExecutionAttribute mavenExecutionAttribute, Object value ) {
218 		if ( value != null ) {
219 			String s = String.valueOf( value );
220 			switch ( mavenExecutionAttribute ) {
221 				case PLAN_ORDER:
222 					setExecutionOrder( Integer.parseInt( s ) );
223 					break;
224 
225 				case LIFECYCLE:
226 					setLifecycle( s );
227 					break;
228 
229 				case PHASE:
230 					setPhase( s );
231 					break;
232 				case PLUGIN:
233 					setPluginArtifactId( s );
234 					break;
235 
236 				case PLUGIN_VERSION:
237 					setPluginVersion( s );
238 					break;
239 
240 				case PLUGIN_EXECUTION_ID:
241 					setPluginExecutionId( s );
242 					break;
243 
244 				case PLUGIN_GOAL:
245 					setPluginGoal( s );
246 					break;
247 
248 				default:
249 					break;
250 			}
251 		}
252 	}
253 
254 	public void setExecutionOrder( int executionOrder ) {
255 		this.executionOrder = executionOrder;
256 	}
257 
258 	public void setLifecycle( String lifecycle ) {
259 		this.lifecycle = lifecycle;
260 	}
261 
262 	public void setPhase( String phase ) {
263 		this.phase = phase;
264 	}
265 
266 	public void setPluginArtifactId( String pluginArtifactId ) {
267 		this.pluginArtifactId = pluginArtifactId;
268 	}
269 
270 	public void setPluginVersion( String pluginVersion ) {
271 		this.pluginVersion = pluginVersion;
272 	}
273 
274 	public void setPluginExecutionId( String pluginExecutionId ) {
275 		this.pluginExecutionId = pluginExecutionId;
276 	}
277 
278 	public void setPluginGoal( String pluginGoal ) {
279 		this.pluginGoal = pluginGoal;
280 	}
281 
282 	@Override
283 	public String toString() {
284 		return "MavenExecutionInfo [executionOrder=" + executionOrder + ", lifecycle=" + lifecycle + ", phase=" + phase
285 				+ ", pluginArtifactId=" + pluginArtifactId + ", pluginVersion=" + pluginVersion + ", pluginExecutionId="
286 				+ pluginExecutionId + ", pluginGoal=" + pluginGoal + "]";
287 	}
288 
289 	@Override
290 	public boolean equals( Object o ) {
291 
292 		if ( !( o instanceof MavenExecutionInfo ) ) {
293 			return false;
294 		}
295 
296 		MavenExecutionInfo other = (MavenExecutionInfo) o;
297 
298 		return new EqualsBuilder()
299 				.append( getLifecycle(), other.getLifecycle() )
300 				.append( getExecutionOrder(), other.getExecutionOrder() )
301 				.append( getPhase(), other.getPhase() )
302 				.append( getPluginArtifactId(), other.getPluginArtifactId() )
303 				.append( getPluginVersion(), other.getPluginVersion() )
304 				.append( getPluginExecutionId(), other.getPluginExecutionId() )
305 				.append( getPluginGoal(), other.getPluginGoal() )
306 				.isEquals();
307 
308 	}
309 
310 	@Override
311 	public int hashCode() {
312 		return new HashCodeBuilder()
313 				.append( getLifecycle() )
314 				.append( getExecutionOrder() )
315 				.append( getPhase() )
316 				.append( getPluginArtifactId() )
317 				.append( getPluginVersion() )
318 				.append( getPluginExecutionId() )
319 				.append( getPluginGoal() )
320 				.toHashCode()
321 
322 		;
323 	}
324 
325 }