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