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
021package org.apache.directory.api.util;
022
023
024import java.util.Collections;
025import java.util.LinkedList;
026import java.util.List;
027
028import org.apache.directory.api.i18n.I18n;
029
030
031/**
032 * A monitor that tracks both, mandatory and optional components.
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036public class MandatoryAndOptionalComponentsMonitor implements ComponentsMonitor
037{
038
039    /** The mandatory components monitor. */
040    private ComponentsMonitor mandatoryComponentsMonitor;
041
042    /** The optional components monitor. */
043    private ComponentsMonitor optionalComponentsMonitor;
044
045
046    /**
047     * Instantiates a new mandatory and optional components monitor. The mandatory and optional
048     * components must be disjunct.
049     *
050     * @param mandatoryComponents the mandatory components
051     * @param optionalComponents the optional components
052     * @throws IllegalArgumentException if the same component is defined as mandatory and optional
053     */
054    public MandatoryAndOptionalComponentsMonitor( String[] mandatoryComponents, String[] optionalComponents )
055    {
056        // check for common elements
057        for ( int i = 0; i < mandatoryComponents.length; i++ )
058        {
059            for ( int j = 0; j < optionalComponents.length; j++ )
060            {
061                if ( mandatoryComponents[i].equals( optionalComponents[j] ) )
062                {
063                    throw new IllegalArgumentException( I18n.err( I18n.ERR_17035_DUPLICATED_COMMON_ELEMENT, 
064                        mandatoryComponents[i] ) );
065                }
066            }
067        }
068
069        mandatoryComponentsMonitor = new MandatoryComponentsMonitor( mandatoryComponents );
070        optionalComponentsMonitor = new OptionalComponentsMonitor( optionalComponents );
071    }
072
073
074    /**
075     * {@inheritDoc}
076     */
077    @Override
078    public ComponentsMonitor useComponent( String component )
079    {
080        try
081        {
082            mandatoryComponentsMonitor.useComponent( component );
083        }
084        catch ( IllegalArgumentException e1 )
085        {
086            try
087            {
088                optionalComponentsMonitor.useComponent( component );
089            }
090            catch ( IllegalArgumentException e2 )
091            {
092                throw new IllegalArgumentException( I18n.err( I18n.ERR_17036_UNREGISTRED_COMPONENT, component ), e1 );
093            }
094        }
095
096        return this;
097    }
098
099
100    /**
101     * {@inheritDoc}
102     */
103    @Override
104    public boolean allComponentsUsed()
105    {
106        return mandatoryComponentsMonitor.allComponentsUsed() && optionalComponentsMonitor.allComponentsUsed();
107    }
108
109
110    /**
111     * {@inheritDoc}
112     */
113    @Override
114    public boolean finalStateValid()
115    {
116        return mandatoryComponentsMonitor.finalStateValid() && optionalComponentsMonitor.finalStateValid();
117    }
118
119
120    /**
121     * {@inheritDoc}
122     */
123    @Override
124    public List<String> getRemainingComponents()
125    {
126        List<String> remainingComponents = new LinkedList<>();
127
128        remainingComponents.addAll( mandatoryComponentsMonitor.getRemainingComponents() );
129        remainingComponents.addAll( optionalComponentsMonitor.getRemainingComponents() );
130
131        return Collections.unmodifiableList( remainingComponents );
132    }
133
134}