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      public static ArtifactScopeEnum checkScope(ArtifactScopeEnum scope) {
53          return scope == null ? DEFAULT_SCOPE : scope;
54      }
55  
56      /**
57       *
58       * @return unsafe String representation of this scope.
59       */
60      public String getScope() {
61          if (id == 1) {
62              return Artifact.SCOPE_COMPILE;
63          } else if (id == 2) {
64              return Artifact.SCOPE_TEST;
65  
66          } else if (id == 3) {
67              return Artifact.SCOPE_RUNTIME;
68  
69          } else if (id == 4) {
70              return Artifact.SCOPE_PROVIDED;
71          } else if (id == 5) {
72              return Artifact.SCOPE_SYSTEM;
73          } else {
74              return Artifact.SCOPE_RUNTIME_PLUS_SYSTEM;
75          }
76      }
77  
78      private static final ArtifactScopeEnum[][][] COMPLIANCY_SETS = {
79          {{compile}, {compile, provided, system}},
80          {{test}, {compile, test, provided, system}},
81          {{runtime}, {compile, runtime, system}},
82          {{provided}, {compile, test, provided}}
83      };
84  
85      /**
86       * scope relationship function. Used by the graph conflict resolution policies
87       *
88       * @param scope
89       * @return true is supplied scope is an inclusive sub-scope of current one.
90       */
91      public boolean encloses(ArtifactScopeEnum scope) {
92          final ArtifactScopeEnum s = checkScope(scope);
93  
94          // system scope is historic only - and simple
95          if (id == system.id) {
96              return scope.id == system.id;
97          }
98  
99          for (ArtifactScopeEnum[][] set : COMPLIANCY_SETS) {
100             if (id == set[0][0].id) {
101                 for (ArtifactScopeEnum ase : set[1]) {
102                     if (s.id == ase.id) {
103                         return true;
104                     }
105                 }
106                 break;
107             }
108         }
109         return false;
110     }
111 }