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 package org.apache.directory.ldap.client.api.search; 21 22 23 import java.util.ArrayList; 24 import java.util.List; 25 26 import org.apache.directory.api.i18n.I18n; 27 28 29 /** 30 * An implementation of the Filter interface for the AND and OR Filters 31 * 32 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 33 */ 34 /* No qualifier*/final class SetOfFiltersFilter extends AbstractFilter 35 { 36 /** The operator to use with this set (AND or OR) */ 37 private FilterOperator operator; 38 39 /** The list of inner filters */ 40 private List<Filter> filters; 41 42 43 /** 44 * Creates a new instance of SetOfFiltersFilter. 45 * 46 * @param operator The operator 47 */ 48 private SetOfFiltersFilter( FilterOperator operator ) 49 { 50 this.operator = operator; 51 this.filters = new ArrayList<>(); 52 } 53 54 55 /** 56 * Adds a Filter into the set of Filters 57 * 58 * @param filter The filter to add 59 * @return The Set of Filters with the added filter 60 */ 61 public SetOfFiltersFilter add( Filter filter ) 62 { 63 filters.add( filter ); 64 return this; 65 } 66 67 68 /** 69 * Injects a list of Filters into the set of Filters 70 * 71 * @param filters The filters to inject 72 * @return The Set of Filters with the injected filters 73 */ 74 public SetOfFiltersFilter addAll( Filter... filters ) 75 { 76 for ( Filter filter : filters ) 77 { 78 this.filters.add( filter ); 79 } 80 81 return this; 82 } 83 84 85 /** 86 * Injects a list of Filters into the set of Filters 87 * 88 * @param filters The filters to inject 89 * @return The Set of Filters with the injected filters 90 */ 91 public SetOfFiltersFilter addAll( List<Filter> filters ) 92 { 93 this.filters.addAll( filters ); 94 95 return this; 96 } 97 98 99 /** 100 * Creates an AND set of filters 101 * 102 * @param filters The inner filters 103 * @return An AND filter 104 */ 105 public static SetOfFiltersFilter and( Filter... filters ) 106 { 107 return new SetOfFiltersFilter( FilterOperator.AND ).addAll( filters ); 108 } 109 110 111 /** 112 * Creates an OR set of filters 113 * 114 * @param filters The inner filters 115 * @return An OR filter 116 */ 117 public static SetOfFiltersFilter or( Filter... filters ) 118 { 119 return new SetOfFiltersFilter( FilterOperator.OR ).addAll( filters ); 120 } 121 122 123 /** 124 * {@inheritDoc} 125 */ 126 @Override 127 public StringBuilder build( StringBuilder builder ) 128 { 129 if ( filters.isEmpty() ) 130 { 131 throw new IllegalStateException( I18n.err( I18n.ERR_04166_ONE_FILTER_REQUIRED ) ); 132 } 133 134 builder.append( "(" ).append( operator.operator() ); 135 136 for ( Filter filter : filters ) 137 { 138 filter.build( builder ); 139 } 140 141 return builder.append( ")" ); 142 } 143 }