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          throws IllegalArgumentException
56      {
57          // check for common elements
58          for ( int i = 0; i < mandatoryComponents.length; i++ )
59          {
60              for ( int j = 0; j < optionalComponents.length; j++ )
61              {
62                  if ( mandatoryComponents[i].equals( optionalComponents[j] ) )
63                  {
64                      throw new IllegalArgumentException( I18n.err( I18n.ERR_04415, mandatoryComponents[i] ) );
65                  }
66              }
67          }
68  
69          mandatoryComponentsMonitor = new MandatoryComponentsMonitor( mandatoryComponents );
70          optionalComponentsMonitor = new OptionalComponentsMonitor( optionalComponents );
71      }
72  
73  
74      /**
75       * {@inheritDoc}
76       */
77      public ComponentsMonitor useComponent( String component )
78      {
79          try
80          {
81              mandatoryComponentsMonitor.useComponent( component );
82          }
83          catch ( IllegalArgumentException e1 )
84          {
85              try
86              {
87                  optionalComponentsMonitor.useComponent( component );
88              }
89              catch ( IllegalArgumentException e2 )
90              {
91                  throw new IllegalArgumentException( I18n.err( I18n.ERR_04416, component ) );
92              }
93          }
94  
95          return this;
96      }
97  
98  
99      /**
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 }