View Javadoc
1   package org.eclipse.aether.util.graph.transformer;
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 org.eclipse.aether.RepositoryException;
23  import org.eclipse.aether.collection.DependencyGraphTransformationContext;
24  import org.eclipse.aether.collection.DependencyGraphTransformer;
25  import org.eclipse.aether.graph.DependencyNode;
26  
27  /**
28   * A dependency graph transformer that chains other transformers.
29   */
30  public final class ChainedDependencyGraphTransformer
31      implements DependencyGraphTransformer
32  {
33  
34      private final DependencyGraphTransformer[] transformers;
35  
36      /**
37       * Creates a new transformer that chains the specified transformers.
38       * 
39       * @param transformers The transformers to chain, may be {@code null} or empty.
40       */
41      public ChainedDependencyGraphTransformer( DependencyGraphTransformer... transformers )
42      {
43          if ( transformers == null )
44          {
45              this.transformers = new DependencyGraphTransformer[0];
46          }
47          else
48          {
49              this.transformers = transformers;
50          }
51      }
52  
53      /**
54       * Creates a new transformer that chains the specified transformers or simply returns one of them if the other one
55       * is {@code null}.
56       * 
57       * @param transformer1 The first transformer of the chain, may be {@code null}.
58       * @param transformer2 The second transformer of the chain, may be {@code null}.
59       * @return The chained transformer or {@code null} if both input transformers are {@code null}.
60       */
61      public static DependencyGraphTransformer newInstance( DependencyGraphTransformer transformer1,
62                                                            DependencyGraphTransformer transformer2 )
63      {
64          if ( transformer1 == null )
65          {
66              return transformer2;
67          }
68          else if ( transformer2 == null )
69          {
70              return transformer1;
71          }
72          return new ChainedDependencyGraphTransformer( transformer1, transformer2 );
73      }
74  
75      public DependencyNode transformGraph( DependencyNode node, DependencyGraphTransformationContext context )
76          throws RepositoryException
77      {
78          for ( DependencyGraphTransformer transformer : transformers )
79          {
80              node = transformer.transformGraph( node, context );
81          }
82          return node;
83      }
84  
85  }