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_04415, mandatoryComponents[i] ) ); 064 } 065 } 066 } 067 068 mandatoryComponentsMonitor = new MandatoryComponentsMonitor( mandatoryComponents ); 069 optionalComponentsMonitor = new OptionalComponentsMonitor( optionalComponents ); 070 } 071 072 073 /** 074 * {@inheritDoc} 075 */ 076 public ComponentsMonitor useComponent( String component ) 077 { 078 try 079 { 080 mandatoryComponentsMonitor.useComponent( component ); 081 } 082 catch ( IllegalArgumentException e1 ) 083 { 084 try 085 { 086 optionalComponentsMonitor.useComponent( component ); 087 } 088 catch ( IllegalArgumentException e2 ) 089 { 090 throw new IllegalArgumentException( I18n.err( I18n.ERR_04416, component ), e1 ); 091 } 092 } 093 094 return this; 095 } 096 097 098 /** 099 * {@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}