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.apache.maven.artifact;
20  
21  /**
22   * Type safe reincarnation of Artifact scope. Also supplies the {@code DEFAULT_SCOPE} as well
23   * as convenience method to deal with scope relationships.
24   *
25   * @author <a href="oleg@codehaus.org">Oleg Gusakov</a>
26   *
27   */
28  public enum ArtifactScopeEnum {
29      compile(1),
30      test(2),
31      runtime(3),
32      provided(4),
33      system(5),
34      runtime_plus_system(6);
35  
36      public static final ArtifactScopeEnum DEFAULT_SCOPE = compile;
37  
38      private int id;
39  
40      // Constructor
41      ArtifactScopeEnum(int id) {
42          this.id = id;
43      }
44  
45      int getId() {
46          return id;
47      }
48  
49      /**
50       * Helper method to simplify null processing
51       *
52       * @param scope a scope or {@code null}
53       * @return the provided scope or DEFAULT_SCOPE
54       */
55      public static ArtifactScopeEnum checkScope(ArtifactScopeEnum scope) {
56          return scope == null ? DEFAULT_SCOPE : scope;
57      }
58  
59      /**
60       *
61       * @return unsafe String representation of this scope.
62       */
63      public String getScope() {
64          if (id == 1) {
65              return Artifact.SCOPE_COMPILE;
66          } else if (id == 2) {
67              return Artifact.SCOPE_TEST;
68  
69          } else if (id == 3) {
70              return Artifact.SCOPE_RUNTIME;
71  
72          } else if (id == 4) {
73              return Artifact.SCOPE_PROVIDED;
74          } else if (id == 5) {
75              return Artifact.SCOPE_SYSTEM;
76          } else {
77              return Artifact.SCOPE_RUNTIME_PLUS_SYSTEM;
78          }
79      }
80  
81      private static final ArtifactScopeEnum[][][] COMPLIANCY_SETS = {
82          {{compile}, {compile, provided, system}},
83          {{test}, {compile, test, provided, system}},
84          {{runtime}, {compile, runtime, system}},
85          {{provided}, {compile, test, provided}}
86      };
87  
88      /**
89       * scope relationship function. Used by the graph conflict resolution policies
90       *
91       * @param scope a scope
92       * @return true is supplied scope is an inclusive sub-scope of current one.
93       */
94      public boolean encloses(ArtifactScopeEnum scope) {
95          final ArtifactScopeEnum s = checkScope(scope);
96  
97          // system scope is historic only - and simple
98          if (id == system.id) {
99              return scope.id == system.id;
100         }
101 
102         for (ArtifactScopeEnum[][] set : COMPLIANCY_SETS) {
103             if (id == set[0][0].id) {
104                 for (ArtifactScopeEnum ase : set[1]) {
105                     if (s.id == ase.id) {
106                         return true;
107                     }
108                 }
109                 break;
110             }
111         }
112         return false;
113     }
114 }