1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.eclipse.aether.util.graph.transformer;
20
21 import org.eclipse.aether.RepositoryException;
22 import org.eclipse.aether.collection.DependencyGraphTransformationContext;
23 import org.eclipse.aether.collection.DependencyGraphTransformer;
24 import org.eclipse.aether.graph.DependencyNode;
25
26 import static java.util.Objects.requireNonNull;
27
28 /**
29 * A dependency graph transformer that chains other transformers.
30 */
31 public final class ChainedDependencyGraphTransformer implements DependencyGraphTransformer {
32
33 private final DependencyGraphTransformer[] transformers;
34
35 /**
36 * Creates a new transformer that chains the specified transformers.
37 *
38 * @param transformers the transformers to chain, may be {@code null} or empty
39 */
40 public ChainedDependencyGraphTransformer(DependencyGraphTransformer... transformers) {
41 if (transformers == null) {
42 this.transformers = new DependencyGraphTransformer[0];
43 } else {
44 this.transformers = transformers;
45 }
46 }
47
48 /**
49 * Creates a new transformer that chains the specified transformers or simply returns one of them if the other one
50 * is {@code null}.
51 *
52 * @param transformer1 the first transformer of the chain, may be {@code null}
53 * @param transformer2 the second transformer of the chain, may be {@code null}
54 * @return the chained transformer or {@code null} if both input transformers are {@code null}
55 */
56 public static DependencyGraphTransformer newInstance(
57 DependencyGraphTransformer transformer1, DependencyGraphTransformer transformer2) {
58 if (transformer1 == null) {
59 return transformer2;
60 } else if (transformer2 == null) {
61 return transformer1;
62 }
63 return new ChainedDependencyGraphTransformer(transformer1, transformer2);
64 }
65
66 @Override
67 public DependencyNode transformGraph(DependencyNode node, DependencyGraphTransformationContext context)
68 throws RepositoryException {
69 requireNonNull(node, "node cannot be null");
70 requireNonNull(context, "context cannot be null");
71 for (DependencyGraphTransformer transformer : transformers) {
72 node = transformer.transformGraph(node, context);
73 }
74 return node;
75 }
76 }