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   *    https://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_17035_DUPLICATED_COMMON_ELEMENT, 
64                          mandatoryComponents[i] ) );
65                  }
66              }
67          }
68  
69          mandatoryComponentsMonitor = new MandatoryComponentsMonitor( mandatoryComponents );
70          optionalComponentsMonitor = new OptionalComponentsMonitor( optionalComponents );
71      }
72  
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public ComponentsMonitor useComponent( String component )
79      {
80          try
81          {
82              mandatoryComponentsMonitor.useComponent( component );
83          }
84          catch ( IllegalArgumentException e1 )
85          {
86              try
87              {
88                  optionalComponentsMonitor.useComponent( component );
89              }
90              catch ( IllegalArgumentException e2 )
91              {
92                  throw new IllegalArgumentException( I18n.err( I18n.ERR_17036_UNREGISTRED_COMPONENT, component ), e1 );
93              }
94          }
95  
96          return this;
97      }
98  
99  
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 }