001package org.apache.maven.artifact;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022/**
023 * Type safe reincarnation of Artifact scope. Also supplies the <code>DEFAULT_SCOPE<code> as well
024 * as convenience method to deal with scope relationships.
025 *
026 * @author <a href="oleg@codehaus.org">Oleg Gusakov</a>
027 *
028 */
029
030public enum ArtifactScopeEnum
031{
032    compile( 1 ), test( 2 ), runtime( 3 ), provided( 4 ), system( 5 ), runtime_plus_system( 6 );
033
034    public static final ArtifactScopeEnum DEFAULT_SCOPE = compile;
035
036    private int id;
037
038    // Constructor
039    ArtifactScopeEnum( int id )
040    {
041        this.id = id;
042    }
043
044    int getId()
045    {
046        return id;
047    }
048
049
050    /**
051     * Helper method to simplify null processing
052     *
053     * @return
054     */
055    public static final ArtifactScopeEnum checkScope( ArtifactScopeEnum scope )
056    {
057        return scope == null ? DEFAULT_SCOPE : scope;
058    }
059
060    /**
061     *
062     * @return unsafe String representation of this scope.
063     */
064    public String getScope()
065    {
066        if ( id == 1 )
067        {
068            return Artifact.SCOPE_COMPILE;
069        }
070        else if ( id == 2 )
071        {
072            return Artifact.SCOPE_TEST;
073
074        }
075        else if ( id == 3 )
076        {
077            return Artifact.SCOPE_RUNTIME;
078
079        }
080        else if ( id == 4 )
081        {
082            return Artifact.SCOPE_PROVIDED;
083        }
084        else if ( id == 5 )
085        {
086            return Artifact.SCOPE_SYSTEM;
087        }
088        else
089        {
090            return Artifact.SCOPE_RUNTIME_PLUS_SYSTEM;
091        }
092    }
093
094    private static final ArtifactScopeEnum [][][] COMPLIANCY_SETS = {
095          { { compile  }, { compile,                provided, system } }
096        , { { test     }, { compile, test,          provided, system } }
097        , { { runtime  }, { compile,       runtime,           system } }
098        , { { provided }, { compile, test,          provided         } }
099    };
100
101    /**
102     * scope relationship function. Used by the graph conflict resolution policies
103     *
104     * @param scope
105     * @return true is supplied scope is an inclusive sub-scope of current one.
106     */
107    public boolean encloses( ArtifactScopeEnum scope )
108    {
109        final ArtifactScopeEnum s = checkScope( scope );
110
111        // system scope is historic only - and simple
112        if ( id == system.id )
113        {
114            return scope.id == system.id;
115        }
116
117        for ( ArtifactScopeEnum[][] set : COMPLIANCY_SETS )
118        {
119            if ( id == set[0][0].id )
120            {
121                for ( ArtifactScopeEnum ase : set[1] )
122                {
123                    if ( s.id == ase.id )
124                    {
125                        return true;
126                    }
127                }
128                break;
129            }
130        }
131        return false;
132    }
133}