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.dsmlv2.request;
021
022
023import java.util.ArrayList;
024import java.util.List;
025
026
027/**
028 * A Object that stores the substring filter. 
029 * 
030 * A substring filter follow this
031 * grammar : 
032 * 
033 * substring = attr "=" ( ([initial] any [final] | 
034 *                        (initial [any] [final) | 
035 *                        ([initial] [any] final) ) 
036 *                       
037 * initial = value 
038 * any = "*" *(value "*")
039 * final = value
040 * 
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 */
043public class SubstringFilter extends Filter
044{
045    /** The substring filter type (an attributeDescription) */
046    private String type;
047
048    /** The initial filter */
049    private String initialSubstrings;
050
051    /** The any filter. It's a list of LdapString */
052    private List<String> anySubstrings = new ArrayList<String>( 1 );
053
054    /** The final filter */
055    private String finalSubstrings;
056
057
058    /**
059     * Get the internal substrings
060     * 
061     * @return Returns the anySubstrings.
062     */
063    public List<String> getAnySubstrings()
064    {
065        return anySubstrings;
066    }
067
068
069    /**
070     * Add a internal substring
071     * 
072     * @param any The anySubstrings to set.
073     */
074    public void addAnySubstrings( String any )
075    {
076        this.anySubstrings.add( any );
077    }
078
079
080    /**
081     * Get the final substring
082     * 
083     * @return Returns the finalSubstrings.
084     */
085    public String getFinalSubstrings()
086    {
087        return finalSubstrings;
088    }
089
090
091    /**
092     * Set the final substring
093     * 
094     * @param finalSubstrings The finalSubstrings to set.
095     */
096    public void setFinalSubstrings( String finalSubstrings )
097    {
098        this.finalSubstrings = finalSubstrings;
099    }
100
101
102    /**
103     * Get the initial substring
104     * 
105     * @return Returns the initialSubstrings.
106     */
107    public String getInitialSubstrings()
108    {
109        return initialSubstrings;
110    }
111
112
113    /**
114     * Set the initial substring
115     * 
116     * @param initialSubstrings The initialSubstrings to set.
117     */
118    public void setInitialSubstrings( String initialSubstrings )
119    {
120        this.initialSubstrings = initialSubstrings;
121    }
122
123
124    /**
125     * Get the attribute
126     * 
127     * @return Returns the type.
128     */
129    public String getType()
130    {
131        return type;
132    }
133
134
135    /**
136     * Set the attribute to match
137     * 
138     * @param type The type to set.
139     */
140    public void setType( String type )
141    {
142        this.type = type;
143    }
144
145
146    /**
147     * Return a string compliant with RFC 2254 representing a Substring filter
148     * 
149     * @return The substring filter string
150     */
151    public String toString()
152    {
153
154        StringBuffer sb = new StringBuffer();
155
156        if ( initialSubstrings != null )
157        {
158            sb.append( initialSubstrings );
159        }
160
161        sb.append( '*' );
162
163        if ( anySubstrings != null )
164        {
165            for ( String any : anySubstrings )
166            {
167                sb.append( any ).append( '*' );
168            }
169        }
170
171        if ( finalSubstrings != null )
172        {
173            sb.append( finalSubstrings );
174        }
175
176        return sb.toString();
177    }
178}