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        throws IllegalArgumentException
056    {
057        // check for common elements
058        for ( int i = 0; i < mandatoryComponents.length; i++ )
059        {
060            for ( int j = 0; j < optionalComponents.length; j++ )
061            {
062                if ( mandatoryComponents[i].equals( optionalComponents[j] ) )
063                {
064                    throw new IllegalArgumentException( I18n.err( I18n.ERR_04415, mandatoryComponents[i] ) );
065                }
066            }
067        }
068
069        mandatoryComponentsMonitor = new MandatoryComponentsMonitor( mandatoryComponents );
070        optionalComponentsMonitor = new OptionalComponentsMonitor( optionalComponents );
071    }
072
073
074    /**
075     * {@inheritDoc}
076     */
077    public ComponentsMonitor useComponent( String component )
078    {
079        try
080        {
081            mandatoryComponentsMonitor.useComponent( component );
082        }
083        catch ( IllegalArgumentException e1 )
084        {
085            try
086            {
087                optionalComponentsMonitor.useComponent( component );
088            }
089            catch ( IllegalArgumentException e2 )
090            {
091                throw new IllegalArgumentException( I18n.err( I18n.ERR_04416, component ), e1 );
092            }
093        }
094
095        return this;
096    }
097
098
099    /**
100     * {@inheritDoc}
101     */
102    public boolean allComponentsUsed()
103    {
104        return ( mandatoryComponentsMonitor.allComponentsUsed() && optionalComponentsMonitor.allComponentsUsed() );
105    }
106
107
108    /**
109     * {@inheritDoc}
110     */
111    public boolean finalStateValid()
112    {
113        return ( mandatoryComponentsMonitor.finalStateValid() && optionalComponentsMonitor.finalStateValid() );
114    }
115
116
117    /**
118     * {@inheritDoc}
119     */
120    public List<String> getRemainingComponents()
121    {
122        List<String> remainingComponents = new LinkedList<String>();
123
124        remainingComponents.addAll( mandatoryComponentsMonitor.getRemainingComponents() );
125        remainingComponents.addAll( optionalComponentsMonitor.getRemainingComponents() );
126
127        return Collections.unmodifiableList( remainingComponents );
128    }
129
130}