View Javadoc
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.impl.scope;
20  
21  import java.util.Arrays;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashSet;
25  
26  import static java.util.Objects.requireNonNull;
27  
28  /**
29   * Build scope query.
30   *
31   * @since 2.0.0
32   */
33  public final class BuildScopeQuery {
34      public enum Mode {
35          ALL,
36          BY_PROJECT_PATH,
37          BY_BUILD_PATH,
38          SELECT,
39          SINGLETON;
40  
41          private void validate(ProjectPath projectPath, BuildPath buildPath) {
42              if ((this == ALL) && (projectPath != null || buildPath != null)) {
43                  throw new IllegalArgumentException(this.name() + " requires no parameter");
44              } else if (this == BY_PROJECT_PATH && (projectPath == null || buildPath != null)) {
45                  throw new IllegalArgumentException(this.name() + " requires project path parameter only");
46              } else if (this == BY_BUILD_PATH && (projectPath != null || buildPath == null)) {
47                  throw new IllegalArgumentException(this.name() + " requires build path parameter only");
48              } else if ((this == SELECT || this == SINGLETON) && (projectPath == null || buildPath == null)) {
49                  throw new IllegalArgumentException(this.name() + " requires both parameters");
50              }
51          }
52      }
53  
54      private final Mode mode;
55      private final ProjectPath projectPath;
56      private final BuildPath buildPath;
57  
58      private BuildScopeQuery(Mode mode, ProjectPath projectPath, BuildPath buildPath) {
59          this.mode = requireNonNull(mode, "mode");
60          mode.validate(projectPath, buildPath);
61          this.projectPath = projectPath;
62          this.buildPath = buildPath;
63      }
64  
65      public Mode getMode() {
66          return mode;
67      }
68  
69      public ProjectPath getProjectPath() {
70          return projectPath;
71      }
72  
73      public BuildPath getBuildPath() {
74          return buildPath;
75      }
76  
77      @Override
78      public String toString() {
79          if ((mode == Mode.ALL)) {
80              return mode.name();
81          } else if (mode == Mode.BY_PROJECT_PATH) {
82              return mode.name() + "(" + projectPath.getId() + ")";
83          } else if (mode == Mode.BY_BUILD_PATH) {
84              return mode.name() + "(" + buildPath.getId() + ")";
85          } else {
86              return mode.name() + "(" + projectPath.getId() + ", " + buildPath.getId() + ")";
87          }
88      }
89  
90      public static Collection<BuildScopeQuery> all() {
91          return Collections.singleton(new BuildScopeQuery(Mode.ALL, null, null));
92      }
93  
94      public static Collection<BuildScopeQuery> byProjectPath(ProjectPath projectPath) {
95          return Collections.singleton(new BuildScopeQuery(Mode.BY_PROJECT_PATH, projectPath, null));
96      }
97  
98      public static Collection<BuildScopeQuery> byBuildPath(BuildPath buildPath) {
99          return Collections.singleton(new BuildScopeQuery(Mode.BY_BUILD_PATH, null, buildPath));
100     }
101 
102     public static Collection<BuildScopeQuery> select(ProjectPath projectPath, BuildPath buildPath) {
103         return Collections.singleton(new BuildScopeQuery(Mode.SELECT, projectPath, buildPath));
104     }
105 
106     public static Collection<BuildScopeQuery> singleton(ProjectPath projectPath, BuildPath buildPath) {
107         return Collections.singleton(new BuildScopeQuery(Mode.SINGLETON, projectPath, buildPath));
108     }
109 
110     @SafeVarargs
111     public static Collection<BuildScopeQuery> union(Collection<BuildScopeQuery>... buildScopeQueries) {
112         HashSet<BuildScopeQuery> result = new HashSet<>();
113         Arrays.asList(buildScopeQueries).forEach(result::addAll);
114         return result;
115     }
116 }