View Javadoc
1   package org.apache.maven.lifecycle.providers;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Arrays;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.LinkedHashMap;
26  
27  import javax.inject.Provider;
28  
29  import org.apache.maven.lifecycle.Lifecycle;
30  import org.apache.maven.lifecycle.mapping.LifecyclePhase;
31  
32  /**
33   * Base lifecycle provider.
34   */
35  public abstract class AbstractLifecycleProvider
36      implements Provider<Lifecycle>
37  {
38      private final Lifecycle lifecycle;
39  
40      protected AbstractLifecycleProvider( String id, String[] phases, String[] pluginBindings )
41      {
42          HashMap<String, LifecyclePhase> defaultBindings = null;
43          if ( pluginBindings != null )
44          {
45              final int len = pluginBindings.length;
46  
47              if ( len < 2 || len % 2 != 0 )
48              {
49                  throw new IllegalArgumentException( "Plugin bindings must have more than 0, even count of elements" );
50              }
51  
52              defaultBindings = new LinkedHashMap<>( len / 2 );
53  
54              for ( int i = 0; i < len; i += 2 )
55              {
56                  defaultBindings.put( pluginBindings[i], new LifecyclePhase( pluginBindings[i + 1] ) );
57              }
58          }
59  
60          this.lifecycle = new Lifecycle(
61              id,
62              Collections.unmodifiableList( Arrays.asList( phases ) ),
63              defaultBindings == null ? null : Collections.unmodifiableMap( defaultBindings )
64          );
65      }
66  
67      @Override
68      public Lifecycle get()
69      {
70          return lifecycle;
71      }
72  }