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    
029    public class MandatoryAndOptionalComponentsMonitor implements ComponentsMonitor
030    {
031        private ComponentsMonitor mandatoryComponentsMonitor;
032    
033        private ComponentsMonitor optionalComponentsMonitor;
034    
035    
036        public MandatoryAndOptionalComponentsMonitor(String[] mandatoryComponents, String[] optionalComponents)
037            throws IllegalArgumentException
038        {
039            // check for common elements
040            for ( int i = 0; i < mandatoryComponents.length; i++ )
041            {
042                for ( int j = 0; j < optionalComponents.length; j++ )
043                {
044                    if ( mandatoryComponents[i].equals( optionalComponents[j] ) )
045                    {
046                        throw new IllegalArgumentException( "Common element, \"" + mandatoryComponents[i]
047                            + "\" detected for Mandatory and Optional components." );
048                    }
049                }
050            }
051    
052            mandatoryComponentsMonitor = new MandatoryComponentsMonitor( mandatoryComponents );
053            optionalComponentsMonitor = new OptionalComponentsMonitor( optionalComponents );
054        }
055    
056    
057        public ComponentsMonitor useComponent( String component )
058        {
059            try
060            {
061                mandatoryComponentsMonitor.useComponent( component );
062            }
063            catch ( IllegalArgumentException e1 )
064            {
065                try
066                {
067                    optionalComponentsMonitor.useComponent( component );
068                }
069                catch ( IllegalArgumentException e2 )
070                {
071                    throw new IllegalArgumentException( "Unregistered or previously used component: " + component );
072                }
073            }
074    
075            return this;
076        }
077    
078    
079        public boolean allComponentsUsed()
080        {
081            return ( mandatoryComponentsMonitor.allComponentsUsed() && optionalComponentsMonitor.allComponentsUsed() );
082        }
083    
084    
085        public boolean finalStateValid()
086        {
087            return ( mandatoryComponentsMonitor.finalStateValid() && optionalComponentsMonitor.finalStateValid() );
088        }
089    
090    
091        public List getRemainingComponents()
092        {
093            List remainingComponents = new LinkedList();
094    
095            remainingComponents.addAll( mandatoryComponentsMonitor.getRemainingComponents() );
096            remainingComponents.addAll( optionalComponentsMonitor.getRemainingComponents() );
097    
098            return Collections.unmodifiableList( remainingComponents );
099        }
100    
101    }