001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.impl.scope; 020 021import java.util.Arrays; 022import java.util.Collection; 023import java.util.Collections; 024import java.util.HashSet; 025 026import static java.util.Objects.requireNonNull; 027 028/** 029 * Build scope query. 030 * 031 * @since 2.0.0 032 */ 033public final class BuildScopeQuery { 034 public enum Mode { 035 ALL, 036 BY_PROJECT_PATH, 037 BY_BUILD_PATH, 038 SELECT, 039 SINGLETON; 040 041 private void validate(ProjectPath projectPath, BuildPath buildPath) { 042 if ((this == ALL) && (projectPath != null || buildPath != null)) { 043 throw new IllegalArgumentException(this.name() + " requires no parameter"); 044 } else if (this == BY_PROJECT_PATH && (projectPath == null || buildPath != null)) { 045 throw new IllegalArgumentException(this.name() + " requires project path parameter only"); 046 } else if (this == BY_BUILD_PATH && (projectPath != null || buildPath == null)) { 047 throw new IllegalArgumentException(this.name() + " requires build path parameter only"); 048 } else if ((this == SELECT || this == SINGLETON) && (projectPath == null || buildPath == null)) { 049 throw new IllegalArgumentException(this.name() + " requires both parameters"); 050 } 051 } 052 } 053 054 private final Mode mode; 055 private final ProjectPath projectPath; 056 private final BuildPath buildPath; 057 058 private BuildScopeQuery(Mode mode, ProjectPath projectPath, BuildPath buildPath) { 059 this.mode = requireNonNull(mode, "mode"); 060 mode.validate(projectPath, buildPath); 061 this.projectPath = projectPath; 062 this.buildPath = buildPath; 063 } 064 065 public Mode getMode() { 066 return mode; 067 } 068 069 public ProjectPath getProjectPath() { 070 return projectPath; 071 } 072 073 public BuildPath getBuildPath() { 074 return buildPath; 075 } 076 077 @Override 078 public String toString() { 079 if ((mode == Mode.ALL)) { 080 return mode.name(); 081 } else if (mode == Mode.BY_PROJECT_PATH) { 082 return mode.name() + "(" + projectPath.getId() + ")"; 083 } else if (mode == Mode.BY_BUILD_PATH) { 084 return mode.name() + "(" + buildPath.getId() + ")"; 085 } else { 086 return mode.name() + "(" + projectPath.getId() + ", " + buildPath.getId() + ")"; 087 } 088 } 089 090 public static Collection<BuildScopeQuery> all() { 091 return Collections.singleton(new BuildScopeQuery(Mode.ALL, null, null)); 092 } 093 094 public static Collection<BuildScopeQuery> byProjectPath(ProjectPath projectPath) { 095 return Collections.singleton(new BuildScopeQuery(Mode.BY_PROJECT_PATH, projectPath, null)); 096 } 097 098 public static Collection<BuildScopeQuery> byBuildPath(BuildPath buildPath) { 099 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}