View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  
21  package org.apache.directory.api.util;
22  
23  
24  import java.util.Collections;
25  import java.util.LinkedList;
26  import java.util.List;
27  
28  import org.apache.directory.api.i18n.I18n;
29  
30  
31  /**
32   * A monitor that tracks both, mandatory and optional components.
33   *
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public class MandatoryAndOptionalComponentsMonitor implements ComponentsMonitor
37  {
38  
39      /** The mandatory components monitor. */
40      private ComponentsMonitor mandatoryComponentsMonitor;
41  
42      /** The optional components monitor. */
43      private ComponentsMonitor optionalComponentsMonitor;
44  
45  
46      /**
47       * Instantiates a new mandatory and optional components monitor. The mandatory and optional
48       * components must be disjunct.
49       *
50       * @param mandatoryComponents the mandatory components
51       * @param optionalComponents the optional components
52       * @throws IllegalArgumentException if the same component is defined as mandatory and optional
53       */
54      public MandatoryAndOptionalComponentsMonitor( String[] mandatoryComponents, String[] optionalComponents )
55      {
56          // check for common elements
57          for ( int i = 0; i < mandatoryComponents.length; i++ )
58          {
59              for ( int j = 0; j < optionalComponents.length; j++ )
60              {
61                  if ( mandatoryComponents[i].equals( optionalComponents[j] ) )
62                  {
63                      throw new IllegalArgumentException( I18n.err( I18n.ERR_04415, mandatoryComponents[i] ) );
64                  }
65              }
66          }
67  
68          mandatoryComponentsMonitor = new MandatoryComponentsMonitor( mandatoryComponents );
69          optionalComponentsMonitor = new OptionalComponentsMonitor( optionalComponents );
70      }
71  
72  
73      /**
74       * {@inheritDoc}
75       */
76      public ComponentsMonitor useComponent( String component )
77      {
78          try
79          {
80              mandatoryComponentsMonitor.useComponent( component );
81          }
82          catch ( IllegalArgumentException e1 )
83          {
84              try
85              {
86                  optionalComponentsMonitor.useComponent( component );
87              }
88              catch ( IllegalArgumentException e2 )
89              {
90                  throw new IllegalArgumentException( I18n.err( I18n.ERR_04416, component ), e1 );
91              }
92          }
93  
94          return this;
95      }
96  
97  
98      /**
99       * {@inheritDoc}
100      */
101     public boolean allComponentsUsed()
102     {
103         return ( mandatoryComponentsMonitor.allComponentsUsed() && optionalComponentsMonitor.allComponentsUsed() );
104     }
105 
106 
107     /**
108      * {@inheritDoc}
109      */
110     public boolean finalStateValid()
111     {
112         return ( mandatoryComponentsMonitor.finalStateValid() && optionalComponentsMonitor.finalStateValid() );
113     }
114 
115 
116     /**
117      * {@inheritDoc}
118      */
119     public List<String> getRemainingComponents()
120     {
121         List<String> remainingComponents = new LinkedList<String>();
122 
123         remainingComponents.addAll( mandatoryComponentsMonitor.getRemainingComponents() );
124         remainingComponents.addAll( optionalComponentsMonitor.getRemainingComponents() );
125 
126         return Collections.unmodifiableList( remainingComponents );
127     }
128 
129 }