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     */
020    
021    package org.apache.directory.shared.ldap.util;
022    
023    
024    import java.util.Collections;
025    import java.util.LinkedList;
026    import java.util.List;
027    
028    import org.apache.directory.shared.i18n.I18n;
029    
030    
031    public class MandatoryAndOptionalComponentsMonitor implements ComponentsMonitor
032    {
033        private ComponentsMonitor mandatoryComponentsMonitor;
034    
035        private ComponentsMonitor optionalComponentsMonitor;
036    
037    
038        public MandatoryAndOptionalComponentsMonitor(String[] mandatoryComponents, String[] optionalComponents)
039            throws IllegalArgumentException
040        {
041            // check for common elements
042            for ( int i = 0; i < mandatoryComponents.length; i++ )
043            {
044                for ( int j = 0; j < optionalComponents.length; j++ )
045                {
046                    if ( mandatoryComponents[i].equals( optionalComponents[j] ) )
047                    {
048                        throw new IllegalArgumentException( I18n.err( I18n.ERR_04415, mandatoryComponents[i] ) );
049                    }
050                }
051            }
052    
053            mandatoryComponentsMonitor = new MandatoryComponentsMonitor( mandatoryComponents );
054            optionalComponentsMonitor = new OptionalComponentsMonitor( optionalComponents );
055        }
056    
057    
058        public ComponentsMonitor useComponent( String component )
059        {
060            try
061            {
062                mandatoryComponentsMonitor.useComponent( component );
063            }
064            catch ( IllegalArgumentException e1 )
065            {
066                try
067                {
068                    optionalComponentsMonitor.useComponent( component );
069                }
070                catch ( IllegalArgumentException e2 )
071                {
072                    throw new IllegalArgumentException( I18n.err( I18n.ERR_04416, component ) );
073                }
074            }
075    
076            return this;
077        }
078    
079    
080        public boolean allComponentsUsed()
081        {
082            return ( mandatoryComponentsMonitor.allComponentsUsed() && optionalComponentsMonitor.allComponentsUsed() );
083        }
084    
085    
086        public boolean finalStateValid()
087        {
088            return ( mandatoryComponentsMonitor.finalStateValid() && optionalComponentsMonitor.finalStateValid() );
089        }
090    
091    
092        public List getRemainingComponents()
093        {
094            List remainingComponents = new LinkedList();
095    
096            remainingComponents.addAll( mandatoryComponentsMonitor.getRemainingComponents() );
097            remainingComponents.addAll( optionalComponentsMonitor.getRemainingComponents() );
098    
099            return Collections.unmodifiableList( remainingComponents );
100        }
101    
102    }