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