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 *
019 */
020package org.apache.directory.api.ldap.model.message;
021
022
023import org.apache.directory.api.i18n.I18n;
024
025
026/**
027 * A search scope enumerated type.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public enum SearchScope
032{
033    /** Base scope */
034    OBJECT(0, "base"),
035    
036    /** One Level scope */
037    ONELEVEL(1, "one"),
038    
039    /** Subtree scope */
040    SUBTREE(2, "sub");
041
042    /** 
043     * The corresponding LDAP scope constant value as defined in 
044     * RFC 4511
045     */
046    private final int scope;
047
048    /**
049     * The LDAP URL string value of either base, one or sub as defined in RFC
050     * 2255.
051     * 
052     * @see <a href="http://www.faqs.org/rfcs/rfc2255.html">RFC 2255</a>
053     */
054    private final String ldapUrlValue;
055
056
057    /**
058     * Creates a new instance of SearchScope based on the respective 
059     * scope constant.
060     *
061     * @param scope the scope constant
062     * @param ldapUrlValue LDAP URL scope string value: base, one, or sub
063     */
064    SearchScope( int scope, String ldapUrlValue )
065    {
066        this.scope = scope;
067        this.ldapUrlValue = ldapUrlValue;
068    }
069
070
071    /**
072     * Gets the LDAP URL value for the scope: according to RFC 2255 this is 
073     * either base, one, or sub.
074     * 
075     * @see <a href="http://www.faqs.org/rfcs/rfc2255.html">RFC 2255</a>
076     * 
077     * @return the LDAP URL value
078     */
079    public String getLdapUrlValue()
080    {
081        return ldapUrlValue;
082    }
083
084
085    /**
086     * Gets the corresponding scope constant value as defined in 
087     * RFC 4511.
088     * 
089     * @return the scope
090     */
091    public int getScope()
092    {
093        return scope;
094    }
095
096
097    /**
098     * Gets the SearchScope enumerated type for the corresponding 
099     * scope numeric value.
100     *
101     * @param scope the numeric value to get SearchScope for
102     * @return the SearchScope enumerated type for the scope numeric value
103     */
104    public static SearchScope getSearchScope( int scope )
105    {
106        switch ( scope )
107        {
108            case 0:
109                return OBJECT;
110
111            case 1:
112                return ONELEVEL;
113
114            case 2:
115                return SUBTREE;
116
117            default:
118                throw new IllegalArgumentException( I18n.err( I18n.ERR_04160, scope ) );
119        }
120    }
121
122
123    /**
124     * Gets the SearchScope associated with a scope String
125     *
126     * @param scope The scope we are looking for
127     * @return the scope
128     */
129    public SearchScope getScope( String scope )
130    {
131        if ( "base".equalsIgnoreCase( scope ) )
132        {
133            return OBJECT;
134        }
135        else if ( "one".equalsIgnoreCase( scope ) )
136        {
137            return ONELEVEL;
138        }
139        else if ( "sub".equalsIgnoreCase( scope ) )
140        {
141            return SUBTREE;
142        }
143        else
144        {
145            throw new IllegalArgumentException( I18n.err( I18n.ERR_04161, scope ) );
146        }
147    }
148
149
150    /**
151     * Gets the SearchScope enumerated type for the corresponding 
152     * scope value of either base, one or sub.
153     *
154     * @param scope the scope value to get SearchScope for
155     * @return the SearchScope enumerated type for the LDAP URL scope value
156     */
157    public static int getSearchScope( String scope )
158    {
159        if ( "base".equalsIgnoreCase( scope ) )
160        {
161            return OBJECT.getScope();
162        }
163        else if ( "one".equalsIgnoreCase( scope ) )
164        {
165            return ONELEVEL.getScope();
166        }
167        else if ( "sub".equalsIgnoreCase( scope ) )
168        {
169            return SUBTREE.getScope();
170        }
171        else
172        {
173            throw new IllegalArgumentException( I18n.err( I18n.ERR_04161, scope ) );
174        }
175    }
176
177
178    /**
179     * {@inheritDoc}
180     */
181    @Override
182    public String toString()
183    {
184        return ldapUrlValue;
185    }
186}